1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\PhpTokenizer\Pattern\Patterns; |
4
|
|
|
|
5
|
|
|
use Funivan\PhpTokenizer\QuerySequence\QuerySequence; |
6
|
|
|
use Funivan\PhpTokenizer\Strategy\Possible; |
7
|
|
|
use Funivan\PhpTokenizer\Strategy\QueryStrategy; |
8
|
|
|
use Funivan\PhpTokenizer\Strategy\Strict; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Pattern used to finding classes in tour source code |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class ClassPattern implements PatternInterface { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Result of this pattern will be body of the class |
18
|
|
|
*/ |
19
|
|
|
const OUTPUT_BODY = 1; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Result of this pattern will be full class |
23
|
|
|
*/ |
24
|
|
|
const OUTPUT_FULL = 2; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var QueryStrategy |
29
|
|
|
*/ |
30
|
|
|
private $nameQuery = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $outputType = self::OUTPUT_BODY; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* By default we search for classes with any name |
40
|
|
|
*/ |
41
|
39 |
|
public function __construct() { |
42
|
39 |
|
$this->nameQuery = Strict::create()->valueLike('!.+!'); |
43
|
39 |
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @codeCoverageIgnore |
48
|
|
|
* @deprecated |
49
|
|
|
* @param string $name |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function nameIs($name) { |
53
|
|
|
trigger_error("Deprecated. Use withName", E_USER_DEPRECATED); |
54
|
|
|
return $this->withName($name); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param QueryStrategy|string $name |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
12 |
|
public function withName($name) { |
63
|
12 |
|
if (is_string($name)) { |
64
|
6 |
|
$this->nameQuery = Strict::create()->valueIs($name); |
65
|
8 |
|
} elseif ($name instanceof QueryStrategy) { |
66
|
6 |
|
$this->nameQuery = $name; |
67
|
4 |
|
} else { |
68
|
3 |
|
throw new \InvalidArgumentException('Expect string or QueryInterface'); |
69
|
|
|
} |
70
|
|
|
|
71
|
9 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function outputBody() { |
79
|
|
|
$this->outputType = self::OUTPUT_BODY; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
6 |
|
public function outputFull() { |
88
|
6 |
|
$this->outputType = self::OUTPUT_FULL; |
89
|
6 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @codeCoverageIgnore |
95
|
|
|
* @deprecated |
96
|
|
|
* @param QueryStrategy $strategy |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function whereName(QueryStrategy $strategy) { |
100
|
|
|
trigger_error("Deprecated. Use withName", E_USER_DEPRECATED); |
101
|
|
|
return $this->withName($strategy); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
36 |
|
public function __invoke(QuerySequence $querySequence) { |
109
|
|
|
|
110
|
36 |
|
$start = $querySequence->strict('class'); |
111
|
36 |
|
$querySequence->strict(T_WHITESPACE); |
112
|
36 |
|
$querySequence->process($this->nameQuery); |
113
|
36 |
|
$startClassBody = $querySequence->search('{'); |
114
|
36 |
|
$querySequence->moveToToken($startClassBody); |
115
|
36 |
|
$body = $querySequence->section('{', '}'); |
116
|
|
|
|
117
|
36 |
|
if ($start->isValid()) { |
118
|
|
|
# catch class modifiers |
119
|
33 |
|
$querySequence->moveToToken($start); |
120
|
33 |
|
$querySequence->move(-2); |
121
|
33 |
|
$modifier = $querySequence->process(Possible::create()->valueIs([ |
|
|
|
|
122
|
33 |
|
'abstract', |
123
|
22 |
|
'final', |
124
|
22 |
|
])); |
125
|
33 |
|
if ($modifier->isValid()) { |
126
|
3 |
|
$start = $modifier; |
127
|
2 |
|
} |
128
|
22 |
|
} |
129
|
|
|
|
130
|
36 |
|
if (!$querySequence->isValid()) { |
131
|
36 |
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
33 |
|
if ($this->outputType == self::OUTPUT_BODY) { |
135
|
27 |
|
return $body->extractItems(1, -1); |
136
|
|
|
} |
137
|
|
|
|
138
|
6 |
|
if ($this->outputType == self::OUTPUT_FULL) { |
139
|
6 |
|
return $querySequence->getCollection()->extractByTokens($start, $body->getLast()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: