-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce safe constructor overrides with `@phpstan-consistent-construc…
…tor`
- Loading branch information
1 parent
317a997
commit 17d6b29
Showing
9 changed files
with
111 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\ExtendedMethodReflection; | ||
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection; | ||
use PHPStan\Rules\IdentifierRuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function sprintf; | ||
|
||
final class MethodVisibilityComparisonHelper | ||
{ | ||
|
||
/** @return list<IdentifierRuleError> */ | ||
public function compare(ExtendedMethodReflection $prototype, ClassReflection $prototypeDeclaringClass, PhpMethodFromParserNodeReflection $method): array | ||
{ | ||
/** @var list<IdentifierRuleError> $messages */ | ||
$messages = []; | ||
|
||
if ($prototype->isPublic()) { | ||
if (!$method->isPublic()) { | ||
$messages[] = RuleErrorBuilder::message(sprintf( | ||
'%s method %s::%s() overriding public method %s::%s() should also be public.', | ||
$method->isPrivate() ? 'Private' : 'Protected', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName(), | ||
$prototypeDeclaringClass->getDisplayName(true), | ||
$prototype->getName(), | ||
)) | ||
->nonIgnorable() | ||
->identifier('method.visibility') | ||
->build(); | ||
} | ||
} elseif ($method->isPrivate()) { | ||
$messages[] = RuleErrorBuilder::message(sprintf( | ||
'Private method %s::%s() overriding protected method %s::%s() should be protected or public.', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName(), | ||
$prototypeDeclaringClass->getDisplayName(true), | ||
$prototype->getName(), | ||
)) | ||
->nonIgnorable() | ||
->identifier('method.visibility') | ||
->build(); | ||
} | ||
|
||
return $messages; | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug12137; | ||
|
||
/** @phpstan-consistent-constructor */ | ||
abstract class ParentClass | ||
{ | ||
protected function __construct() | ||
{ | ||
} | ||
|
||
public static function create(): static | ||
{ | ||
return new static(); | ||
} | ||
} | ||
|
||
class ChildClass extends ParentClass | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
} |