Skip to content
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

Support PHPStan 2.0 #144

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 5 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@
"php": ">=8.1",
"nette/utils": "^3.2.9 || ^4.0",
"webmozart/assert": "^1.11",
"phpstan/phpstan": "^1.11",
"phpstan/phpstan": "^2.0",
"symplify/rule-doc-generator-contracts": "^11.2"
},
"require-dev": {
"nikic/php-parser": "^4.19",
"symplify/phpstan-extensions": "^11.4",
"symplify/rule-doc-generator": "^12.2",
"phpunit/phpunit": "^10.5",
"symfony/framework-bundle": "6.1.*",
"rector/rector": "^1.2.3",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use

        "rector/rector": "dev-main as 1.2.10",

to test rector for CI?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied it to f83aafa

"symplify/easy-coding-standard": "^12.3",
"phpstan/extension-installer": "^1.4",
"tomasvotruba/class-leak": "^0.2",
"tomasvotruba/unused-public": "^0.3.10",
"tomasvotruba/type-coverage": "^0.3.1"
"tomasvotruba/class-leak": "^1.2",
"rector/rector": "dev-main as 1.2.10",
"phpstan/extension-installer": "^1.4"
},
"autoload": {
"psr-4": {
Expand All @@ -36,10 +33,6 @@
"stubs"
],
"files": [
"tests/Rules/Rector/PhpUpgradeImplementsMinPhpVersionInterfaceRule/Fixture/SomePhpFeatureRector.php",
"vendor/rector/rector/src/Contract/Rector/RectorInterface.php",
"vendor/rector/rector/src/Contract/Rector/ConfigurableRectorInterface.php",
"vendor/rector/rector/vendor/rector/rector-downgrade-php/src/Set/ValueObject/DowngradeSetList.php"
]
},
"extra": {
Expand All @@ -58,7 +51,7 @@
"scripts": {
"check-cs": "vendor/bin/ecs check --ansi",
"fix-cs": "vendor/bin/ecs check --fix --ansi",
"phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify",
"phpstan": "vendor/bin/phpstan analyse --ansi",
"rector": "vendor/bin/rector process --dry-run --ansi",
"docs": "vendor/bin/rule-doc-generator generate src --readme --ansi"
}
Expand Down
18 changes: 9 additions & 9 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ parameters:
- */Fixture/*

# https://github.com/TomasVotruba/unused-public
unused_public:
methods: true
properties: true
constants: true

type_coverage:
return: 99
param: 99
property: 99
# unused_public:
# methods: true
# properties: true
# constants: true

# type_coverage:
# return: 99
# param: 99
# property: 99

ignoreErrors:
# needless generics
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/ReflectionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
private readonly TypeAwareNodeFinder $typeAwareNodeFinder
) {
$parserFactory = new ParserFactory();
$this->parser = $parserFactory->create(ParserFactory::PREFER_PHP7);
$this->parser = $parserFactory->createForNewestSupportedVersion();
}

public function parseMethodReflection(ReflectionMethod|MethodReflection $reflectionMethod): ?ClassMethod
Expand Down
1 change: 0 additions & 1 deletion src/ReturnTypeExtension/NodeGetAttributeTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
Expand Down
4 changes: 0 additions & 4 deletions src/Rules/AbstractSymplifyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use Symplify\PHPStanRules\Contract\ManyNodeRuleInterface;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;

Expand All @@ -24,16 +23,13 @@
return Node::class;
}

/**
* @return string[]|RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
if ($this->shouldSkipNode($node)) {
return [];
}

return $this->process($node, $scope);

Check failure on line 32 in src/Rules/AbstractSymplifyRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Symplify\PHPStanRules\Rules\AbstractSymplifyRule::processNode() should return list<PHPStan\Rules\IdentifierRuleError> but returns array<PHPStan\Rules\RuleError|string>.
}

private function shouldSkipNode(Node $node): bool
Expand Down
8 changes: 3 additions & 5 deletions src/Rules/AnnotateRegexClassConstWithRegexLinkRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use PhpParser\Node\Stmt\ClassConst;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<ClassConst>
* @see \Symplify\PHPStanRules\Tests\Rules\AnnotateRegexClassConstWithRegexLinkRule\AnnotateRegexClassConstWithRegexLinkRuleTest
*/
final class AnnotateRegexClassConstWithRegexLinkRule implements Rule, DocumentedRuleInterface
Expand All @@ -31,17 +33,13 @@
*/
private const ALL_MODIFIERS = 'imsxeADSUXJu';

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return ClassConst::class;
}

