|
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\Parser\Exception\IncorrectSyntaxException; |
|
13
|
|
|
use Hal\Component\Reflected\ReturnedValue; |
|
14
|
|
|
use Hal\Component\Parser\Resolver\NamespaceResolver; |
|
15
|
|
|
use Hal\Component\Parser\Searcher; |
|
16
|
|
|
use Hal\Component\Token\Token; |
|
17
|
|
|
use Hal\Component\Parser\Helper\TypeResolver; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class ReturnParser |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Searcher |
|
25
|
|
|
*/ |
|
26
|
|
|
private $searcher; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var NamespaceResolver |
|
30
|
|
|
*/ |
|
31
|
|
|
private $namespaceResolver; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* CodeParser constructor. |
|
35
|
|
|
* @param Searcher $searcher |
|
36
|
|
|
* @param NamespaceResolver $namespaceResolver |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(Searcher $searcher, NamespaceResolver $namespaceResolver) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->searcher = $searcher; |
|
41
|
|
|
$this->namespaceResolver = $namespaceResolver; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $tokens |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function parse($tokens) |
|
49
|
|
|
{ |
|
50
|
|
|
$returns = array(); |
|
51
|
|
|
|
|
52
|
|
|
// PHP 7 return |
|
53
|
|
|
$closingParenthesis = $this->searcher->getNext($tokens, 0, Token::T_PARENTHESIS_CLOSE); |
|
54
|
|
View Code Duplication |
if($closingParenthesis + 2 <= sizeof($tokens)) { |
|
|
|
|
|
|
55
|
|
|
if(Token::T_FUNCTION_RETURN === $tokens[$closingParenthesis + 1]) { |
|
56
|
|
|
$class = $this->namespaceResolver->resolve($tokens[$closingParenthesis + 2]); |
|
57
|
|
|
array_push($returns, new ReturnedValue($class)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// returns in code |
|
62
|
|
|
$typeResolver = new TypeResolver(); |
|
63
|
|
|
$len = sizeof($tokens); |
|
64
|
|
|
for($i = 0; $i < $len; $i++) { |
|
65
|
|
|
|
|
66
|
|
|
$token = $tokens[$i]; |
|
67
|
|
|
|
|
68
|
|
|
if(Token::T_RETURN_VOID === $token) { |
|
69
|
|
|
array_push($returns, new ReturnedValue(Token::T_VALUE_VOID)); |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if(Token::T_RETURN === $token) { |
|
74
|
|
|
// return with value |
|
75
|
|
|
$next = $tokens[$i + 1]; |
|
76
|
|
View Code Duplication |
if(Token::T_NEW === $next) { |
|
|
|
|
|
|
77
|
|
|
if(!isset($tokens[$i + 2])) { |
|
78
|
|
|
throw new IncorrectSyntaxException('"return new" without classname'); |
|
79
|
|
|
} |
|
80
|
|
|
$classname = $tokens[$i + 2]; |
|
81
|
|
|
array_push($returns, new ReturnedValue($this->namespaceResolver->resolve($classname))); |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// mixed value |
|
86
|
|
|
|
|
87
|
|
|
array_push($returns, new ReturnedValue($typeResolver->resolve($tokens[$i + 1]))); |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
return $returns; |
|
92
|
|
|
} |
|
93
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.