Skip to content

Commit 5f13e90

Browse files
committed
Add e2e test that demonstrates IgnoreErrorExtension
1 parent 6bcb27e commit 5f13e90

File tree

7 files changed

+116
-0
lines changed

7 files changed

+116
-0
lines changed

.github/workflows/e2e-tests.yml

+4
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ jobs:
243243
echo "$OUTPUT"
244244
../bashunit -a matches "Note: Using configuration file .+phpstan.neon." "$OUTPUT"
245245
../bashunit -a contains 'Result cache not used because the metadata do not match: metaExtensions' "$OUTPUT"
246+
- script: |
247+
cd e2e/ignore-error-extension
248+
composer install
249+
../../bin/phpstan
246250
247251
steps:
248252
- name: "Checkout"

e2e/ignore-error-extension/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/composer.lock
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"autoload": {
3+
"psr-4": {
4+
"App\\": "src/"
5+
}
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Method App\\HomepageController\:\:contactAction\(\) has parameter \$someUnrelatedError with no type specified\.$#'
5+
identifier: missingType.parameter
6+
count: 1
7+
path: src/HomepageController.php
8+
9+
-
10+
message: '#^Method App\\HomepageController\:\:getSomething\(\) return type has no value type specified in iterable type array\.$#'
11+
identifier: missingType.iterableValue
12+
count: 1
13+
path: src/HomepageController.php
14+
15+
-
16+
message: '#^Method App\\HomepageController\:\:homeAction\(\) has parameter \$someUnrelatedError with no type specified\.$#'
17+
identifier: missingType.parameter
18+
count: 1
19+
path: src/HomepageController.php
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
4+
parameters:
5+
level: 9
6+
paths:
7+
- src
8+
9+
services:
10+
-
11+
class: App\ControllerActionReturnTypeIgnoreExtension
12+
tags:
13+
- phpstan.ignoreErrorExtension
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
use PhpParser\Node;
8+
use PHPStan\Analyser\Error;
9+
use PHPStan\Analyser\IgnoreErrorExtension;
10+
use PHPStan\Analyser\Scope;
11+
use PHPStan\Node\InClassMethodNode;
12+
13+
// This extension will ignore "missingType.iterableValue" errors for public Action methods inside Controller classes.
14+
final class ControllerActionReturnTypeIgnoreExtension implements IgnoreErrorExtension
15+
{
16+
public function shouldIgnore(Error $error, Node $node, Scope $scope) : bool
17+
{
18+
if ($error->getIdentifier() !== 'missingType.iterableValue') {
19+
return false;
20+
}
21+
22+
// @phpstan-ignore phpstanApi.instanceofAssumption
23+
if (! $node instanceof InClassMethodNode) {
24+
return false;
25+
}
26+
27+
if (! str_ends_with($node->getClassReflection()->getName(), 'Controller')) {
28+
return false;
29+
}
30+
31+
if (! str_ends_with($node->getMethodReflection()->getName(), 'Action')) {
32+
return false;
33+
}
34+
35+
if (! $node->getMethodReflection()->isPublic()) {
36+
return false;
37+
}
38+
39+
return true;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
final class HomepageController
8+
{
9+
public function homeAction($someUnrelatedError = false): array
10+
{
11+
return [
12+
'title' => 'Homepage',
13+
'something' => $this->getSomething(),
14+
];
15+
}
16+
17+
public function contactAction($someUnrelatedError): array
18+
{
19+
return [
20+
'title' => 'Contact',
21+
'something' => $this->getSomething(),
22+
];
23+
}
24+
25+
private function getSomething(): array
26+
{
27+
return [];
28+
}
29+
}

0 commit comments

Comments
 (0)