Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update build, adds PHP8.3 support, adds type hints and return types. #6

Merged
merged 13 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
sanity-check:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
env:
key: cache-v1
extensions: apc, redis, apcu, memcache, memcached
Expand All @@ -18,7 +18,7 @@ jobs:
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: 7.4
php-version: 8.0
extensions: ${{ env.extensions }}
key: ${{ env.key }}

Expand All @@ -32,7 +32,7 @@ jobs:
- name: Setup PHP {{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.0
tools: composer
extensions: ${{ env.extensions }}
ini-values: apc.enable_cli=1
Expand All @@ -54,7 +54,7 @@ jobs:


unit-test:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
needs: [sanity-check]
env:
key: cache-v1
Expand All @@ -72,7 +72,7 @@ jobs:
- 11211:11211
strategy:
matrix:
php-versions: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2' ]
php-versions: [ '8.0', '8.1', '8.2', '8.3' ]

steps:

Expand Down
11 changes: 0 additions & 11 deletions .php_cs.dist

This file was deleted.

2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

[![GitHub Build](https://github.com/DavidGoodwin/RateLimit/actions/workflows/php.yml/badge.svg)](https://github.com/DavidGoodwin/RateLimit/actions/workflows/php.yml)

[![Travis Build](https://travis-ci.org/DavidGoodwin/RateLimit.svg)](https://travis-ci.org/DavidGoodwin/RateLimit/)

[![Coverage Status](https://coveralls.io/repos/github/DavidGoodwin/RateLimit/badge.svg?branch=master)](https://coveralls.io/github/DavidGoodwin/RateLimit?branch=master)

PHP Rate Limiting Library With Token Bucket Algorithm with minimal external dependencies.
Expand Down
21 changes: 13 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
"name": "palepurple/rate-limit",
"description": "PHP rate limiting library with Token Bucket Algorithm, originally touhonoob/rate-limit",
"require": {
"php": ">= 5.6"
"php": ">= 8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.7",
"php-parallel-lint/php-parallel-lint": "^1.0",
"php-coveralls/php-coveralls": "^2.2",
"phpunit/phpunit": "^8",
"predis/predis": "^1.1",
"phpunit/phpunit": "^8|^9",
"predis/predis": "^1.1|2.0",
"psr/cache": "^1.0",
"tedivm/stash": "^0.16",
"tedivm/stash": "^0.16|^1.0",
"psalm/phar": "*",
"phpstan/phpstan": "*"
"phpstan/phpstan": "*",
"slevomat/coding-standard": "^8.15"
},
"suggest": {
"tedivm/stash": "^0.15",
Expand Down Expand Up @@ -45,8 +45,13 @@
"psalm" : "@php ./vendor/bin/psalm.phar src",
"phpstan" : "@php vendor/bin/phpstan analyse --level 6 src",
"lint": "@php ./vendor/bin/parallel-lint --exclude vendor/ .",
"check-format": "@php ./vendor/bin/php-cs-fixer fix --ansi --dry-run --diff",
"format": "@php ./vendor/bin/php-cs-fixer fix --ansi",
"check-format": "@php ./vendor/bin/phpcs src",
"format": "@php ./vendor/bin/phpcbf src",
"test": "@php ./vendor/bin/phpunit"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
10 changes: 10 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<rule ref="PSR12">
<exclude name="Generic.Files.LineLength.TooLong"/>
</rule>

<config name="installed_paths" value="../../slevomat/coding-standard"/>
</ruleset>
16 changes: 0 additions & 16 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@
</errorLevel>
</UndefinedClass>

<LessSpecificReturnType errorLevel="info" />

<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->

<DeprecatedMethod errorLevel="info" />
<DeprecatedProperty errorLevel="info" />
<DeprecatedClass errorLevel="info" />
<DeprecatedConstant errorLevel="info" />
<DeprecatedInterface errorLevel="info" />
<DeprecatedTrait errorLevel="info" />

<MissingClosureReturnType errorLevel="info" />

<MissingClosureParamType errorLevel="info" />
<RedundantCondition errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<UnresolvableInclude errorLevel="info" />
</issueHandlers>
Expand Down
23 changes: 9 additions & 14 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,18 @@ abstract class Adapter
* @param float $value
* @param int $ttl - seconds after which this entry will expire e.g 50
*/
abstract public function set($key, $value, $ttl);

/**
* @param string $key
* @return float
*/
abstract public function get($key);

abstract public function set(string $key, float $value, int $ttl);

/**
* @param string $key
* @return bool
* @return float - the amount of request allowance left
*/
abstract public function exists($key);

abstract public function get(string $key): float;

abstract public function exists(string $key): bool;

/**
* @return bool
* @param string $key
* @return bool - true if delete works
*/
abstract public function del($key);
abstract public function del(string $key): bool;
}
10 changes: 5 additions & 5 deletions src/Adapter/APC.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
*/
class APC extends Adapter
{
public function set($key, $value, $ttl)
public function set(string $key, float $value, int $ttl)
{
return apc_store($key, $value, $ttl);
}

public function get($key)
public function get(string $key): float
{
return apc_fetch($key);
return (float) apc_fetch($key);
}

public function exists($key)
public function exists(string $key): bool
{
return apc_exists($key);
}

public function del($key)
public function del(string $key): bool
{
return apc_delete($key);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Adapter/APCu.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
*/
class APCu extends \PalePurple\RateLimit\Adapter
{
public function set($key, $value, $ttl)
public function set(string $key, float $value, int $ttl): bool
{
return apcu_store($key, $value, $ttl);
}

public function get($key)
public function get(string $key): float
{
return apcu_fetch($key);
}

public function exists($key)
public function exists(string $key): bool
{
return apcu_exists($key);
}

public function del($key)
public function del(string $key): bool
{
return apcu_delete($key);
}
Expand Down
34 changes: 16 additions & 18 deletions src/Adapter/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class Memcached extends \PalePurple\RateLimit\Adapter
{

/**
* @var \Memcached
*/
Expand All @@ -15,37 +14,36 @@ public function __construct(\Memcached $memcached)
$this->memcached = $memcached;
}

public function set($key, $value, $ttl)
public function set(string $key, float $value, int $ttl): bool
{
return $this->memcached->set($key, $value, $ttl);
}

/**
* @return float
* @param string $key
*/
public function get($key)
public function get(string $key): float
{
$val = $this->_get($key);
return (float) $val;
$ret = $this->realGet($key);
if (is_float($ret)) {
return $ret;
}
throw new \InvalidArgumentException("Unexpected data type from memcache, expected float, got " . gettype($ret));
}

/**
* @return bool|float
* @param string $key
*/
private function _get($key)
private function realGet(string $key): bool|float
{
return $this->memcached->get($key);
$ret = $this->memcached->get($key);
if (is_float($ret) || is_bool($ret)) {
return $ret;
}
throw new \InvalidArgumentException("Unsupported data type from memcache: " . gettype($ret));
}

public function exists($key)
public function exists(string $key): bool
{
$val = $this->_get($key);
$val = $this->realGet($key);
return $val !== false;
}

public function del($key)
public function del(string $key): bool
{
return $this->memcached->delete($key);
}
Expand Down
28 changes: 7 additions & 21 deletions src/Adapter/Predis.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,30 @@
*/
class Predis extends \PalePurple\RateLimit\Adapter
{

/**
* @var \Predis\ClientInterface
*/
protected $redis;
protected \Predis\ClientInterface $redis;

public function __construct(\Predis\ClientInterface $client)
{
$this->redis = $client;
}


/**
* @param string $key
* @param float $value
* @param int $ttl
* @return bool
*/
public function set($key, $value, $ttl)
public function set(string $key, float $value, int $ttl): bool
{
return $this->redis->set($key, (string) $value, "ex", $ttl);
$this->redis->set($key, (string)$value, "ex", $ttl);
return true;
}

/**
* @return float
* @param string $key
*/
public function get($key)
public function get(string $key): float
{
return (float)$this->redis->get($key);
}

public function exists($key)
public function exists(string $key): bool
{
return (bool)$this->redis->exists($key);
}

public function del($key)
public function del(string $key): bool
{
return (bool)$this->redis->del([$key]);
}
Expand Down
Loading
Loading