1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the humbug/php-scoper package. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
9
|
|
|
* Pádraic Brady <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper\NodeVisitor\FunctionStmt; |
16
|
|
|
|
17
|
|
|
use PhpParser\Node; |
18
|
|
|
use PhpParser\Node\Expr\FuncCall; |
19
|
|
|
use PhpParser\Node\Name; |
20
|
|
|
use PhpParser\Node\Scalar\String_; |
21
|
|
|
use PhpParser\NodeVisitorAbstract; |
22
|
|
|
|
23
|
|
|
final class ScopeFunctionCallArgumentsStmtNodeVisitor extends NodeVisitorAbstract |
24
|
|
|
{ |
25
|
|
|
private $prefix; |
26
|
|
|
private $whitelist; |
27
|
|
|
private $functions; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $prefix |
31
|
|
|
* @param string[] $whitelist |
32
|
|
|
* @param string[] $functions Functions which first parameter should be prefixed. |
33
|
|
|
*/ |
34
|
|
|
public function __construct(string $prefix, array $whitelist, array $functions) |
35
|
|
|
{ |
36
|
|
|
$this->prefix = $prefix; |
37
|
|
|
$this->whitelist = $whitelist; |
38
|
|
|
$this->functions = $functions; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function enterNode(Node $node): Node |
45
|
|
|
{ |
46
|
|
|
if (!$node instanceof FuncCall || null === $node->name) { |
47
|
|
|
return $node; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!$node->name instanceof Name) { |
51
|
|
|
return $node; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!in_array($node->name->getFirst(), $this->functions)) { |
55
|
|
|
return $node; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$value = $node->args[0]->value; |
59
|
|
|
if (!$value instanceof String_) { |
60
|
|
|
return $node; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$stringValue = ltrim($value->value, '\\'); |
64
|
|
|
|
65
|
|
|
// Do not prefix whitelisted class methods |
66
|
|
|
if (in_array($stringValue, $this->whitelist)) { |
67
|
|
|
return $node; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Do not prefix function calls belonging to the global namespace |
71
|
|
|
if (false !== strstr($stringValue, '\\')) { |
72
|
|
|
$value->value = ($value->value !== $stringValue ? '\\' : '').$this->prefix.'\\'.$stringValue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $node; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|