|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Jean-François Lépine <https://twitter.com/Halleck45> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Hal\Component\Parser\CodeParser; |
|
11
|
|
|
|
|
12
|
|
|
use Hal\Component\Reflected\Method; |
|
13
|
|
|
use Hal\Component\Parser\Resolver\NamespaceResolver; |
|
14
|
|
|
use Hal\Component\Parser\Searcher; |
|
15
|
|
|
use Hal\Component\Reflected\MethodUsage; |
|
16
|
|
|
|
|
17
|
|
|
class MethodUsageParser |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Searcher |
|
22
|
|
|
*/ |
|
23
|
|
|
private $searcher; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var NamespaceResolver |
|
27
|
|
|
*/ |
|
28
|
|
|
private $namespaceResolver; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* CodeParser constructor. |
|
32
|
|
|
* @param Searcher $searcher |
|
33
|
|
|
* @param NamespaceResolver $namespaceResolver |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Searcher $searcher, NamespaceResolver $namespaceResolver) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->searcher = $searcher; |
|
38
|
|
|
$this->namespaceResolver = $namespaceResolver; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Method $method |
|
44
|
|
|
* @return int |
|
45
|
|
|
*/ |
|
46
|
|
|
public function parse(Method $method) { |
|
47
|
|
|
|
|
48
|
|
|
$start = $this->searcher->getNext($method->getTokens(), 0, '{') + 1; |
|
49
|
|
|
$len = sizeof($method->getTokens()); |
|
50
|
|
|
$tokens = array_slice($method->getTokens(), $start, ($len - $start) - 1); |
|
51
|
|
|
|
|
52
|
|
|
foreach($tokens as $n => $token) { |
|
53
|
|
|
// replace $this->aaa by "class_attribute |
|
54
|
|
|
if(preg_match('!^\$this\->\w+$!', $token)) { |
|
55
|
|
|
$tokens[$n] = 'class_attribute'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// replace vars by "var" |
|
59
|
|
|
if(preg_match('!^\$\w+$!', $token) && $token != '$this') { |
|
60
|
|
|
$tokens[$n] = 'var'; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
switch($tokens) { |
|
64
|
|
|
// getters |
|
65
|
|
|
case array('return', 'cast', 'class_attribute'): |
|
66
|
|
|
case array('return','class_attribute'): |
|
67
|
|
|
return MethodUsage::USAGE_GETTER; |
|
68
|
|
|
break; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
// setters |
|
71
|
|
|
case array('class_attribute', '=', 'var'): |
|
72
|
|
|
case array('class_attribute', '=', 'cast', 'var'): |
|
73
|
|
|
case array('class_attribute', '=', 'var', 'return', '$this'): |
|
74
|
|
|
case array('class_attribute', '=', 'cast', 'var', 'return', '$this'): |
|
75
|
|
|
return MethodUsage::USAGE_SETTER; |
|
76
|
|
|
break; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return MethodUsage::USAGE_UNKNWON; |
|
81
|
|
|
} |
|
82
|
|
|
} |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.