Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static analysis with phpstan #65

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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