1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Funivan\PhpTokenizer\Pattern\Patterns; |
6
|
|
|
|
7
|
|
|
use Funivan\PhpTokenizer\Pattern\PatternMatcher; |
8
|
|
|
use Funivan\PhpTokenizer\Query\Query; |
9
|
|
|
use Funivan\PhpTokenizer\QuerySequence\QuerySequence; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class FunctionCallPattern implements PatternInterface { |
15
|
|
|
|
16
|
|
|
const OUTPUT_FULL = 1; |
17
|
|
|
|
18
|
|
|
const OUTPUT_ARGUMENTS = 2; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Query|null |
22
|
|
|
*/ |
23
|
|
|
private $nameQuery; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ParametersPattern |
27
|
|
|
*/ |
28
|
|
|
private $parametersPattern; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $outputType = self::OUTPUT_FULL; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
3 |
|
public function outputFull() : self { |
40
|
3 |
|
$this->outputType = self::OUTPUT_FULL; |
41
|
3 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
3 |
|
public function outputArguments() : self { |
49
|
3 |
|
$this->outputType = self::OUTPUT_ARGUMENTS; |
50
|
3 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Query $query |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
3 |
|
public function withName(Query $query) : self { |
59
|
3 |
|
$this->nameQuery = $query; |
60
|
3 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ParametersPattern $pattern |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
3 |
|
public function withParameters(ParametersPattern $pattern) : self { |
69
|
3 |
|
$this->parametersPattern = $pattern; |
70
|
3 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
12 |
|
public function __invoke(QuerySequence $querySequence) { |
78
|
|
|
|
79
|
12 |
|
$name = $querySequence->strict(T_STRING); |
80
|
12 |
|
if ($this->nameQuery !== null and $this->nameQuery->isValid($name) === false) { |
81
|
3 |
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
12 |
|
$querySequence->possible(T_WHITESPACE); |
85
|
12 |
|
$arguments = $querySequence->section('(', ')'); |
86
|
|
|
|
87
|
12 |
|
if (!$querySequence->isValid()) { |
88
|
12 |
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
12 |
|
$querySequence->moveToToken($name); |
92
|
12 |
|
$before = $querySequence->move(-1); |
93
|
12 |
|
if ($before->getType() === T_WHITESPACE) { |
94
|
12 |
|
$before = $querySequence->move(-1); |
95
|
|
|
} |
96
|
|
|
|
97
|
12 |
|
if (in_array($before->getValue(), ['::', 'function', '->'])) { |
98
|
3 |
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
12 |
|
if ($this->parametersPattern !== null) { |
102
|
3 |
|
$pattern = (new PatternMatcher($arguments))->apply($this->parametersPattern); |
103
|
3 |
|
if (count($pattern->getCollections()) === 0) { |
104
|
3 |
|
return null; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
12 |
|
$lastToken = $arguments->getLast(); |
109
|
12 |
|
if ($lastToken === null) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
12 |
|
if ($this->outputType === self::OUTPUT_ARGUMENTS) { |
114
|
3 |
|
return $arguments; |
115
|
|
|
} |
116
|
|
|
|
117
|
9 |
|
return $querySequence->getCollection()->extractByTokens($name, $lastToken); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |