From fb28c54a9d95af51547fee33ee2dd47010598681 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 13 Mar 2024 14:16:16 +0100 Subject: [PATCH] Add static analysis with phpstan --- .gitattributes | 2 ++ .github/workflows/ci.yml | 15 +++++++++++++ composer.json | 4 ++++ phpstan-fixtures/ExampleUsage.php | 35 +++++++++++++++++++++++++++++++ phpstan.dist.neon | 11 ++++++++++ src/ProphecyTrait.php | 1 + 6 files changed, 68 insertions(+) create mode 100644 phpstan-fixtures/ExampleUsage.php create mode 100644 phpstan.dist.neon diff --git a/.gitattributes b/.gitattributes index 9091cc7..3b7ebf0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,5 +4,7 @@ /.github/ export-ignore /CONTRIBUTING.md export-ignore /fixtures/ export-ignore +/phpstan-fixtures/ export-ignore +/phpstan.dist.neon export-ignore /phpunit.xml.dist export-ignore /tests/ export-ignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed3020b..1182293 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,3 +64,18 @@ jobs: - name: Execute Unit Tests run: vendor/bin/phpunit + + phpstan: + runs-on: ubuntu-latest + name: Static analysis + steps: + - uses: actions/checkout@v4 + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.2" + ini-file: development + coverage: none + - name: Install dependencies + run: composer update --ansi --no-interaction --no-progress + - run: ./vendor/bin/phpstan diff --git a/composer.json b/composer.json index a27bf91..a8e0cbd 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "autoload-dev": { "psr-4": { "Prophecy\\PhpUnit\\Tests\\Fixtures\\": "fixtures", + "Prophecy\\PhpUnit\\Tests\\PhpstanFixtures\\": "phpstan-fixtures", "Prophecy\\PhpUnit\\Tests\\": "tests" } }, @@ -31,5 +32,8 @@ "branch-alias": { "dev-master": "2.x-dev" } + }, + "require-dev": { + "phpstan/phpstan": "^1.10" } } diff --git a/phpstan-fixtures/ExampleUsage.php b/phpstan-fixtures/ExampleUsage.php new file mode 100644 index 0000000..93c71a2 --- /dev/null +++ b/phpstan-fixtures/ExampleUsage.php @@ -0,0 +1,35 @@ +prophesize(\DateTimeImmutable::class); + + $this->configureDouble($prophecy); + + $double = $prophecy->reveal(); + + $this->checkValue($double->format('Y-m-d')); + } + + /** + * @param ObjectProphecy<\DateTimeImmutable> $double + */ + private function configureDouble(ObjectProphecy $double): void + { + } + + private function checkValue(string $value): void + { + self::assertNotEmpty($value); + } +} diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000..28633dc --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,11 @@ +parameters: + level: 9 + treatPhpDocTypesAsCertain: false + paths: + - ./src/ + - ./phpstan-fixtures/ + ignoreErrors: + # recordDoubledType exist only in PHPUnit < 10 but the code is checking that before using it. + - + message: '#^Call to an undefined method Prophecy\\PhpUnit\\Tests\\PhpstanFixtures\\[^:]++\:\:recordDoubledType\(\)\.$#' + path: src/ProphecyTrait.php diff --git a/src/ProphecyTrait.php b/src/ProphecyTrait.php index db1676f..31812cb 100644 --- a/src/ProphecyTrait.php +++ b/src/ProphecyTrait.php @@ -98,6 +98,7 @@ protected function tearDownProphecy(): void private function countProphecyAssertions(): void { \assert($this instanceof TestCase); + \assert($this->prophet !== null); $this->prophecyAssertionsCounted = true; foreach ($this->prophet->getProphecies() as $objectProphecy) {