/**
* @param ClassConst $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand Down Expand Up @@ -69,7 +67,7 @@
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];

Check failure on line 70 in src/Rules/AnnotateRegexClassConstWithRegexLinkRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Symplify\PHPStanRules\Rules\AnnotateRegexClassConstWithRegexLinkRule::processNode() should return list<PHPStan\Rules\IdentifierRuleError> but returns array{PHPStan\Rules\RuleError}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add identifier here as well

}

public function getRuleDefinition(): RuleDefinition
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/CheckClassNamespaceFollowPsr4Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Composer\ClassNamespaceMatcher;
use Symplify\PHPStanRules\Composer\ComposerAutoloadResolver;
use Symplify\PHPStanRules\Composer\Psr4PathValidator;
Expand All @@ -17,6 +18,7 @@
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<ClassLike>
* @see \Symplify\PHPStanRules\Tests\Rules\CheckClassNamespaceFollowPsr4Rule\CheckClassNamespaceFollowPsr4RuleTest
*/
final class CheckClassNamespaceFollowPsr4Rule implements Rule, DocumentedRuleInterface
Expand Down Expand Up @@ -49,7 +51,6 @@ public function getNodeType(): string

/**
* @param ClassLike $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand Down Expand Up @@ -87,7 +88,7 @@ public function processNode(Node $node, Scope $scope): array
$namespacePart = substr($namespaceBeforeClass, 0, -1);
$errorMessage = sprintf(self::ERROR_MESSAGE, $namespacePart);

return [$errorMessage];
return [RuleErrorBuilder::message($errorMessage)->identifier('check.classnamespacepsr4')->build()];
}

public function getRuleDefinition(): RuleDefinition
Expand Down
7 changes: 2 additions & 5 deletions src/Rules/CheckRequiredInterfaceInContractNamespaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -30,17 +31,13 @@
*/
private const A_CONTRACT_NAMESPACE_REGEX = '#\bContracts?\b#';

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return Interface_::class;
}

/**
* @param Interface_ $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -53,7 +50,7 @@
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];

Check failure on line 53 in src/Rules/CheckRequiredInterfaceInContractNamespaceRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Symplify\PHPStanRules\Rules\CheckRequiredInterfaceInContractNamespaceRule::processNode() should return list<PHPStan\Rules\IdentifierRuleError> but returns array{PHPStan\Rules\RuleError}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add identifier here as well

}

public function getRuleDefinition(): RuleDefinition
Expand Down
11 changes: 5 additions & 6 deletions src/Rules/ClassNameRespectsParentSuffixRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use PHPStan\Node\InClassNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPUnit\Framework\TestCase;
use Rector\Rector\AbstractRector;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand All @@ -23,6 +25,7 @@
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<InClassNode>
* @see \Symplify\PHPStanRules\Tests\Rules\ClassNameRespectsParentSuffixRule\ClassNameRespectsParentSuffixRuleTest
*/
final class ClassNameRespectsParentSuffixRule implements Rule, DocumentedRuleInterface, ConfigurableRuleInterface
Expand Down Expand Up @@ -62,17 +65,13 @@
$this->parentClasses = array_merge($parentClasses, self::DEFAULT_PARENT_CLASSES);
}

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return InClassNode::class;
}

/**
* @param InClassNode $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -90,7 +89,7 @@
return [];
}

return $this->processClassNameAndShort($classReflection);

Check failure on line 92 in src/Rules/ClassNameRespectsParentSuffixRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Symplify\PHPStanRules\Rules\ClassNameRespectsParentSuffixRule::processNode() should return list<PHPStan\Rules\IdentifierRuleError> but returns list<PHPStan\Rules\RuleError>.
}

public function getRuleDefinition(): RuleDefinition
Expand All @@ -117,7 +116,7 @@
}

/**
* @return array<int, string>
* @return list<RuleError>
*/
private function processClassNameAndShort(ClassReflection $classReflection): array
{
Expand All @@ -132,7 +131,7 @@
}

