From 5331d943fe44f95ebdd7a6603e69c7de9c41ed8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Sun, 22 Jan 2023 01:03:53 +0000 Subject: [PATCH] Upgrade static analysis to PHPStan level 2 (#1043) * Upgrade static analysis to PHPStan level 2 * Fix CS and CI * Fix coverage path * Start debugging * Stop debugging --- .gitattributes | 1 + .github/workflows/linters.yml | 3 + .github/workflows/tests.yml | 2 +- phpstan-tests.dist.neon | 15 +++++ phpstan.dist.neon | 59 ++++++++++++++----- tests/PHPUnit/PredisCommandTestCase.php | 2 +- .../Configuration/Option/ConnectionsTest.php | 13 ++-- 7 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 phpstan-tests.dist.neon diff --git a/.gitattributes b/.gitattributes index bab718b3..e20c2b8a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14,3 +14,4 @@ /VERSION export-ignore /phpunit.xml.dist export-ignore /phpstan.dist.neon export-ignore +/phpstan-tests.dist.neon export-ignore diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml index 25e72678..b6d481b0 100644 --- a/.github/workflows/linters.yml +++ b/.github/workflows/linters.yml @@ -82,6 +82,9 @@ jobs: - name: Perform static analysis run: composer run phpstan + - name: Perform static analysis on tests + run: composer run phpstan -- --configuration=phpstan-tests.dist.neon + coding_standards: name: Coding Standards needs: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f26de2b9..35b38266 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -64,7 +64,7 @@ jobs: - name: Run PHPUnit tests with coverage if: ${{ matrix.php == '8.1' && matrix.redis == '7' }} - run: vendor/bin/phpunit --verbose --coverage-clover build/logs/clover.xml --coverage-filter src + run: vendor/bin/phpunit --verbose --coverage-clover build/logs/clover.xml --coverage-filter ./src - name: Send coverage to Coveralls env: diff --git a/phpstan-tests.dist.neon b/phpstan-tests.dist.neon new file mode 100644 index 00000000..c6e8f162 --- /dev/null +++ b/phpstan-tests.dist.neon @@ -0,0 +1,15 @@ +parameters: + level: 0 + + paths: + - tests/ + + ignoreErrors: + # Uses func_get_args() + - message: "#^Static method Predis\\\\Command\\\\RawCommand\\:\\:create\\(\\) invoked with 0 parameters, at least 1 required\\.$#" + count: 1 + path: tests/Predis/Command/RawCommandTest.php + # Mockery + - message: "#^Call to an undefined method Predis\\\\Configuration\\\\Option\\\\AggregateTest\\:\\:getMockConnectionClass\\(\\)\\.$#" + count: 2 + path: tests/Predis/Configuration/Option/AggregateTest.php diff --git a/phpstan.dist.neon b/phpstan.dist.neon index c0603cff..f7bb09a5 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -1,25 +1,56 @@ parameters: - level: 0 + level: 2 paths: - - src - - tests + - src/ ignoreErrors: - - '#^Function phpiredis_\w+ not found\.$#' - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" + # Tricky ones + - message: "#^Cannot cast Predis\\\\Response\\\\ResponseInterface to string\\.$#" + count: 1 + path: src/Client.php + - message: "#^Cannot cast Predis\\\\Connection\\\\ConnectionInterface to string\\.$#" + count: 1 + path: src/Client.php + # phpiredis extension is not installed + - "#^Function phpiredis_\\w+ not found\\.$#" + - "#^Constant PHPIREDIS_#" + # "new static" in non-final classes + - message: "#^Unsafe usage of new static\\(\\)\\.$#" count: 3 path: src/Client.php - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" + - message: "#^Unsafe usage of new static\\(\\)\\.$#" count: 1 path: src/Connection/Parameters.php - - - message: "#^Static method Predis\\\\Command\\\\RawCommand\\:\\:create\\(\\) invoked with 0 parameters, at least 1 required\\.$#" + # Using an interface as concrete type + - message: "#^Call to an undefined method Predis\\\\Command\\\\FactoryInterface\\:\\:define\\(\\)\\.$#" count: 1 - path: tests/Predis/Command/RawCommandTest.php - - - message: "#^Call to an undefined method Predis\\\\Configuration\\\\Option\\\\AggregateTest\\:\\:getMockConnectionClass\\(\\)\\.$#" + path: src/Configuration/Option/Commands.php + - message: "#^Call to an undefined method Predis\\\\Command\\\\FactoryInterface\\:\\:undefine\\(\\)\\.$#" + count: 1 + path: src/Configuration/Option/Commands.php + - message: "#^Access to an undefined property Predis\\\\Configuration\\\\OptionsInterface\\:\\:\\$parameters\\.$#" + count: 1 + path: src/Configuration/Option/Connections.php + - message: "#^Access to an undefined property Predis\\\\Connection\\\\ParametersInterface\\:\\:\\$weight\\.$#" + count: 1 + path: src/Connection/Cluster/PredisCluster.php + - message: "#^Variable \\$response might not be defined\\.$#" count: 2 - path: tests/Predis/Configuration/Option/AggregateTest.php + path: src/Connection/Cluster/RedisCluster.php + - message: "#^Access to an undefined property Predis\\\\Connection\\\\ParametersInterface\\:\\:\\$role\\.$#" + count: 1 + path: src/Connection/Replication/MasterSlaveReplication.php + - message: "#^Access to an undefined property Predis\\\\Connection\\\\ParametersInterface\\:\\:\\$role\\.$#" + count: 1 + path: src/Connection/Replication/SentinelReplication.php + # try/catch problems + - message: "#^Variable \\$connection might not be defined\\.$#" + count: 1 + path: src/Connection/Replication/MasterSlaveReplication.php + - message: "#^Variable \\$response might not be defined\\.$#" + count: 1 + path: src/Connection/Replication/MasterSlaveReplication.php + - message: "#^Variable \\$response might not be defined\\.$#" + count: 1 + path: src/Connection/Replication/SentinelReplication.php diff --git a/tests/PHPUnit/PredisCommandTestCase.php b/tests/PHPUnit/PredisCommandTestCase.php index ac4f5dde..8e9fb270 100644 --- a/tests/PHPUnit/PredisCommandTestCase.php +++ b/tests/PHPUnit/PredisCommandTestCase.php @@ -79,7 +79,7 @@ abstract class PredisCommandTestCase extends PredisTestCase /** * Returns a new command instance with the specified arguments. * - * @param ... List of arguments for the command + * @param mixed ...$arguments List of arguments for the command * * @return CommandInterface */ diff --git a/tests/Predis/Configuration/Option/ConnectionsTest.php b/tests/Predis/Configuration/Option/ConnectionsTest.php index a58fec3d..fc46308b 100644 --- a/tests/Predis/Configuration/Option/ConnectionsTest.php +++ b/tests/Predis/Configuration/Option/ConnectionsTest.php @@ -12,7 +12,6 @@ namespace Predis\Configuration\Option; -use PHPUnit\Framework\MockObject\MockObject; use Predis\Configuration\OptionsInterface; use PredisTestCase; use stdClass; @@ -37,7 +36,7 @@ class ConnectionsTest extends PredisTestCase */ public function testAcceptsNamedArrayWithSchemeToConnectionClassMappings(): void { - /** @var OptionsInterface */ + /** @var \Predis\Configuration\OptionsInterface */ $options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock(); $class = get_class($this->getMockBuilder('Predis\Connection\NodeConnectionInterface')->getMock()); @@ -49,7 +48,7 @@ class ConnectionsTest extends PredisTestCase ->method('define') ->with($this->matchesRegularExpression('/^tcp|redis$/'), $class); - /** @var OptionInterface */ + /** @var \Predis\Configuration\OptionInterface */ $option = $this->getMockBuilder('Predis\Configuration\Option\Connections') ->onlyMethods(['getDefault']) ->getMock(); @@ -140,7 +139,7 @@ class ConnectionsTest extends PredisTestCase { $parameters = ['database' => 5, 'password' => 'mypassword']; - /** @var OptionsInterface|MockObject */ + /** @var \Predis\Configuration\OptionsInterface|\PHPUnit\Framework\MockObject\MockObject\MockObject */ $options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock(); $options ->expects($this->once()) @@ -164,7 +163,7 @@ class ConnectionsTest extends PredisTestCase */ public function testAcceptsConnectionFactoryInstance(): void { - /** @var OptionInterface */ + /** @var \Predis\Configuration\OptionInterface */ $option = $this->getMockBuilder('Predis\Configuration\Option\Connections') ->onlyMethods(['getDefault']) ->getMock(); @@ -185,7 +184,7 @@ class ConnectionsTest extends PredisTestCase { $option = new Connections(); - /** @var OptionsInterface */ + /** @var \Predis\Configuration\OptionsInterface */ $options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock(); $callable = $this->getMockBuilder('stdClass') @@ -212,7 +211,7 @@ class ConnectionsTest extends PredisTestCase $option = new Connections(); - /** @var OptionsInterface */ + /** @var \Predis\Configuration\OptionsInterface */ $options = $this->getMockBuilder('Predis\Configuration\OptionsInterface')->getMock(); $option->filter($options, new stdClass());