From 2e065f483609e7a6450f5b6414313c4048e34cdf Mon Sep 17 00:00:00 2001 From: Anthony Clark Date: Fri, 2 Jun 2023 08:59:24 -0700 Subject: [PATCH] Filter out internal PHP functions from set (#109) * Filter out internal PHP functions --- ...vel-array-str-functions-to-static-call.php | 93 ++++++++++--------- phpstan.neon | 4 - src/NodeFactory/ModelFactoryNodeFactory.php | 2 +- .../AddGenericReturnTypeToRelationsRector.php | 6 +- .../MigrateToSimplifiedAttributeRector.php | 3 +- ...pertyDeferToDeferrableProviderToRector.php | 3 +- ...menRoutesStringMiddlewareToArrayRector.php | 4 +- ...placeFakerInstanceWithHelperRectorTest.php | 2 +- 8 files changed, 61 insertions(+), 56 deletions(-) diff --git a/config/sets/laravel-array-str-functions-to-static-call.php b/config/sets/laravel-array-str-functions-to-static-call.php index fabc4d62..a1fbb3a1 100644 --- a/config/sets/laravel-array-str-functions-to-static-call.php +++ b/config/sets/laravel-array-str-functions-to-static-call.php @@ -12,51 +12,58 @@ return static function (RectorConfig $rectorConfig): void { $rectorConfig->import(__DIR__ . '/../config.php'); + + $internalFunctions = get_defined_functions()['internal']; + $rectorConfig ->ruleWithConfiguration( FuncCallToStaticCallRector::class, - [new FuncCallToStaticCall('array_add', 'Illuminate\Support\Arr', 'add'), - new FuncCallToStaticCall('array_collapse', 'Illuminate\Support\Arr', 'collapse'), - new FuncCallToStaticCall('array_divide', 'Illuminate\Support\Arr', 'divide'), - new FuncCallToStaticCall('array_dot', 'Illuminate\Support\Arr', 'dot'), - new FuncCallToStaticCall('array_except', 'Illuminate\Support\Arr', 'except'), - new FuncCallToStaticCall('array_first', 'Illuminate\Support\Arr', 'first'), - new FuncCallToStaticCall('array_flatten', 'Illuminate\Support\Arr', 'flatten'), - new FuncCallToStaticCall('array_forget', 'Illuminate\Support\Arr', 'forget'), - new FuncCallToStaticCall('array_get', 'Illuminate\Support\Arr', 'get'), - new FuncCallToStaticCall('array_has', 'Illuminate\Support\Arr', 'has'), - new FuncCallToStaticCall('array_last', 'Illuminate\Support\Arr', 'last'), - new FuncCallToStaticCall('array_only', 'Illuminate\Support\Arr', 'only'), - new FuncCallToStaticCall('array_pluck', 'Illuminate\Support\Arr', 'pluck'), - new FuncCallToStaticCall('array_prepend', 'Illuminate\Support\Arr', 'prepend'), - new FuncCallToStaticCall('array_pull', 'Illuminate\Support\Arr', 'pull'), - new FuncCallToStaticCall('array_random', 'Illuminate\Support\Arr', 'random'), - new FuncCallToStaticCall('array_sort', 'Illuminate\Support\Arr', 'sort'), - new FuncCallToStaticCall('array_sort_recursive', 'Illuminate\Support\Arr', 'sortRecursive'), - new FuncCallToStaticCall('array_where', 'Illuminate\Support\Arr', 'where'), - new FuncCallToStaticCall('array_wrap', 'Illuminate\Support\Arr', 'wrap'), - new FuncCallToStaticCall('array_set', 'Illuminate\Support\Arr', 'set'), - new FuncCallToStaticCall('camel_case', 'Illuminate\Support\Str', 'camel'), - new FuncCallToStaticCall('ends_with', 'Illuminate\Support\Str', 'endsWith'), - new FuncCallToStaticCall('kebab_case', 'Illuminate\Support\Str', 'kebab'), - new FuncCallToStaticCall('snake_case', 'Illuminate\Support\Str', 'snake'), - new FuncCallToStaticCall('starts_with', 'Illuminate\Support\Str', 'startsWith'), - new FuncCallToStaticCall('str_after', 'Illuminate\Support\Str', 'after'), - new FuncCallToStaticCall('str_before', 'Illuminate\Support\Str', 'before'), - new FuncCallToStaticCall('str_contains', 'Illuminate\Support\Str', 'contains'), - new FuncCallToStaticCall('str_finish', 'Illuminate\Support\Str', 'finish'), - new FuncCallToStaticCall('str_is', 'Illuminate\Support\Str', 'is'), - new FuncCallToStaticCall('str_limit', 'Illuminate\Support\Str', 'limit'), - new FuncCallToStaticCall('str_plural', 'Illuminate\Support\Str', 'plural'), - new FuncCallToStaticCall('str_random', 'Illuminate\Support\Str', 'random'), - new FuncCallToStaticCall('str_replace_array', 'Illuminate\Support\Str', 'replaceArray'), - new FuncCallToStaticCall('str_replace_first', 'Illuminate\Support\Str', 'replaceFirst'), - new FuncCallToStaticCall('str_replace_last', 'Illuminate\Support\Str', 'replaceLast'), - new FuncCallToStaticCall('str_singular', 'Illuminate\Support\Str', 'singular'), - new FuncCallToStaticCall('str_slug', 'Illuminate\Support\Str', 'slug'), - new FuncCallToStaticCall('str_start', 'Illuminate\Support\Str', 'start'), - new FuncCallToStaticCall('studly_case', 'Illuminate\Support\Str', 'studly'), - new FuncCallToStaticCall('title_case', 'Illuminate\Support\Str', 'title'), - ] + array_filter( + [ + new FuncCallToStaticCall('array_add', 'Illuminate\Support\Arr', 'add'), + new FuncCallToStaticCall('array_collapse', 'Illuminate\Support\Arr', 'collapse'), + new FuncCallToStaticCall('array_divide', 'Illuminate\Support\Arr', 'divide'), + new FuncCallToStaticCall('array_dot', 'Illuminate\Support\Arr', 'dot'), + new FuncCallToStaticCall('array_except', 'Illuminate\Support\Arr', 'except'), + new FuncCallToStaticCall('array_first', 'Illuminate\Support\Arr', 'first'), + new FuncCallToStaticCall('array_flatten', 'Illuminate\Support\Arr', 'flatten'), + new FuncCallToStaticCall('array_forget', 'Illuminate\Support\Arr', 'forget'), + new FuncCallToStaticCall('array_get', 'Illuminate\Support\Arr', 'get'), + new FuncCallToStaticCall('array_has', 'Illuminate\Support\Arr', 'has'), + new FuncCallToStaticCall('array_last', 'Illuminate\Support\Arr', 'last'), + new FuncCallToStaticCall('array_only', 'Illuminate\Support\Arr', 'only'), + new FuncCallToStaticCall('array_pluck', 'Illuminate\Support\Arr', 'pluck'), + new FuncCallToStaticCall('array_prepend', 'Illuminate\Support\Arr', 'prepend'), + new FuncCallToStaticCall('array_pull', 'Illuminate\Support\Arr', 'pull'), + new FuncCallToStaticCall('array_random', 'Illuminate\Support\Arr', 'random'), + new FuncCallToStaticCall('array_sort', 'Illuminate\Support\Arr', 'sort'), + new FuncCallToStaticCall('array_sort_recursive', 'Illuminate\Support\Arr', 'sortRecursive'), + new FuncCallToStaticCall('array_where', 'Illuminate\Support\Arr', 'where'), + new FuncCallToStaticCall('array_wrap', 'Illuminate\Support\Arr', 'wrap'), + new FuncCallToStaticCall('array_set', 'Illuminate\Support\Arr', 'set'), + new FuncCallToStaticCall('camel_case', 'Illuminate\Support\Str', 'camel'), + new FuncCallToStaticCall('ends_with', 'Illuminate\Support\Str', 'endsWith'), + new FuncCallToStaticCall('kebab_case', 'Illuminate\Support\Str', 'kebab'), + new FuncCallToStaticCall('snake_case', 'Illuminate\Support\Str', 'snake'), + new FuncCallToStaticCall('starts_with', 'Illuminate\Support\Str', 'startsWith'), + new FuncCallToStaticCall('str_after', 'Illuminate\Support\Str', 'after'), + new FuncCallToStaticCall('str_before', 'Illuminate\Support\Str', 'before'), + new FuncCallToStaticCall('str_contains', 'Illuminate\Support\Str', 'contains'), + new FuncCallToStaticCall('str_finish', 'Illuminate\Support\Str', 'finish'), + new FuncCallToStaticCall('str_is', 'Illuminate\Support\Str', 'is'), + new FuncCallToStaticCall('str_limit', 'Illuminate\Support\Str', 'limit'), + new FuncCallToStaticCall('str_plural', 'Illuminate\Support\Str', 'plural'), + new FuncCallToStaticCall('str_random', 'Illuminate\Support\Str', 'random'), + new FuncCallToStaticCall('str_replace_array', 'Illuminate\Support\Str', 'replaceArray'), + new FuncCallToStaticCall('str_replace_first', 'Illuminate\Support\Str', 'replaceFirst'), + new FuncCallToStaticCall('str_replace_last', 'Illuminate\Support\Str', 'replaceLast'), + new FuncCallToStaticCall('str_singular', 'Illuminate\Support\Str', 'singular'), + new FuncCallToStaticCall('str_slug', 'Illuminate\Support\Str', 'slug'), + new FuncCallToStaticCall('str_start', 'Illuminate\Support\Str', 'start'), + new FuncCallToStaticCall('studly_case', 'Illuminate\Support\Str', 'studly'), + new FuncCallToStaticCall('title_case', 'Illuminate\Support\Str', 'title'), + ], + fn ($function) => ! in_array($function->getOldFuncName(), $internalFunctions, true) + ) ); }; diff --git a/phpstan.neon b/phpstan.neon index 20db6682..372e80eb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -27,7 +27,3 @@ parameters: - '#Parameter \#1 \$node \(PhpParser\\Node\\(.*?) of method RectorLaravel\\(.*?)\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method Rector\\Core\\Contract\\Rector\\PhpRectorInterface\:\:refactor\(\)#' - '#Parameter \#1 \$className of method Rector\\Core\\Reflection\\ReflectionResolver\:\:resolveMethodReflection\(\) expects class\-string, string given#' - - - - message: '#Do not use chained method calls\. Put each on separated lines#' - path: config/* diff --git a/src/NodeFactory/ModelFactoryNodeFactory.php b/src/NodeFactory/ModelFactoryNodeFactory.php index be484402..4b10c243 100644 --- a/src/NodeFactory/ModelFactoryNodeFactory.php +++ b/src/NodeFactory/ModelFactoryNodeFactory.php @@ -125,7 +125,7 @@ function (Node $node) use ($closure, $name): ?Return_ { return null; } - if ($node->expr === null) { + if (! $node->expr instanceof Expr) { return null; } diff --git a/src/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector.php b/src/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector.php index a3350346..da3ac1a2 100644 --- a/src/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector.php +++ b/src/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector.php @@ -125,7 +125,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node // Don't update an existing return type if it differs from the native return type (thus the one without generics). // E.g. we only add generics to an existing return type, but don't change the type itself. if ( - $phpDocInfo->getReturnTagValue() !== null + $phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode && ! $this->areNativeTypeAndPhpDocReturnTypeEqual( $node, $methodReturnType, @@ -151,7 +151,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node // Don't update the docblock if return type already contains the correct generics. This avoids overwriting // non-FQCN with our fully qualified ones. if ( - $phpDocInfo->getReturnTagValue() !== null + $phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode && $this->areGenericTypesEqual( $node, $phpDocInfo->getReturnTagValue(), @@ -168,7 +168,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node ); // Update or add return tag - if ($phpDocInfo->getReturnTagValue() !== null) { + if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) { $phpDocInfo->getReturnTagValue() ->type = $genericTypeNode; } else { diff --git a/src/Rector/ClassMethod/MigrateToSimplifiedAttributeRector.php b/src/Rector/ClassMethod/MigrateToSimplifiedAttributeRector.php index 408c32e6..40d997c7 100644 --- a/src/Rector/ClassMethod/MigrateToSimplifiedAttributeRector.php +++ b/src/Rector/ClassMethod/MigrateToSimplifiedAttributeRector.php @@ -4,6 +4,7 @@ namespace RectorLaravel\Rector\ClassMethod; +use PhpParser\Comment\Doc; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Expr\Array_; @@ -96,7 +97,7 @@ public function refactor(Node $node): ?Node // Preserve docblock $docComment = $node->getDocComment(); - if ($docComment !== null) { + if ($docComment instanceof Doc) { $newNode->setDocComment($docComment); } diff --git a/src/Rector/Class_/PropertyDeferToDeferrableProviderToRector.php b/src/Rector/Class_/PropertyDeferToDeferrableProviderToRector.php index 17f2c878..21609bee 100644 --- a/src/Rector/Class_/PropertyDeferToDeferrableProviderToRector.php +++ b/src/Rector/Class_/PropertyDeferToDeferrableProviderToRector.php @@ -5,6 +5,7 @@ namespace RectorLaravel\Rector\Class_; use PhpParser\Node; +use PhpParser\Node\Expr; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; @@ -88,7 +89,7 @@ private function matchDeferWithFalseProperty(Class_ $class): ?Property } $onlyProperty = $property->props[0]; - if ($onlyProperty->default === null) { + if (! $onlyProperty->default instanceof Expr) { return null; } diff --git a/src/Rector/MethodCall/LumenRoutesStringMiddlewareToArrayRector.php b/src/Rector/MethodCall/LumenRoutesStringMiddlewareToArrayRector.php index a6ed6d87..f8dac4ec 100644 --- a/src/Rector/MethodCall/LumenRoutesStringMiddlewareToArrayRector.php +++ b/src/Rector/MethodCall/LumenRoutesStringMiddlewareToArrayRector.php @@ -115,7 +115,7 @@ private function getRouterMethodAttributes(MethodCall $methodCall): ?Array_ private function findItemInArrayByKey(Array_ $array, string $keyName): ?ArrayItem { foreach ($array->items as $i => $item) { - if ($item === null) { + if (! $item instanceof ArrayItem) { continue; } if (! $this->arrayManipulator->hasKeyName($item, $keyName)) { @@ -133,7 +133,7 @@ private function findItemInArrayByKey(Array_ $array, string $keyName): ?ArrayIte private function replaceItemInArrayByKey(Array_ $array, ArrayItem $arrayItem, string $keyName): void { foreach ($array->items as $i => $item) { - if ($item === null) { + if (! $item instanceof ArrayItem) { continue; } if (! $this->arrayManipulator->hasKeyName($item, $keyName)) { diff --git a/tests/Rector/PropertyFetch/ReplaceFakerInstanceWithHelperRector/ReplaceFakerInstanceWithHelperRectorTest.php b/tests/Rector/PropertyFetch/ReplaceFakerInstanceWithHelperRector/ReplaceFakerInstanceWithHelperRectorTest.php index d98eb7cb..d3cfa4c6 100644 --- a/tests/Rector/PropertyFetch/ReplaceFakerInstanceWithHelperRector/ReplaceFakerInstanceWithHelperRectorTest.php +++ b/tests/Rector/PropertyFetch/ReplaceFakerInstanceWithHelperRector/ReplaceFakerInstanceWithHelperRectorTest.php @@ -16,7 +16,7 @@ public function test(string $filePath): void $this->doTestFile($filePath); } - public function provideData(): Iterator + public static function provideData(): Iterator { return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); }