|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Padawan\Framework\Complete\Resolver; |
|
4
|
|
|
|
|
5
|
|
|
use Padawan\Domain\Completion\Token; |
|
6
|
|
|
use Padawan\Domain\Completion\Context; |
|
7
|
|
|
use Padawan\Domain\Scope; |
|
8
|
|
|
use Padawan\Domain\Scope\FileScope; |
|
9
|
|
|
use Padawan\Domain\Project\FQN; |
|
10
|
|
|
use Padawan\Domain\Project\Index; |
|
11
|
|
|
use Padawan\Domain\Project\FQCN; |
|
12
|
|
|
use Padawan\Parser\ErrorFreePhpParser; |
|
13
|
|
|
use Padawan\Parser\UseParser; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use PhpParser\Node\Expr\Variable; |
|
16
|
|
|
use PhpParser\Node\Name; |
|
17
|
|
|
|
|
18
|
|
|
class ContextResolver |
|
19
|
|
|
{ |
|
20
|
|
|
public function __construct( |
|
21
|
|
|
ErrorFreePhpParser $parser, |
|
22
|
|
|
NodeTypeResolver $typeResolver, |
|
23
|
|
|
LoggerInterface $logger, |
|
24
|
|
|
UseParser $useParser |
|
25
|
|
|
) { |
|
26
|
|
|
$this->parser = $parser; |
|
27
|
|
|
$this->typeResolver = $typeResolver; |
|
28
|
|
|
$this->logger = $logger; |
|
29
|
|
|
$this->useParser = $useParser; |
|
30
|
|
|
} |
|
31
|
|
|
public function getContext($badLine, Index $index, Scope $scope = null) |
|
32
|
|
|
{ |
|
33
|
|
|
if (empty($scope)) { |
|
34
|
|
|
$scope = new FileScope(new FQN); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$token = $this->getLastToken($badLine); |
|
38
|
|
|
$this->logger->debug(sprintf( |
|
39
|
|
|
'Found token \'%s\' with type %s', |
|
40
|
|
|
$token->getSymbol(), |
|
41
|
|
|
$token->getType() |
|
42
|
|
|
)); |
|
43
|
|
|
return $this->createContext($scope, $token, $badLine, $index); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return Token |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function getLastToken($badLine) |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
$symbols = @token_get_all($this->prepareLine($badLine, false)); |
|
53
|
|
|
} catch (\Exception $e) { |
|
54
|
|
|
$symbols = [0, 0]; |
|
55
|
|
|
} |
|
56
|
|
|
$token = null; |
|
57
|
|
|
array_shift($symbols); |
|
58
|
|
|
do { |
|
59
|
|
|
$token = $this->addSymbolForToken(array_pop($symbols), $token); |
|
60
|
|
|
} while (!$token->isReady() && count($symbols)); |
|
61
|
|
|
return $token; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function createContext(Scope $scope, Token $token, $badLine, Index $index) |
|
65
|
|
|
{ |
|
66
|
|
|
$context = new Context($scope, $token); |
|
67
|
|
|
$nodes = $this->parser->parse($this->prepareLine($badLine)); |
|
68
|
|
|
|
|
69
|
|
|
if ($token->isObjectOperator() || $token->isStaticOperator() || $token->isMethodCall()) { |
|
70
|
|
|
if (is_array($nodes)) { |
|
71
|
|
|
$workingNode = array_pop($nodes); |
|
72
|
|
|
} else { |
|
73
|
|
|
$workingNode = $nodes; |
|
74
|
|
|
} |
|
75
|
|
|
$isThis = false; |
|
76
|
|
|
if ($workingNode instanceof Variable && $workingNode->name === 'this') { |
|
77
|
|
|
$isThis = true; |
|
78
|
|
|
} |
|
79
|
|
|
if ($workingNode instanceof Name) { |
|
80
|
|
|
$nodeFQCN = $this->useParser->getFQCN($workingNode); |
|
81
|
|
|
if ($scope->getFQCN() instanceof FQCN |
|
|
|
|
|
|
82
|
|
|
&& $nodeFQCN->toString() === $scope->getFQCN()->toString() |
|
|
|
|
|
|
83
|
|
|
) { |
|
84
|
|
|
$isThis = true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
$types = $this->typeResolver->getChainType($workingNode, $index, $scope); |
|
88
|
|
|
$context->setData([ |
|
89
|
|
|
array_pop($types), |
|
90
|
|
|
$isThis, |
|
91
|
|
|
$types, |
|
92
|
|
|
$workingNode |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
if ($token->isUseOperator() |
|
96
|
|
|
|| $token->isNamespaceOperator() |
|
97
|
|
|
|| $token->isNewOperator() |
|
98
|
|
|
) { |
|
99
|
|
|
$context->setData(trim($token->getSymbol())); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $context; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function addSymbolForToken($symbol, Token $token = null) |
|
106
|
|
|
{ |
|
107
|
|
|
if (is_array($symbol)) { |
|
108
|
|
|
$code = $symbol[0]; |
|
109
|
|
|
$symbol = $symbol[1]; |
|
110
|
|
|
} else { |
|
111
|
|
|
$code = $symbol; |
|
112
|
|
|
} |
|
113
|
|
|
if (empty($token)) { |
|
114
|
|
|
$token = new Token($code, $symbol); |
|
115
|
|
|
} else { |
|
116
|
|
|
$token->add($code, $symbol); |
|
117
|
|
|
} |
|
118
|
|
|
return $token; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function prepareLine($badLine, $wrapFunctionCall = true) |
|
122
|
|
|
{ |
|
123
|
|
|
if (strpos($badLine, '<?php') === false |
|
124
|
|
|
|| strpos($badLine, '<?') === false |
|
125
|
|
|
) { |
|
126
|
|
|
$badLine = '<?php ' . $badLine; |
|
127
|
|
|
} |
|
128
|
|
|
$badLine = str_replace(['elseif', 'else', 'catch'], '', $badLine); |
|
129
|
|
|
if ($wrapFunctionCall && $badLine[strlen($badLine) - 1] === '(') { |
|
130
|
|
|
$badLine .= ')'; |
|
131
|
|
|
} |
|
132
|
|
|
return $badLine; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
private $logger; |
|
136
|
|
|
private $parser; |
|
137
|
|
|
private $typeResolver; |
|
138
|
|
|
private $useParser; |
|
139
|
|
|
} |
|
140
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: