Skip to content

Commit 89cdfaa

Browse files
committed
Fix missing and too-wide return types with --fix, add projectType
2 parents 6a460c5 + 15add59 commit 89cdfaa

127 files changed

Lines changed: 16650 additions & 306 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ lint:
2020
--exclude tests/PHPStan/Analyser/nsrt \
2121
--exclude tests/PHPStan/Rules/Methods/data \
2222
--exclude tests/PHPStan/Rules/Functions/data \
23+
--exclude tests/PHPStan/Rules/data/fix-missing-return-type \
24+
--exclude tests/PHPStan/Rules/TooWideTypehints/data/fix-too-wide-return-type \
2325
--exclude tests/PHPStan/Rules/Names/data \
2426
--exclude tests/PHPStan/Rules/Operators/data/invalid-inc-dec.php \
2527
--exclude tests/PHPStan/Rules/Arrays/data/offset-access-without-dim-for-reading.php \

‎conf/bleedingEdge.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ parameters:
1212
checkExtensionsForComparisonOperators: true
1313
checkGenericIterableClasses: true
1414
reportTooWideBool: true
15+
fixMissingReturnTypes: true
1516
rawMessageInBaseline: true
1617
reportNestedTooWideType: false # tmp
1718
assignToByRefForeachExpr: true

‎conf/config.neon‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ parameters:
7474
checkConditionalParameterTypes: false
7575
checkMixedWitnessTypes: false
7676
recursiveLoopTypes: false
77+
fixMissingReturnTypes: false
7778
fileExtensions:
7879
- php
7980
checkAdvancedIsset: false
@@ -97,6 +98,7 @@ parameters:
9798
checkPhpDocMethodSignatures: false
9899
checkExtraArguments: false
99100
checkMissingTypehints: false
101+
projectType: open
100102
checkTooWideParameterOutInProtectedAndPublicMethods: false
101103
checkTooWideReturnTypesInProtectedAndPublicMethods: false
102104
checkTooWideThrowTypesInProtectedAndPublicMethods: false

‎conf/parametersSchema.neon‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ parametersSchema:
7171
checkConditionalParameterTypes: bool()
7272
checkMixedWitnessTypes: bool()
7373
recursiveLoopTypes: bool()
74+
fixMissingReturnTypes: bool()
7475
])
7576
fileExtensions: listOf(string())
7677
checkAdvancedIsset: bool()
@@ -94,6 +95,7 @@ parametersSchema:
9495
checkPhpDocMethodSignatures: bool()
9596
checkExtraArguments: bool()
9697
checkMissingTypehints: bool()
98+
projectType: anyOf('open', 'closed')
9799
checkTooWideParameterOutInProtectedAndPublicMethods: bool()
98100
checkTooWideReturnTypesInProtectedAndPublicMethods: bool()
99101
checkTooWideThrowTypesInProtectedAndPublicMethods: bool()

‎e2e/fix-many-files/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/src
2+
/tmp
3+
/vendor
4+
/composer.lock

‎e2e/fix-many-files/composer.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"autoload-dev": {
3+
"classmap": [
4+
"rules/"
5+
]
6+
}
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
# @tags result-cache
3+
4+
function set_up() {
5+
e2e::set_up
6+
}
7+
8+
function tear_down() {
9+
e2e::tear_down
10+
}
11+
12+
# the files changed by the previous rounds do not fit on the command line of the next round
13+
function test_fix_many_files() {
14+
composer install
15+
rm -rf src tmp && mkdir -p src
16+
printf '<?php\n\nfunction step1(): void {}\nfunction step2(): void {}\nfunction step3(): void {}\n' > src/functions.php
17+
for i in $(seq 1 2000); do
18+
printf '<?php\n\nstep1();\n' > "src/a-file-with-a-name-long-enough-to-fill-the-command-line-of-the-next-round-$i.php"
19+
done
20+
OUTPUT=$(../../bin/phpstan analyse --fix 2>&1) && CODE=0 || CODE=$?
21+
echo "$OUTPUT" | tail -20
22+
assert_same "0" "$CODE"
23+
assert_contains 'Round 2: fixed 2000 errors in 2000 files' "$OUTPUT"
24+
assert_contains '[OK] 4000 errors fixed in 2000 files' "$OUTPUT"
25+
assert_same "step3();" "$(tail -1 src/a-file-with-a-name-long-enough-to-fill-the-command-line-of-the-next-round-1.php)"
26+
# the list of the changed files is removed after the rounds
27+
assert_same "" "$(find tmp -name 'fixed-files-*')"
28+
}

‎e2e/fix-many-files/phpstan.neon‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: 0
3+
paths:
4+
- src
5+
tmpDir: tmp
6+
7+
rules:
8+
- FixManyFilesE2E\StepRule
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace FixManyFilesE2E;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\FuncCall;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\Rule;
9+
use PHPStan\Rules\RuleErrorBuilder;
10+
11+
/**
12+
* Renames step1() to step2() and step2() to step3(), so every file changed in the first round is changed in the second one too.
13+
*
14+
* @implements Rule<FuncCall>
15+
*/
16+
final class StepRule implements Rule
17+
{
18+
19+
public function getNodeType(): string
20+
{
21+
return FuncCall::class;
22+
}
23+
24+
public function processNode(Node $node, Scope $scope): array
25+
{
26+
if (!$node->name instanceof Node\Name || preg_match('~^step([1-2])$~', $node->name->toString(), $matches) !== 1) {
27+
return [];
28+
}
29+
30+
$next = 'step' . ((int) $matches[1] + 1);
31+
32+
return [
33+
RuleErrorBuilder::message(sprintf('Call %s() instead.', $next))
34+
->identifier('fixManyFilesE2E.step')
35+
->fixNode($node, static function (FuncCall $node) use ($next): FuncCall {
36+
$node->name = new Node\Name($next);
37+
return $node;
38+
})
39+
->build(),
40+
];
41+
}
42+
43+
}

‎e2e/ignore-error-extension/src/ControllerActionReturnTypeIgnoreExtension.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Analyser\IgnoreErrorExtension;
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Node\InClassMethodNode;
12+
use PHPStan\Node\MethodReturnStatementsNode;
1213

1314
// This extension will ignore "missingType.iterableValue" errors for public Action methods inside Controller classes.
1415
final class ControllerActionReturnTypeIgnoreExtension implements IgnoreErrorExtension
@@ -19,8 +20,10 @@ public function shouldIgnore(Error $error, Node $node, Scope $scope) : bool
1920
return false;
2021
}
2122

22-
// @phpstan-ignore phpstanApi.instanceofAssumption
23-
if (! $node instanceof InClassMethodNode) {
23+
// Methods with a body report missing return types on MethodReturnStatementsNode,
24+
// abstract methods and methods in interfaces on InClassMethodNode.
25+
// @phpstan-ignore phpstanApi.instanceofAssumption, phpstanApi.instanceofAssumption
26+
if (! $node instanceof InClassMethodNode && ! $node instanceof MethodReturnStatementsNode) {
2427
return false;
2528
}
2629

0 commit comments

Comments
 (0)