Skip to content

Commit

Permalink
Filter out internal PHP functions from set (#109)
Browse files Browse the repository at this point in the history
* Filter out internal PHP functions
  • Loading branch information
driftingly authored Jun 2, 2023
1 parent 02c1eb7 commit 2e065f4
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 56 deletions.
93 changes: 50 additions & 43 deletions config/sets/laravel-array-str-functions-to-static-call.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
};
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
2 changes: 1 addition & 1 deletion src/NodeFactory/ModelFactoryNodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function (Node $node) use ($closure, $name): ?Return_ {
return null;
}

if ($node->expr === null) {
if (! $node->expr instanceof Expr) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace RectorLaravel\Rector\ClassMethod;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down

0 comments on commit 2e065f4

Please sign in to comment.