diff --git a/phpstan.neon b/phpstan.neon index 654ce0db3ec..2346be283eb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,10 +1,6 @@ includes: - vendor/symplify/phpstan-rules/config/symplify-rules.neon -rules: - # @todo this rule is costly and designed for hand-on refactoring; enable only when needed - #- Rector\Utils\PHPStan\Rule\LongAndDependentComplexRectorRule - parameters: level: 8 diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule.php index 5f9ed1a8552..d1132dea29c 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule.php +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule.php @@ -3,8 +3,8 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\ValueObject\PhpVersionFeature; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector; +use Rector\ValueObject\PhpVersionFeature; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rule(ReturnUnionTypeRector::class); diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule_true_in_union.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule_true_in_union.php index 68597338d92..3870cf3c697 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule_true_in_union.php +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector/config/configured_rule_true_in_union.php @@ -3,8 +3,8 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\ValueObject\PhpVersionFeature; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector; +use Rector\ValueObject\PhpVersionFeature; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rule(ReturnUnionTypeRector::class); diff --git a/utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php b/utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php deleted file mode 100644 index a3139c80cff..00000000000 --- a/utils/PHPStan/Rule/LongAndDependentComplexRectorRule.php +++ /dev/null @@ -1,132 +0,0 @@ - - * This rule helps to find overly complex rules, that usually have little value, but are costrly to run. - */ -final readonly class LongAndDependentComplexRectorRule implements Rule -{ - /** - * @var int - */ - private const ALLOWED_TRANSITIONAL_COMPLEXITY = 140; - - private Parser $phpParser; - - private NodeFinder $nodeFinder; - - public function __construct( - private AstCognitiveComplexityAnalyzer $astCognitiveComplexityAnalyzer, - ) { - $parserFactory = new ParserFactory(); - $this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7); - - $this->nodeFinder = new NodeFinder(); - } - - public function getNodeType(): string - { - return InClassNode::class; - } - - /** - * @param InClassNode $node - */ - public function processNode(Node $node, Scope $scope): array - { - // check only rector rules - $classReflection = $node->getClassReflection(); - if (! $classReflection->isSubclassOf(RectorInterface::class)) { - return []; - } - - // not much complex - if (! $classReflection->hasConstructor()) { - return []; - } - - $extendedMethodReflection = $classReflection->getConstructor(); - $parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle( - $extendedMethodReflection->getVariants() - ); - - $originalClassLike = $node->getOriginalNode(); - if (! $originalClassLike instanceof Class_) { - return []; - } - - $currentClassLikeComplexity = $this->astCognitiveComplexityAnalyzer->analyzeClassLike($originalClassLike); - $totalTransitionalComplexity = $currentClassLikeComplexity; - - foreach ($parametersAcceptorWithPhpDocs->getParameters() as $parameterReflectionWithPhpDoc) { - $parameterType = $parameterReflectionWithPhpDoc->getType(); - if (! $parameterType instanceof TypeWithClassName) { - continue; - } - - $parameterClassReflection = $parameterType->getClassReflection(); - if (! $parameterClassReflection instanceof ClassReflection) { - continue; - } - - $dependencyClass = $this->parseClassReflectionToClassNode($parameterClassReflection); - if (! $dependencyClass instanceof Class_) { - continue; - } - - $dependencyComplexity = $this->astCognitiveComplexityAnalyzer->analyzeClassLike($dependencyClass); - $totalTransitionalComplexity += $dependencyComplexity; - } - - if ($totalTransitionalComplexity < self::ALLOWED_TRANSITIONAL_COMPLEXITY) { - return []; - } - - return [sprintf( - 'Transitional dependency complexity %d is over %d, please consider splitting it up.', - $totalTransitionalComplexity, - self::ALLOWED_TRANSITIONAL_COMPLEXITY - )]; - } - - private function parseClassReflectionToClassNode(ClassReflection $classReflection): ?Class_ - { - $fileName = $classReflection->getFileName(); - if (! is_string($fileName)) { - return null; - } - - $fileContents = FileSystem::read($fileName); - - $stmts = $this->phpParser->parse($fileContents); - if ($stmts === null) { - return null; - } - - $foundNode = $this->nodeFinder->findFirstInstanceOf($stmts, Class_::class); - if (! $foundNode instanceof Class_) { - return null; - } - - return $foundNode; - } -}