diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml new file mode 100644 index 00000000..ee78d555 --- /dev/null +++ b/.github/workflows/coding-standards.yml @@ -0,0 +1,28 @@ +name: Linters + +on: + push: + branches: + - main + - v2.** + pull_request: + +jobs: + + php-cs-fixer: + name: linter + runs-on: ubuntu-latest + + steps: + + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup PHP with Composer and extensions + with: + php-version: 8.1 + tools: php-cs-fixer + uses: shivammathur/setup-php@v2 + + - name: Run php-cs-fixer + run: php-cs-fixer fix --diff --dry-run --allow-risky=yes --using-cache=no diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 0c9e048f..48c8c16e 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -13,6 +13,7 @@ EOS; return (new PhpCsFixer\Config) ->setUsingCache(false) ->setRules([ + '@PHP71Migration' => true, 'header_comment' => ['header' => $PREDIS_HEADER], '@Symfony' => true, 'phpdoc_separation' => false, diff --git a/examples/custom_cluster_distributor.php b/examples/custom_cluster_distributor.php index 9d964dee..4efd892d 100644 --- a/examples/custom_cluster_distributor.php +++ b/examples/custom_cluster_distributor.php @@ -54,7 +54,7 @@ class NaiveDistributor implements DistributorInterface, HashGeneratorInterface public function getBySlot($slot) { - return isset($this->nodes[$slot]) ? $this->nodes[$slot] : null; + return $this->nodes[$slot] ?? null; } public function getByHash($hash) diff --git a/examples/transaction_using_cas.php b/examples/transaction_using_cas.php index 65e0b29c..edc85faa 100644 --- a/examples/transaction_using_cas.php +++ b/examples/transaction_using_cas.php @@ -36,7 +36,7 @@ function zpop($client, $key) ]; $client->transaction($options, function ($tx) use ($key, &$element) { - @list($element) = $tx->zrange($key, 0, 0); + @[$element] = $tx->zrange($key, 0, 0); if (isset($element)) { $tx->multi(); // With CAS, MULTI *must* be explicitly invoked. diff --git a/src/Collection/Iterator/CursorBasedIterator.php b/src/Collection/Iterator/CursorBasedIterator.php index 012d0fed..baa3a7c9 100644 --- a/src/Collection/Iterator/CursorBasedIterator.php +++ b/src/Collection/Iterator/CursorBasedIterator.php @@ -118,7 +118,7 @@ abstract class CursorBasedIterator implements Iterator */ protected function fetch() { - list($cursor, $elements) = $this->executeCommand(); + [$cursor, $elements] = $this->executeCommand(); if (!$cursor) { $this->fetchmore = false; diff --git a/src/Command/Redis/BITOP.php b/src/Command/Redis/BITOP.php index e6e30fcc..fcf0495c 100644 --- a/src/Command/Redis/BITOP.php +++ b/src/Command/Redis/BITOP.php @@ -33,7 +33,7 @@ class BITOP extends RedisCommand public function setArguments(array $arguments) { if (count($arguments) === 3 && is_array($arguments[2])) { - list($operation, $destination) = $arguments; + [$operation, $destination] = $arguments; $arguments = $arguments[2]; array_unshift($arguments, $operation, $destination); } diff --git a/src/Command/Redis/BLPOP.php b/src/Command/Redis/BLPOP.php index 3c222bf1..feb7ee4b 100644 --- a/src/Command/Redis/BLPOP.php +++ b/src/Command/Redis/BLPOP.php @@ -33,7 +33,7 @@ class BLPOP extends RedisCommand public function setArguments(array $arguments) { if (count($arguments) === 2 && is_array($arguments[0])) { - list($arguments, $timeout) = $arguments; + [$arguments, $timeout] = $arguments; array_push($arguments, $timeout); } diff --git a/src/Command/Redis/BRPOP.php b/src/Command/Redis/BRPOP.php index b69a6ef8..cf88dc90 100644 --- a/src/Command/Redis/BRPOP.php +++ b/src/Command/Redis/BRPOP.php @@ -33,7 +33,7 @@ class BRPOP extends RedisCommand public function setArguments(array $arguments) { if (count($arguments) === 2 && is_array($arguments[0])) { - list($arguments, $timeout) = $arguments; + [$arguments, $timeout] = $arguments; array_push($arguments, $timeout); } diff --git a/src/Command/Redis/CLIENT.php b/src/Command/Redis/CLIENT.php index 7717574a..6c7bdc9c 100644 --- a/src/Command/Redis/CLIENT.php +++ b/src/Command/Redis/CLIENT.php @@ -63,7 +63,7 @@ class CLIENT extends RedisCommand $client = []; foreach (explode(' ', $clientData) as $kv) { - @list($k, $v) = explode('=', $kv); + @[$k, $v] = explode('=', $kv); $client[$k] = $v; } diff --git a/src/Command/Redis/INFO.php b/src/Command/Redis/INFO.php index 587b3d34..cb13a4cb 100644 --- a/src/Command/Redis/INFO.php +++ b/src/Command/Redis/INFO.php @@ -62,7 +62,7 @@ class INFO extends RedisCommand continue; } - list($k, $v) = $this->parseRow($row); + [$k, $v] = $this->parseRow($row); $current[$k] = $v; } @@ -81,7 +81,7 @@ class INFO extends RedisCommand continue; } - list($k, $v) = $this->parseRow($row); + [$k, $v] = $this->parseRow($row); $info[$k] = $v; } @@ -97,7 +97,7 @@ class INFO extends RedisCommand */ protected function parseRow($row) { - list($k, $v) = explode(':', $row, 2); + [$k, $v] = explode(':', $row, 2); if (preg_match('/^db\d+$/', $k)) { $v = $this->parseDatabaseStats($v); @@ -118,7 +118,7 @@ class INFO extends RedisCommand $db = []; foreach (explode(',', $str) as $dbvar) { - list($dbvk, $dbvv) = explode('=', $dbvar); + [$dbvk, $dbvv] = explode('=', $dbvar); $db[trim($dbvk)] = $dbvv; } diff --git a/src/Command/Redis/ZRANGEBYLEX.php b/src/Command/Redis/ZRANGEBYLEX.php index b8913fa8..18b4a6d3 100644 --- a/src/Command/Redis/ZRANGEBYLEX.php +++ b/src/Command/Redis/ZRANGEBYLEX.php @@ -37,8 +37,8 @@ class ZRANGEBYLEX extends ZRANGE $limit = array_change_key_case($opts['LIMIT'], CASE_UPPER); $finalizedOpts[] = 'LIMIT'; - $finalizedOpts[] = isset($limit['OFFSET']) ? $limit['OFFSET'] : $limit[0]; - $finalizedOpts[] = isset($limit['COUNT']) ? $limit['COUNT'] : $limit[1]; + $finalizedOpts[] = $limit['OFFSET'] ?? $limit[0]; + $finalizedOpts[] = $limit['COUNT'] ?? $limit[1]; } return $finalizedOpts; diff --git a/src/Command/Redis/ZRANGEBYSCORE.php b/src/Command/Redis/ZRANGEBYSCORE.php index 696fdb90..66cbe4ea 100644 --- a/src/Command/Redis/ZRANGEBYSCORE.php +++ b/src/Command/Redis/ZRANGEBYSCORE.php @@ -37,8 +37,8 @@ class ZRANGEBYSCORE extends ZRANGE $limit = array_change_key_case($opts['LIMIT'], CASE_UPPER); $finalizedOpts[] = 'LIMIT'; - $finalizedOpts[] = isset($limit['OFFSET']) ? $limit['OFFSET'] : $limit[0]; - $finalizedOpts[] = isset($limit['COUNT']) ? $limit['COUNT'] : $limit[1]; + $finalizedOpts[] = $limit['OFFSET'] ?? $limit[0]; + $finalizedOpts[] = $limit['COUNT'] ?? $limit[1]; } return array_merge($finalizedOpts, parent::prepareOptions($options)); diff --git a/src/Connection/Cluster/RedisCluster.php b/src/Connection/Cluster/RedisCluster.php index 020b1de2..85e979b6 100644 --- a/src/Connection/Cluster/RedisCluster.php +++ b/src/Connection/Cluster/RedisCluster.php @@ -285,7 +285,7 @@ class RedisCluster implements ClusterInterface, IteratorAggregate, Countable foreach ($response as $slots) { // We only support master servers for now, so we ignore subsequent // elements in the $slots array identifying slaves. - list($start, $end, $master) = $slots; + [$start, $end, $master] = $slots; if ($master[0] === '') { $this->slotmap->setSlots($start, $end, (string) $connection); @@ -461,7 +461,7 @@ class RedisCluster implements ClusterInterface, IteratorAggregate, Countable */ protected function onMovedResponse(CommandInterface $command, $details) { - list($slot, $connectionID) = explode(' ', $details, 2); + [$slot, $connectionID] = explode(' ', $details, 2); if (!$connection = $this->getConnectionById($connectionID)) { $connection = $this->createConnection($connectionID); @@ -487,7 +487,7 @@ class RedisCluster implements ClusterInterface, IteratorAggregate, Countable */ protected function onAskResponse(CommandInterface $command, $details) { - list($slot, $connectionID) = explode(' ', $details, 2); + [$slot, $connectionID] = explode(' ', $details, 2); if (!$connection = $this->getConnectionById($connectionID)) { $connection = $this->createConnection($connectionID); diff --git a/src/Connection/Replication/MasterSlaveReplication.php b/src/Connection/Replication/MasterSlaveReplication.php index b93c8615..613a0581 100644 --- a/src/Connection/Replication/MasterSlaveReplication.php +++ b/src/Connection/Replication/MasterSlaveReplication.php @@ -375,7 +375,7 @@ class MasterSlaveReplication implements ReplicationInterface continue; } - list($k, $v) = explode(':', $row, 2); + [$k, $v] = explode(':', $row, 2); $info[$k] = $v; } diff --git a/src/Monitor/Consumer.php b/src/Monitor/Consumer.php index d7f0954e..eb46b049 100644 --- a/src/Monitor/Consumer.php +++ b/src/Monitor/Consumer.php @@ -166,7 +166,7 @@ class Consumer implements Iterator }; $event = preg_replace_callback('/ \(db (\d+)\) | \[(\d+) (.*?)\] /', $callback, $event, 1); - @list($timestamp, $command, $arguments) = explode(' ', $event, 3); + @[$timestamp, $command, $arguments] = explode(' ', $event, 3); return (object) [ 'timestamp' => (float) $timestamp, diff --git a/src/Response/Error.php b/src/Response/Error.php index 39a147f5..3a1c5034 100644 --- a/src/Response/Error.php +++ b/src/Response/Error.php @@ -41,7 +41,7 @@ class Error implements ErrorInterface */ public function getErrorType() { - list($errorType) = explode(' ', $this->getMessage(), 2); + [$errorType] = explode(' ', $this->getMessage(), 2); return $errorType; } diff --git a/src/Response/ServerException.php b/src/Response/ServerException.php index e61b2f95..305fe409 100644 --- a/src/Response/ServerException.php +++ b/src/Response/ServerException.php @@ -26,7 +26,7 @@ class ServerException extends PredisException implements ErrorInterface */ public function getErrorType() { - list($errorType) = explode(' ', $this->getMessage(), 2); + [$errorType] = explode(' ', $this->getMessage(), 2); return $errorType; } diff --git a/tests/PHPUnit/PredisTestCase.php b/tests/PHPUnit/PredisTestCase.php index 095c452c..03856843 100644 --- a/tests/PHPUnit/PredisTestCase.php +++ b/tests/PHPUnit/PredisTestCase.php @@ -75,7 +75,7 @@ abstract class PredisTestCase extends \PHPUnit\Framework\TestCase public function assertRedisCommand($expected, $actual, string $message = ''): void { if (is_array($expected)) { - @list($command, $arguments) = $expected; + @[$command, $arguments] = $expected; } else { $command = $expected; $arguments = null; diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index 3051fb9b..04bac903 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -1268,9 +1268,9 @@ class ClientTest extends PredisTestCase { $defaults = $this->getDefaultParametersArray(); - $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme']; - $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host']; - $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port']; + $scheme = $parameters['scheme'] ?? $defaults['scheme']; + $host = $parameters['host'] ?? $defaults['host']; + $port = $parameters['port'] ?? $defaults['port']; unset($parameters['scheme'], $parameters['host'], $parameters['port']); $uriString = "$scheme://$host:$port/?"; diff --git a/tests/Predis/Cluster/Distributor/KetamaRingTest.php b/tests/Predis/Cluster/Distributor/KetamaRingTest.php index f3845ff2..054769b5 100644 --- a/tests/Predis/Cluster/Distributor/KetamaRingTest.php +++ b/tests/Predis/Cluster/Distributor/KetamaRingTest.php @@ -29,7 +29,7 @@ class KetamaRingTest extends PredisDistributorTestCase { /** @var HashGeneratorInterface */ $ring = $this->getDistributorInstance(); - list(, $hash) = unpack('V', md5('foobar', true)); + [, $hash] = unpack('V', md5('foobar', true)); $this->assertEquals($hash, $ring->hash('foobar')); } diff --git a/tests/Predis/Command/Redis/INFO_Test.php b/tests/Predis/Command/Redis/INFO_Test.php index e6015e12..9ad12749 100644 --- a/tests/Predis/Command/Redis/INFO_Test.php +++ b/tests/Predis/Command/Redis/INFO_Test.php @@ -310,7 +310,7 @@ BUFFER; $command = $this->getCommand(); $this->assertIsArray($info = $redis->executeCommand($command)); - $this->assertArrayHasKey('redis_version', isset($info['Server']) ? $info['Server'] : $info); + $this->assertArrayHasKey('redis_version', $info['Server'] ?? $info); } /** diff --git a/tests/Predis/Command/Redis/SETRANGE_Test.php b/tests/Predis/Command/Redis/SETRANGE_Test.php index 8c8edb15..e135a414 100644 --- a/tests/Predis/Command/Redis/SETRANGE_Test.php +++ b/tests/Predis/Command/Redis/SETRANGE_Test.php @@ -98,7 +98,7 @@ class SETRANGE_Test extends PredisCommandTestCase $this->assertSame(4, $redis->setrange('key:binary', 0, pack('i', -2147483648))); - list($unpacked) = array_values(unpack('i', $redis->get('key:binary'))); + [$unpacked] = array_values(unpack('i', $redis->get('key:binary'))); $this->assertEquals(-2147483648, $unpacked); } diff --git a/tests/Predis/Command/Redis/UNSUBSCRIBE_Test.php b/tests/Predis/Command/Redis/UNSUBSCRIBE_Test.php index 7fe0fafa..418aa63e 100644 --- a/tests/Predis/Command/Redis/UNSUBSCRIBE_Test.php +++ b/tests/Predis/Command/Redis/UNSUBSCRIBE_Test.php @@ -121,8 +121,8 @@ class UNSUBSCRIBE_Test extends PredisCommandTestCase $this->assertSame(['subscribe', 'channel:foo', 1], $redis->subscribe('channel:foo')); $this->assertSame(['subscribe', 'channel:bar', 2], $redis->subscribe('channel:bar')); - list($_, $unsubscribed1, $_) = $redis->unsubscribe(); - list($_, $unsubscribed2, $_) = $redis->getConnection()->read(); + [$_, $unsubscribed1, $_] = $redis->unsubscribe(); + [$_, $unsubscribed2, $_] = $redis->getConnection()->read(); $this->assertSameValues(['channel:foo', 'channel:bar'], [$unsubscribed1, $unsubscribed2]); $this->assertSame('echoed', $redis->echo('echoed')); diff --git a/tests/Predis/Configuration/Option/AggregateTest.php b/tests/Predis/Configuration/Option/AggregateTest.php index e6550646..6a9c1b22 100644 --- a/tests/Predis/Configuration/Option/AggregateTest.php +++ b/tests/Predis/Configuration/Option/AggregateTest.php @@ -260,7 +260,7 @@ class AggregateTest extends PredisTestCase */ public function ___AggregateConnectionSkipCreationOnConnectionInstance(): void { - list(, $connectionClass) = $this->getMockConnectionClass(); + [, $connectionClass] = $this->getMockConnectionClass(); /** @var ClusterInterface|MockObject */ $cluster = $this->getMockBuilder('Predis\Connection\Cluster\ClusterInterface')->getMock(); @@ -285,7 +285,7 @@ class AggregateTest extends PredisTestCase */ public function ___AggregateConnectionWithMixedParameters(): void { - list(, $connectionClass) = $this->getMockConnectionClass(); + [, $connectionClass] = $this->getMockConnectionClass(); /** @var ClusterInterface|MockObject */ $cluster = $this->getMockBuilder('Predis\Connection\Cluster\ClusterInterface')->getMock(); diff --git a/tests/Predis/Connection/FactoryTest.php b/tests/Predis/Connection/FactoryTest.php index 6d431234..8e1f2950 100644 --- a/tests/Predis/Connection/FactoryTest.php +++ b/tests/Predis/Connection/FactoryTest.php @@ -439,7 +439,7 @@ class FactoryTest extends PredisTestCase */ public function testDefineConnectionWithFQN(): void { - list(, $connectionClass) = $this->getMockConnectionClass(); + [, $connectionClass] = $this->getMockConnectionClass(); $parameters = new Parameters(['scheme' => 'foobar']); $factory = new Factory(); @@ -455,7 +455,7 @@ class FactoryTest extends PredisTestCase */ public function testDefineConnectionWithCallable(): void { - list(, $connectionClass) = $this->getMockConnectionClass(); + [, $connectionClass] = $this->getMockConnectionClass(); $parameters = new Parameters(['scheme' => 'foobar']); $factory = new Factory(); @@ -527,7 +527,7 @@ class FactoryTest extends PredisTestCase $this->expectException('InvalidArgumentException'); $this->expectExceptionMessage("Unknown connection scheme: 'test'"); - list(, $connectionClass) = $this->getMockConnectionClass(); + [, $connectionClass] = $this->getMockConnectionClass(); $factory = new Factory(); diff --git a/tests/Predis/Connection/ParametersTest.php b/tests/Predis/Connection/ParametersTest.php index 560d0586..700ec4a4 100644 --- a/tests/Predis/Connection/ParametersTest.php +++ b/tests/Predis/Connection/ParametersTest.php @@ -415,9 +415,9 @@ class ParametersTest extends PredisTestCase { $defaults = $this->getDefaultParametersArray(); - $scheme = isset($parameters['scheme']) ? $parameters['scheme'] : $defaults['scheme']; - $host = isset($parameters['host']) ? $parameters['host'] : $defaults['host']; - $port = isset($parameters['port']) ? $parameters['port'] : $defaults['port']; + $scheme = $parameters['scheme'] ?? $defaults['scheme']; + $host = $parameters['host'] ?? $defaults['host']; + $port = $parameters['port'] ?? $defaults['port']; unset($parameters['scheme'], $parameters['host'], $parameters['port']); $uriString = "$scheme://$host:$port/?"; diff --git a/tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php b/tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php index 9c1e9e7b..a8249484 100644 --- a/tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php +++ b/tests/Predis/Connection/Replication/MasterSlaveReplicationTest.php @@ -989,7 +989,7 @@ class MasterSlaveReplicationTest extends PredisTestCase $replication ->getReplicationStrategy() ->setCommandReadOnly('exists', function ($cmd) { - list($arg1) = $cmd->getArguments(); + [$arg1] = $cmd->getArguments(); return $arg1 === 'foo'; }); diff --git a/tests/Predis/Pipeline/PipelineTest.php b/tests/Predis/Pipeline/PipelineTest.php index 445f6082..74ded602 100644 --- a/tests/Predis/Pipeline/PipelineTest.php +++ b/tests/Predis/Pipeline/PipelineTest.php @@ -541,7 +541,7 @@ class PipelineTest extends PredisTestCase throw new InvalidArgumentException("Expected ECHO, got {$id}"); } - list($echoed) = $command->getArguments(); + [$echoed] = $command->getArguments(); return $echoed; }; diff --git a/tests/Predis/Transaction/MultiExecTest.php b/tests/Predis/Transaction/MultiExecTest.php index 9e8f5990..95cac4a4 100644 --- a/tests/Predis/Transaction/MultiExecTest.php +++ b/tests/Predis/Transaction/MultiExecTest.php @@ -940,7 +940,7 @@ class MultiExecTest extends PredisTestCase return true; case 'ECHO': - @list($trigger) = $command->getArguments(); + @[$trigger] = $command->getArguments(); if (strpos($trigger, 'ERR ') === 0) { throw new Response\ServerException($trigger); }