$errorMessage = sprintf(self::ERROR_MESSAGE, $expectedSuffix);
return [$errorMessage];
return [RuleErrorBuilder::message($errorMessage)->build()];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add identifier here as well

}

return [];
Expand Down
8 changes: 3 additions & 5 deletions src/Rules/Complexity/ForbiddenArrayMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use PhpParser\Node\Expr\ArrayItem;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\TypeWithClassName;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<Array_>
* @see \Symplify\PHPStanRules\Tests\Rules\Complexity\ForbiddenArrayMethodCallRule\ForbiddenArrayMethodCallRuleTest
*/
final class ForbiddenArrayMethodCallRule implements Rule, DocumentedRuleInterface
Expand All @@ -25,17 +27,13 @@
*/
public const ERROR_MESSAGE = 'Array method calls [$this, "method"] are not allowed. Use explicit method instead to help PhpStorm, PHPStan and Rector understand your code';

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return Array_::class;
}

/**
* @param Array_ $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -44,7 +42,7 @@
}

$typeWithClassName = $this->resolveFirstArrayItemClassType($node, $scope);
if (! $typeWithClassName instanceof TypeWithClassName) {

Check failure on line 45 in src/Rules/Complexity/ForbiddenArrayMethodCallRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Doing instanceof PHPStan\Type\TypeWithClassName is error-prone and deprecated. Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.
return [];
}

Expand All @@ -58,7 +56,7 @@
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];

Check failure on line 59 in src/Rules/Complexity/ForbiddenArrayMethodCallRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Symplify\PHPStanRules\Rules\Complexity\ForbiddenArrayMethodCallRule::processNode() should return list<PHPStan\Rules\IdentifierRuleError> but returns array{PHPStan\Rules\RuleError}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add identifier here as well

}

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -86,7 +84,7 @@
}

$firstItemType = $scope->getType($firstItem->value);
if (! $firstItemType instanceof TypeWithClassName) {

Check failure on line 87 in src/Rules/Complexity/ForbiddenArrayMethodCallRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Doing instanceof PHPStan\Type\TypeWithClassName is error-prone and deprecated. Use Type::getObjectClassNames() or Type::getObjectClassReflections() instead.
return null;
}

Expand All @@ -103,7 +101,7 @@
$secondItemValue = $secondItem->value;

$secondItemType = $scope->getType($secondItemValue);
if (! $secondItemType instanceof ConstantStringType) {

Check failure on line 104 in src/Rules/Complexity/ForbiddenArrayMethodCallRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.
return null;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Rules/Domain/RequireAttributeNamespaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<InClassNode>
* @see \Symplify\PHPStanRules\Tests\Rules\Domain\RequireAttributeNamespaceRule\RequireAttributeNamespaceRuleTest
*/
final class RequireAttributeNamespaceRule implements Rule, DocumentedRuleInterface
Expand Down Expand Up @@ -59,7 +61,6 @@

/**
* @param InClassNode $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -74,6 +75,6 @@
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];

Check failure on line 78 in src/Rules/Domain/RequireAttributeNamespaceRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Symplify\PHPStanRules\Rules\Domain\RequireAttributeNamespaceRule::processNode() should return list<PHPStan\Rules\IdentifierRuleError> but returns array{PHPStan\Rules\RuleError}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add identifier here as well

}
}
8 changes: 3 additions & 5 deletions src/Rules/Domain/RequireExceptionNamespaceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @implements Rule<InClassNode>
* @see \Symplify\PHPStanRules\Tests\Rules\Domain\RequireExceptionNamespaceRule\RequireExceptionNamespaceRuleTest
*/
final class RequireExceptionNamespaceRule implements Rule, DocumentedRuleInterface
Expand Down Expand Up @@ -48,17 +50,13 @@
]);
}

/**
* @return class-string<Node>
*/
public function getNodeType(): string
{
return InClassNode::class;
}

/**
* @param InClassNode $node
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -77,6 +75,6 @@
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->build()];

Check failure on line 78 in src/Rules/Domain/RequireExceptionNamespaceRule.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Symplify\PHPStanRules\Rules\Domain\RequireExceptionNamespaceRule::processNode() should return list<PHPStan\Rules\IdentifierRuleError> but returns array{PHPStan\Rules\RuleError}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add identifier here as well, basically, add unique identifier on message error

}
}
Loading
Loading