Skip to content

Commit 7d74a63

Browse files
committed
[BE] TooWideMethodReturnTypehintRule - always report for final methods
1 parent 81aaff9 commit 7d74a63

File tree

7 files changed

+10
-79
lines changed

7 files changed

+10
-79
lines changed

changelog-2.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ Bleeding edge (TODO move to other sections)
7171
* Report narrowing `PHPStan\Type\Type` interface via `@var` (https://github.com/phpstan/phpstan-src/commit/713b98fb107213c28e3d8c8b4b43c5f5fc47c144), https://github.com/nunomaduro/larastan/issues/1567#issuecomment-1460445389
7272
* Check invalid PHPDocs in previously unchecked statement types (https://github.com/phpstan/phpstan-src/commit/9780d352f3264aac09ac7954f691de1877db8e01)
7373
* InvalidPHPStanDocTagRule in StubValidator (https://github.com/phpstan/phpstan-src/commit/9c2552b7e744926d1a74c1ba8fd32c64079eed61)
74-
* TooWideMethodReturnTypehintRule - always report for final methods (https://github.com/phpstan/phpstan-src/commit/c30e9a484c8245b8126cd63444607ca74d2af761)
7574
* Report unused results of `and` and `or` (https://github.com/phpstan/phpstan-src/commit/1d8fff637d70a9e9ed3f11dee5d61b9f796cbf1a)
7675
* Report unused result of ternary (https://github.com/phpstan/phpstan-src/commit/9664f7a9d2223c07e750f0dfc949c3accfa6b65e)
7776
* Report unused results of `&&` and `||` (https://github.com/phpstan/phpstan-src/commit/cf2c8bbd9ebd2ebe300dbd310e136ad603d7def3)
@@ -128,6 +127,7 @@ Improvements 🔧
128127
* Empty `skipCheckGenericClasses` (https://github.com/phpstan/phpstan-src/commit/28c2c79b16cac6ba6b01f1b4d211541dd49d8a77)
129128
* Report unnecessary nullsafe property fetch inside `??` / `isset` / `empty` with different message ([#1253](https://github.com/phpstan/phpstan-src/pull/1253)), thanks @rajyan!
130129
* Specify explicit mixed array type via `is_array` ([#1191](https://github.com/phpstan/phpstan-src/pull/1191)), thanks @herndlm!
130+
* TooWideMethodReturnTypehintRule - always report for final methods (https://github.com/phpstan/phpstan-src/commit/c30e9a484c8245b8126cd63444607ca74d2af761)
131131

132132
Bugfixes 🐛
133133
=====================

conf/bleedingEdge.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ parameters:
88
arrayValues: true
99
looseComparison: true
1010
readOnlyByPhpDoc: true
11-
alwaysCheckTooWideReturnTypeFinalMethods: true
1211
alwaysTrueAlwaysReported: true
1312
disableUnreachableBranchesRules: true
1413
pure: true

conf/config.level4.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ services:
282282
class: PHPStan\Rules\TooWideTypehints\TooWideMethodReturnTypehintRule
283283
arguments:
284284
checkProtectedAndPublicMethods: %checkTooWideReturnTypesInProtectedAndPublicMethods%
285-
alwaysCheckFinal: %featureToggles.alwaysCheckTooWideReturnTypeFinalMethods%
286285
tags:
287286
- phpstan.rules.rule
288287

conf/config.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ parameters:
2929
illegalConstructorMethodCall: false
3030
looseComparison: false
3131
readOnlyByPhpDoc: false
32-
alwaysCheckTooWideReturnTypeFinalMethods: false
3332
alwaysTrueAlwaysReported: false
3433
disableUnreachableBranchesRules: false
3534
stricterFunctionMap: false

conf/parametersSchema.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ parametersSchema:
3535
illegalConstructorMethodCall: bool(),
3636
looseComparison: bool(),
3737
readOnlyByPhpDoc: bool()
38-
alwaysCheckTooWideReturnTypeFinalMethods: bool()
3938
alwaysTrueAlwaysReported: bool()
4039
disableUnreachableBranchesRules: bool()
4140
stricterFunctionMap: bool()

src/Rules/TooWideTypehints/TooWideMethodReturnTypehintRule.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
final class TooWideMethodReturnTypehintRule implements Rule
2323
{
2424

25-
public function __construct(private bool $checkProtectedAndPublicMethods, private bool $alwaysCheckFinal)
25+
public function __construct(private bool $checkProtectedAndPublicMethods)
2626
{
2727
}
2828

@@ -39,20 +39,14 @@ public function processNode(Node $node, Scope $scope): array
3939
$method = $node->getMethodReflection();
4040
$isFirstDeclaration = $method->getPrototype()->getDeclaringClass() === $method->getDeclaringClass();
4141
if (!$method->isPrivate()) {
42-
if ($this->alwaysCheckFinal) {
43-
if (!$method->getDeclaringClass()->isFinal() && !$method->isFinal()->yes()) {
44-
if (!$this->checkProtectedAndPublicMethods) {
45-
return [];
46-
}
42+
if (!$method->getDeclaringClass()->isFinal() && !$method->isFinal()->yes()) {
43+
if (!$this->checkProtectedAndPublicMethods) {
44+
return [];
45+
}
4746

48-
if ($isFirstDeclaration) {
49-
return [];
50-
}
47+
if ($isFirstDeclaration) {
48+
return [];
5149
}
52-
} elseif (!$this->checkProtectedAndPublicMethods) {
53-
return [];
54-
} elseif ($isFirstDeclaration && !$method->getDeclaringClass()->isFinal() && !$method->isFinal()->yes()) {
55-
return [];
5650
}
5751
}
5852

tests/PHPStan/Rules/TooWideTypehints/TooWideMethodReturnTypehintRuleTest.php

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ class TooWideMethodReturnTypehintRuleTest extends RuleTestCase
1414

1515
private bool $checkProtectedAndPublicMethods = true;
1616

17-
private bool $alwaysCheckFinal = false;
18-
1917
protected function getRule(): Rule
2018
{
21-
return new TooWideMethodReturnTypehintRule($this->checkProtectedAndPublicMethods, $this->alwaysCheckFinal);
19+
return new TooWideMethodReturnTypehintRule($this->checkProtectedAndPublicMethods);
2220
}
2321

2422
public function testPrivate(): void
@@ -113,26 +111,6 @@ public function testBug6175(): void
113111
public function dataAlwaysCheckFinal(): iterable
114112
{
115113
yield [
116-
false,
117-
false,
118-
[
119-
[
120-
'Method MethodTooWideReturnAlwaysCheckFinal\Foo::test() never returns null so it can be removed from the return type.',
121-
8,
122-
],
123-
[
124-
'Method MethodTooWideReturnAlwaysCheckFinal\FinalFoo::test() never returns null so it can be removed from the return type.',
125-
28,
126-
],
127-
[
128-
'Method MethodTooWideReturnAlwaysCheckFinal\FooFinalMethods::test() never returns null so it can be removed from the return type.',
129-
48,
130-
],
131-
],
132-
];
133-
134-
yield [
135-
true,
136114
false,
137115
[
138116
[
@@ -167,42 +145,6 @@ public function dataAlwaysCheckFinal(): iterable
167145
];
168146

169147
yield [
170-
false,
171-
true,
172-
[
173-
[
174-
'Method MethodTooWideReturnAlwaysCheckFinal\Foo::test() never returns null so it can be removed from the return type.',
175-
8,
176-
],
177-
[
178-
'Method MethodTooWideReturnAlwaysCheckFinal\FinalFoo::test() never returns null so it can be removed from the return type.',
179-
28,
180-
],
181-
[
182-
'Method MethodTooWideReturnAlwaysCheckFinal\FinalFoo::test2() never returns null so it can be removed from the return type.',
183-
33,
184-
],
185-
[
186-
'Method MethodTooWideReturnAlwaysCheckFinal\FinalFoo::test3() never returns null so it can be removed from the return type.',
187-
38,
188-
],
189-
[
190-
'Method MethodTooWideReturnAlwaysCheckFinal\FooFinalMethods::test() never returns null so it can be removed from the return type.',
191-
48,
192-
],
193-
[
194-
'Method MethodTooWideReturnAlwaysCheckFinal\FooFinalMethods::test2() never returns null so it can be removed from the return type.',
195-
53,
196-
],
197-
[
198-
'Method MethodTooWideReturnAlwaysCheckFinal\FooFinalMethods::test3() never returns null so it can be removed from the return type.',
199-
58,
200-
],
201-
],
202-
];
203-
204-
yield [
205-
true,
206148
true,
207149
[
208150
[
@@ -241,10 +183,9 @@ public function dataAlwaysCheckFinal(): iterable
241183
* @dataProvider dataAlwaysCheckFinal
242184
* @param list<array{0: string, 1: int, 2?: string|null}> $expectedErrors
243185
*/
244-
public function testAlwaysCheckFinal(bool $checkProtectedAndPublicMethods, bool $alwaysCheckFinal, array $expectedErrors): void
186+
public function testAlwaysCheckFinal(bool $checkProtectedAndPublicMethods, array $expectedErrors): void
245187
{
246188
$this->checkProtectedAndPublicMethods = $checkProtectedAndPublicMethods;
247-
$this->alwaysCheckFinal = $alwaysCheckFinal;
248189
$this->analyse([__DIR__ . '/data/method-too-wide-return-always-check-final.php'], $expectedErrors);
249190
}
250191

0 commit comments

Comments
 (0)