-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathPregReplaceCallbackArgVisitor.php
More file actions
53 lines (42 loc) · 1.42 KB
/
PregReplaceCallbackArgVisitor.php
File metadata and controls
53 lines (42 loc) · 1.42 KB
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
<?php declare(strict_types = 1);
namespace PHPStan\Parser;
use Override;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use PHPStan\DependencyInjection\AutowiredService;
#[AutowiredService]
final class PregReplaceCallbackArgVisitor extends NodeVisitorAbstract
{
public const ATTRIBUTE_NAME = 'pregReplaceCallbackFlags';
#[Override]
public function enterNode(Node $node): ?Node
{
if (!$node instanceof Node\Expr\FuncCall || !$node->name instanceof Node\Name || $node->isFirstClassCallable()) {
return null;
}
$functionName = $node->name->toLowerString();
if ($functionName === 'preg_replace_callback') {
$args = $node->getArgs();
if (isset($args[1]) && isset($args[5])) {
$args[1]->setAttribute(self::ATTRIBUTE_NAME, $args[5]->value);
}
} elseif ($functionName === 'preg_replace_callback_array') {
$args = $node->getArgs();
if (!isset($args[0]) || !isset($args[4])) {
return null;
}
$args[0]->setAttribute(self::ATTRIBUTE_NAME, $args[4]->value);
// Also set the attribute on closures/arrow functions inside the array values
$arrayArg = $args[0]->value;
if ($arrayArg instanceof Node\Expr\Array_) {
foreach ($arrayArg->items as $item) {
if (!($item->value instanceof Node\Expr\Closure) && !($item->value instanceof Node\Expr\ArrowFunction)) {
continue;
}
$item->value->setAttribute(self::ATTRIBUTE_NAME, $args[4]->value);
}
}
}
return null;
}
}