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

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:

-
name: 'Rector'
if: ${{ false }} # Temporarily disabled as it is not compatible with PHPStan 2.0
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 revert this, rector should run now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverted a6be6a1 d8fb908

run: composer rector --ansi

-
Expand All @@ -34,6 +35,7 @@ jobs:

-
name: 'Check Active Classes'
if: ${{ false }} # Temporarily disabled as it is not compatible with PHPStan 2.0
run: vendor/bin/class-leak check src --ansi

name: ${{ matrix.actions.name }}
Expand Down
16 changes: 4 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@
"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
Contributor 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"
"rector/rector": "dev-main as 1.2.10",
"phpstan/extension-installer": "^1.4"
},
"autoload": {
"psr-4": {
Expand All @@ -36,10 +32,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 +50,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,9 +23,6 @@ public function getNodeType(): string
return Node::class;
}

/**
* @return string[]|RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
if ($this->shouldSkipNode($node)) {
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 @@ final class AnnotateRegexClassConstWithRegexLinkRule implements Rule, Documented
*/
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 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->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

}

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)->build()];
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ final class CheckRequiredInterfaceInContractNamespaceRule implements Rule, Docum
*/
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 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->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

}

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 @@ public function __construct(
$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 Down Expand Up @@ -117,7 +116,7 @@ class SomeCommand extends Command
}

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

$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 @@ final class ForbiddenArrayMethodCallRule implements Rule, DocumentedRuleInterfac
*/
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 @@ -58,7 +56,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->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

}

public function getRuleDefinition(): RuleDefinition
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 @@ public function getNodeType(): string

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

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->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

}
}
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 @@ final class SomeException extends Exception
]);
}

/**
* @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 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [self::ERROR_MESSAGE];
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)->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, basically, add unique identifier on message error

}
}
Loading