1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padawan\Parser; |
4
|
|
|
|
5
|
|
|
use Padawan\Domain\Project\FQCN; |
6
|
|
|
use Padawan\Domain\Project\Node\Uses; |
7
|
|
|
use PhpParser\Node\Name; |
8
|
|
|
use PhpParser\Node\Stmt\Use_; |
9
|
|
|
|
10
|
|
|
class UseParser { |
11
|
|
|
/** @var Uses */ |
12
|
|
|
private $uses; |
13
|
|
|
public function parse(Use_ $node){ |
14
|
|
|
foreach($node->uses AS $use){ |
15
|
|
|
$fqcn = $this->parseFQCN($use->name->toString()); |
16
|
|
|
$this->uses->add($fqcn, $use->alias); |
17
|
|
|
} |
18
|
|
|
return $this->uses; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $type |
23
|
|
|
*/ |
24
|
|
|
public function parseType($type){ |
25
|
|
|
$pureFQCN = $this->parseFQCN($type); |
26
|
|
|
if($pureFQCN->isScalar()){ |
27
|
|
|
return $pureFQCN; |
28
|
|
|
} |
29
|
|
|
if(strpos($type, '\\') === 0){ |
30
|
|
|
return $pureFQCN; |
31
|
|
|
} |
32
|
|
|
$fqcn = $this->uses->find($type); |
33
|
|
|
if(!empty($fqcn)){ |
34
|
|
|
return $fqcn; |
35
|
|
|
} |
36
|
|
|
return $this->createFQCN($pureFQCN); |
37
|
|
|
} |
38
|
|
|
public function getFQCN(Name $node = null){ |
39
|
|
|
if($node === null) { |
40
|
|
|
return $node; |
41
|
|
|
} |
42
|
|
|
if($node->isFullyQualified()){ |
43
|
|
|
return $this->parseFQCN($node->toString()); |
44
|
|
|
} |
45
|
|
|
$fqcn = $this->uses->find($node->getFirst()); |
46
|
|
|
if($fqcn){ |
47
|
|
|
return $fqcn; |
48
|
|
|
} |
49
|
|
|
return $this->createFQCN($node->toString()); |
50
|
|
|
} |
51
|
|
|
public function parseFQCN($fqcn){ |
52
|
|
|
$fqcn = trim($fqcn, '\\'); |
53
|
|
|
if(empty($fqcn)){ |
54
|
|
|
return new FQCN(''); |
55
|
|
|
} |
56
|
|
|
$parts = explode('\\', $fqcn); |
57
|
|
|
$name = array_pop($parts); |
58
|
|
|
$regex = '/(\w+)(\\[\\])?/'; |
59
|
|
|
preg_match($regex, $name, $matches); |
60
|
|
|
if(count($matches) === 0){ |
61
|
|
|
throw new \Exception("Could not parse FQCN for empty class name: " . $fqcn); |
62
|
|
|
} |
63
|
|
|
$name = $matches[1]; |
64
|
|
|
$isArray = count($matches) === 3 && $matches[2] = '[]'; |
65
|
|
|
return new FQCN( |
66
|
|
|
$name, |
67
|
|
|
$parts, |
|
|
|
|
68
|
|
|
$isArray |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
public function getUses() { |
72
|
|
|
return $this->uses; |
73
|
|
|
} |
74
|
|
|
public function setUses(Uses $uses = null) { |
75
|
|
|
$this->uses = $uses; |
76
|
|
|
} |
77
|
|
|
protected function createFQCN($fqcn) { |
78
|
|
|
$fqn = $this->uses->getFQCN()->join($this->parseFQCN($fqcn)); |
79
|
|
|
return new FQCN($fqn->getLast(), $fqn->getTail()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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: