Skip to content

Require PHPStan 2.0 #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
],
"require": {
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^1.12.5"
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"editorconfig-checker/editorconfig-checker": "^10.6.0",
"ergebnis/composer-normalize": "^2.28",
"nette/neon": "^3.3.1",
"phpstan/phpstan-deprecation-rules": "^1.2",
"phpstan/phpstan-phpunit": "^1.4.0",
"phpstan/phpstan-strict-rules": "^1.6.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^9.5.20",
"shipmonk/composer-dependency-analyser": "^1.3.0",
"shipmonk/dead-code-detector": "^0.2.1",
"shipmonk/name-collision-detector": "^2.0.0",
"slevomat/coding-standard": "^8.0.1"
},
Expand Down
191 changes: 61 additions & 130 deletions composer.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ includes:
- ./vendor/phpstan/phpstan-phpunit/extension.neon
- ./vendor/phpstan/phpstan-phpunit/rules.neon
- ./vendor/phpstan/phpstan-deprecation-rules/rules.neon
- ./vendor/shipmonk/dead-code-detector/rules.neon
- ./rules.neon

parameters:
Expand Down
2 changes: 2 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ services:
allowedCheckedExceptionCallables: %shipmonkRules.forbidCheckedExceptionInCallable.allowedCheckedExceptionCallables%
-
class: ShipMonk\PHPStan\Rule\ForbidCheckedExceptionInYieldingMethodRule
arguments:
exceptionTypeResolver: @PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver
-
class: ShipMonk\PHPStan\Rule\ForbidCustomFunctionsRule
arguments:
Expand Down
5 changes: 0 additions & 5 deletions src/Rule/EnforceListReturnRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\VerbosityLevel;
use function count;

Expand All @@ -31,10 +30,6 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
if (AccessoryArrayListType::isListTypeEnabled() === false) {
return [];
}

$methodReflection = $scope->getFunction();

if ($methodReflection === null || $node instanceof ClosureReturnStatementsNode) {
Expand Down
8 changes: 6 additions & 2 deletions src/Rule/EnforceNativeReturnTypehintRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Generator;
use PhpParser\Node;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ReturnStatementsNode;
use PHPStan\Php\PhpVersion;
Expand Down Expand Up @@ -328,7 +329,10 @@ private function alwaysThrowsException(ReturnStatementsNode $node): bool
$exitPoints = $node->getStatementResult()->getExitPoints();

foreach ($exitPoints as $exitPoint) {
if (!$exitPoint->getStatement() instanceof Throw_) {
$statement = $exitPoint->getStatement();
$isThrow = $statement instanceof Expression && $statement->expr instanceof Throw_;

if (!$isThrow) {
return false;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidCheckedExceptionInCallableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
use PHPStan\Node\FunctionCallableNode;
use PHPStan\Node\MethodCallableNode;
use PHPStan\Node\StaticMethodCallableNode;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParameterReflectionWithPhpDocs;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver;
Expand Down Expand Up @@ -239,7 +239,7 @@ public function processArrowFunction(
$result = $this->nodeScopeResolver->processExprNode(
new Expression($node->expr),
$node->expr,
$scope->enterArrowFunction($node),
$scope->enterArrowFunction($node, null),
static function (): void {
},
ExpressionContext::createDeep(),
Expand Down Expand Up @@ -354,7 +354,7 @@ private function checkClassExistence(
*/
private function isImmediatelyInvokedCallable(object $reflection, ?ParameterReflection $parameter): bool
{
if ($parameter instanceof ParameterReflectionWithPhpDocs) {
if ($parameter instanceof ExtendedParameterReflection) {
$parameterCallImmediately = $parameter->isImmediatelyInvokedCallable();

if ($parameterCallImmediately->maybe()) {
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidCheckedExceptionInYieldingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver;
use PHPStan\Rules\Exceptions\ExceptionTypeResolver;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -16,9 +16,9 @@
class ForbidCheckedExceptionInYieldingMethodRule implements Rule
{

private DefaultExceptionTypeResolver $exceptionTypeResolver;
private ExceptionTypeResolver $exceptionTypeResolver;

public function __construct(DefaultExceptionTypeResolver $exceptionTypeResolver)
public function __construct(ExceptionTypeResolver $exceptionTypeResolver)
{
$this->exceptionTypeResolver = $exceptionTypeResolver;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidFetchOnMixedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Identifier;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -25,11 +25,11 @@
class ForbidFetchOnMixedRule implements Rule
{

private Standard $printer;
private Printer $printer;

private bool $checkExplicitMixed;

public function __construct(Standard $printer, bool $checkExplicitMixed)
public function __construct(Printer $printer, bool $checkExplicitMixed)
{
$this->printer = $printer;
$this->checkExplicitMixed = $checkExplicitMixed;
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidMethodCallOnMixedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -24,11 +24,11 @@
class ForbidMethodCallOnMixedRule implements Rule
{

private Standard $printer;
private Printer $printer;

private bool $checkExplicitMixed;

public function __construct(Standard $printer, bool $checkExplicitMixed)
public function __construct(Printer $printer, bool $checkExplicitMixed)
{
$this->printer = $printer;
$this->checkExplicitMixed = $checkExplicitMixed;
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidNotNormalizedTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType;
use PhpParser\PrettyPrinter\Standard as PhpParserPrinter;
use PHPStan\Analyser\NameScope;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
use PHPStan\PhpDoc\TypeNodeResolver;
use PHPStan\PhpDocParser\Ast\Node as PhpDocRootNode;
Expand Down Expand Up @@ -57,7 +57,7 @@ class ForbidNotNormalizedTypeRule implements Rule

private TypeNodeResolver $typeNodeResolver;

private PhpParserPrinter $phpParserPrinter;
private Printer $phpParserPrinter;

private bool $checkDisjunctiveNormalForm;

Expand All @@ -69,7 +69,7 @@ class ForbidNotNormalizedTypeRule implements Rule
public function __construct(
FileTypeMapper $fileTypeMapper,
TypeNodeResolver $typeNodeResolver,
PhpParserPrinter $phpParserPrinter,
Printer $phpParserPrinter,
bool $checkDisjunctiveNormalForm
)
{
Expand Down
18 changes: 9 additions & 9 deletions src/Rule/ForbidNullInInterpolatedStringRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,43 @@
namespace ShipMonk\PHPStan\Rule;

use PhpParser\Node;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\PrettyPrinter\Standard;
use PhpParser\Node\InterpolatedStringPart;
use PhpParser\Node\Scalar\InterpolatedString;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\TypeCombinator;

/**
* @implements Rule<Encapsed>
* @implements Rule<InterpolatedString>
*/
class ForbidNullInInterpolatedStringRule implements Rule
{

private Standard $printer;
private Printer $printer;

public function __construct(Standard $printer)
public function __construct(Printer $printer)
{
$this->printer = $printer;
}

public function getNodeType(): string
{
return Encapsed::class;
return InterpolatedString::class;
}

/**
* @param Encapsed $node
* @param InterpolatedString $node
* @return list<IdentifierRuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
$errors = [];

foreach ($node->parts as $part) {
if ($part instanceof EncapsedStringPart) {
if ($part instanceof InterpolatedStringPart) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Rule/ForbidProtectedEnumMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public function processNode(Node $node, Scope $scope): array

foreach ($node->getMethods() as $classMethod) {
if (
$classMethod->isProtected()
$classMethod->getNode()->isProtected()
&& !$classMethod->isDeclaredInTrait()
) {
$errors[] = RuleErrorBuilder::message('Protected methods within enum makes no sense as you cannot extend them anyway.')
->line($classMethod->getStartLine())
->line($classMethod->getNode()->getStartLine())
->identifier('shipmonk.protectedEnumMethod')
->build();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/ForbidUnsafeArrayKeyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace ShipMonk\PHPStan\Rule;

use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrayItem;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
Expand Down
6 changes: 3 additions & 3 deletions src/Rule/ForbidUnusedExceptionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand All @@ -22,9 +22,9 @@
class ForbidUnusedExceptionRule implements Rule
{

private Standard $printer;
private Printer $printer;

public function __construct(Standard $printer)
public function __construct(Printer $printer)
{
$this->printer = $printer;
}
Expand Down
20 changes: 12 additions & 8 deletions src/Rule/RequirePreviousExceptionPassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Printer\Printer;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\IdentifierRuleError;
Expand All @@ -35,11 +36,14 @@
class RequirePreviousExceptionPassRule implements Rule
{

private Standard $printer;
private Printer $printer;

private bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne;

public function __construct(Standard $printer, bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne = false)
public function __construct(
Printer $printer,
bool $reportEvenIfExceptionIsNotAcceptableByRethrownOne = false
)
{
$this->printer = $printer;
$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne = $reportEvenIfExceptionIsNotAcceptableByRethrownOne;
Expand Down Expand Up @@ -70,12 +74,12 @@ public function processNode(Node $node, Scope $scope): array
}

foreach ($catch->stmts as $statement) {
if (!$statement instanceof Throw_) {
if ($statement instanceof Expression && $statement->expr instanceof Throw_) {
$throwExpression = $statement->expr->expr;
} else {
continue;
}

$throwExpression = $statement->expr;

if ($throwExpression instanceof CallLike) {
$errors = array_merge(
$errors,
Expand Down Expand Up @@ -152,7 +156,7 @@ private function processExceptionCreation(
}

/**
* @return ParameterReflection[]
* @return list<ParameterReflection>
*/
private function getCallLikeParameters(CallLike $node, Scope $scope): array
{
Expand Down
Loading
Loading