-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TooWideMethodReturnTypehintRule - always report for final methods in …
…bleeding edge
- Loading branch information
1 parent
942afbf
commit c30e9a4
Showing
9 changed files
with
226 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
class TooWideMethodReturnTypehintRule implements Rule | ||
{ | ||
|
||
public function __construct(private bool $checkProtectedAndPublicMethods) | ||
public function __construct(private bool $checkProtectedAndPublicMethods, private bool $alwaysCheckFinal) | ||
{ | ||
} | ||
|
||
|
@@ -35,10 +35,19 @@ public function processNode(Node $node, Scope $scope): array | |
$method = $node->getMethodReflection(); | ||
$isFirstDeclaration = $method->getPrototype()->getDeclaringClass() === $method->getDeclaringClass(); | ||
if (!$method->isPrivate()) { | ||
if (!$this->checkProtectedAndPublicMethods) { | ||
if ($this->alwaysCheckFinal) { | ||
if (!$method->getDeclaringClass()->isFinal() && !$method->isFinal()->yes()) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ondrejmirtes
via email
Author
Member
|
||
if (!$this->checkProtectedAndPublicMethods) { | ||
return []; | ||
} | ||
|
||
if ($isFirstDeclaration) { | ||
return []; | ||
} | ||
} | ||
} elseif (!$this->checkProtectedAndPublicMethods) { | ||
return []; | ||
} | ||
if ($isFirstDeclaration && !$method->getDeclaringClass()->isFinal() && !$method->isFinal()->yes()) { | ||
} elseif ($isFirstDeclaration && !$method->getDeclaringClass()->isFinal() && !$method->isFinal()->yes()) { | ||
return []; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
tests/PHPStan/Rules/TooWideTypehints/data/method-too-wide-return-always-check-final.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace MethodTooWideReturnAlwaysCheckFinal; | ||
|
||
class Foo | ||
{ | ||
|
||
private function test(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
protected function test2(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
public function test3(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
} | ||
|
||
final class FinalFoo | ||
{ | ||
|
||
private function test(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
protected function test2(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
public function test3(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
} | ||
|
||
class FooFinalMethods | ||
{ | ||
|
||
private function test(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
final protected function test2(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
final public function test3(): ?int | ||
{ | ||
return 1; | ||
} | ||
|
||
} |
shouldn't be checked only if method is not implementation of interface?
https://phpstan.org/r/c83e6d0b-98d3-489a-a31f-268017c49474