Skip to content

Commit

Permalink
Merge pull request #65 from stof/phpstan
Browse files Browse the repository at this point in the history
Add static analysis with phpstan
  • Loading branch information
stof committed Mar 27, 2024
2 parents 50043b2 + fb28c54 commit 31f0f09
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
"autoload-dev": {
"psr-4": {
"Prophecy\\PhpUnit\\Tests\\Fixtures\\": "fixtures",
"Prophecy\\PhpUnit\\Tests\\PhpstanFixtures\\": "phpstan-fixtures",
"Prophecy\\PhpUnit\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
},
"require-dev": {
"phpstan/phpstan": "^1.10"
}
}
35 changes: 35 additions & 0 deletions phpstan-fixtures/ExampleUsage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Prophecy\PhpUnit\Tests\PhpstanFixtures;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;

class ExampleUsage extends TestCase
{
use ProphecyTrait;

public function testExplicitClass(): void
{
$prophecy = $this->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);
}
}
11 changes: 11 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/ProphecyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 31f0f09

Please sign in to comment.