1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\PhpTokenizer\Pattern\Patterns; |
4
|
|
|
|
5
|
|
|
use Funivan\PhpTokenizer\QuerySequence\QuerySequence; |
6
|
|
|
use Funivan\PhpTokenizer\Strategy\QueryStrategy; |
7
|
|
|
use Funivan\PhpTokenizer\Strategy\Strict; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Pattern used to finding classes in tour source code |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class ClassPattern implements PatternInterface { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var QueryStrategy |
17
|
|
|
*/ |
18
|
|
|
private $nameQuery = null; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* By default we search for classes with any name |
23
|
|
|
*/ |
24
|
31 |
|
public function __construct() { |
25
|
22 |
|
$this->nameQuery = Strict::create()->valueLike('!.+!'); |
26
|
31 |
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @codeCoverageIgnore |
31
|
|
|
* @deprecated |
32
|
|
|
* @param string $name |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function nameIs($name) { |
36
|
|
|
trigger_error("Deprecated. Use withName", E_USER_DEPRECATED); |
37
|
|
|
return $this->withName($name); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param QueryStrategy|string $name |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
12 |
|
public function withName($name) { |
46
|
10 |
|
if (is_string($name)) { |
47
|
6 |
|
$this->nameQuery = Strict::create()->valueIs($name); |
48
|
7 |
|
} elseif ($name instanceof QueryStrategy) { |
49
|
5 |
|
$this->nameQuery = $name; |
50
|
2 |
|
} else { |
51
|
2 |
|
throw new \InvalidArgumentException('Expect string or QueryInterface'); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
10 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @codeCoverageIgnore |
60
|
|
|
* @deprecated |
61
|
|
|
* @param QueryStrategy $strategy |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function whereName(QueryStrategy $strategy) { |
65
|
|
|
trigger_error("Deprecated. Use withName", E_USER_DEPRECATED); |
66
|
|
|
return $this->withName($strategy); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
20 |
|
public function __invoke(QuerySequence $querySequence) { |
74
|
|
|
|
75
|
20 |
|
$querySequence->strict('class'); |
76
|
20 |
|
$querySequence->strict(T_WHITESPACE); |
77
|
20 |
|
$querySequence->process($this->nameQuery); |
78
|
20 |
|
$body = $querySequence->section('{', '}'); |
79
|
|
|
|
80
|
20 |
|
if ($querySequence->isValid()) { |
81
|
18 |
|
return $body->extractItems(1, -1); |
82
|
|
|
} |
83
|
|
|
|
84
|
20 |
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |