forked from symplify/phpstan-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoNestedFuncCallRule.php
97 lines (84 loc) · 2.37 KB
/
NoNestedFuncCallRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
declare(strict_types=1);
namespace Symplify\PHPStanRules\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\NodeFinder;
use PHPStan\Analyser\Scope;
use Symplify\Astral\Naming\SimpleNameResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\PHPStanRules\Tests\Rules\NoNestedFuncCallRule\NoNestedFuncCallRuleTest
*/
final class NoNestedFuncCallRule extends AbstractSymplifyRule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Use separate function calls with readable variable names';
/**
* @var string[]
*/
private const ALLOWED_FUNC_NAMES = [
'count',
'trim',
'ltrim',
'rtrim',
'get_class',
'implode',
'strlen',
'substr',
'getcwd',
'is_file',
'file_exists',
'in_array',
'str_starts_with',
];
public function __construct(
private SimpleNameResolver $simpleNameResolver,
private NodeFinder $nodeFinder
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
* @return string[]
*/
public function process(Node $node, Scope $scope): array
{
$rootFuncCallName = $this->simpleNameResolver->getName($node);
$allowedNames = array_diff(self::ALLOWED_FUNC_NAMES, [$rootFuncCallName]);
foreach ($node->args as $arg) {
$nestedFuncCalls = $this->nodeFinder->findInstanceOf($arg, FuncCall::class);
foreach ($nestedFuncCalls as $nestedFuncCall) {
if ($this->simpleNameResolver->isNames($nestedFuncCall, $allowedNames)) {
continue;
}
return [self::ERROR_MESSAGE];
}
}
return [];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
$filteredValues = array_filter(array_map($callback, $items));
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$mappedItems = array_map($callback, $items);
$filteredValues = array_filter($mappedItems);
CODE_SAMPLE
),
]);
}
}