Skip to content

Commit 31f0f09

Browse files
authored
Merge pull request #65 from stof/phpstan
Add static analysis with phpstan
2 parents 50043b2 + fb28c54 commit 31f0f09

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
/.github/ export-ignore
55
/CONTRIBUTING.md export-ignore
66
/fixtures/ export-ignore
7+
/phpstan-fixtures/ export-ignore
8+
/phpstan.dist.neon export-ignore
79
/phpunit.xml.dist export-ignore
810
/tests/ export-ignore

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,18 @@ jobs:
6464

6565
- name: Execute Unit Tests
6666
run: vendor/bin/phpunit
67+
68+
phpstan:
69+
runs-on: ubuntu-latest
70+
name: Static analysis
71+
steps:
72+
- uses: actions/checkout@v4
73+
- name: Set up PHP
74+
uses: shivammathur/setup-php@v2
75+
with:
76+
php-version: "8.2"
77+
ini-file: development
78+
coverage: none
79+
- name: Install dependencies
80+
run: composer update --ansi --no-interaction --no-progress
81+
- run: ./vendor/bin/phpstan

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
"autoload-dev": {
2525
"psr-4": {
2626
"Prophecy\\PhpUnit\\Tests\\Fixtures\\": "fixtures",
27+
"Prophecy\\PhpUnit\\Tests\\PhpstanFixtures\\": "phpstan-fixtures",
2728
"Prophecy\\PhpUnit\\Tests\\": "tests"
2829
}
2930
},
3031
"extra": {
3132
"branch-alias": {
3233
"dev-master": "2.x-dev"
3334
}
35+
},
36+
"require-dev": {
37+
"phpstan/phpstan": "^1.10"
3438
}
3539
}

phpstan-fixtures/ExampleUsage.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Prophecy\PhpUnit\Tests\PhpstanFixtures;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Prophecy\PhpUnit\ProphecyTrait;
7+
use Prophecy\Prophecy\ObjectProphecy;
8+
9+
class ExampleUsage extends TestCase
10+
{
11+
use ProphecyTrait;
12+
13+
public function testExplicitClass(): void
14+
{
15+
$prophecy = $this->prophesize(\DateTimeImmutable::class);
16+
17+
$this->configureDouble($prophecy);
18+
19+
$double = $prophecy->reveal();
20+
21+
$this->checkValue($double->format('Y-m-d'));
22+
}
23+
24+
/**
25+
* @param ObjectProphecy<\DateTimeImmutable> $double
26+
*/
27+
private function configureDouble(ObjectProphecy $double): void
28+
{
29+
}
30+
31+
private function checkValue(string $value): void
32+
{
33+
self::assertNotEmpty($value);
34+
}
35+
}

phpstan.dist.neon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
parameters:
2+
level: 9
3+
treatPhpDocTypesAsCertain: false
4+
paths:
5+
- ./src/
6+
- ./phpstan-fixtures/
7+
ignoreErrors:
8+
# recordDoubledType exist only in PHPUnit < 10 but the code is checking that before using it.
9+
-
10+
message: '#^Call to an undefined method Prophecy\\PhpUnit\\Tests\\PhpstanFixtures\\[^:]++\:\:recordDoubledType\(\)\.$#'
11+
path: src/ProphecyTrait.php

src/ProphecyTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ protected function tearDownProphecy(): void
9898
private function countProphecyAssertions(): void
9999
{
100100
\assert($this instanceof TestCase);
101+
\assert($this->prophet !== null);
101102
$this->prophecyAssertionsCounted = true;
102103

103104
foreach ($this->prophet->getProphecies() as $objectProphecy) {

0 commit comments

Comments
 (0)