|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpToZephir\Converter; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node\Stmt; |
|
6
|
|
|
use PhpToZephir\ReservedWordReplacer; |
|
7
|
|
|
use PhpToZephir\NodeFetcher; |
|
8
|
|
|
|
|
9
|
|
|
class ClassInformation |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var ReservedWordReplacer |
|
13
|
|
|
*/ |
|
14
|
|
|
private $reservedWordReplacer = null; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var NodeFetcher |
|
17
|
|
|
*/ |
|
18
|
|
|
private $nodeFetcher = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param ReservedWordReplacer $reservedWordReplacer |
|
22
|
|
|
* @param NodeFetcher $nodeFetcher |
|
23
|
|
|
*/ |
|
24
|
82 |
|
public function __construct(ReservedWordReplacer $reservedWordReplacer, NodeFetcher $nodeFetcher) |
|
25
|
|
|
{ |
|
26
|
82 |
|
$this->reservedWordReplacer = $reservedWordReplacer; |
|
27
|
82 |
|
$this->nodeFetcher = $nodeFetcher; |
|
|
|
|
|
|
28
|
82 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $nodes |
|
32
|
|
|
* |
|
33
|
|
|
* @return \PhpToZephir\Converter\ClassMetadata |
|
34
|
|
|
*/ |
|
35
|
82 |
|
public function getClassesMetdata(array $nodes) |
|
36
|
|
|
{ |
|
37
|
82 |
|
$classMetadata = new ClassMetadata(); |
|
38
|
|
|
|
|
39
|
82 |
|
$classMetadata = $this->build($nodes, $classMetadata); |
|
40
|
|
|
|
|
41
|
82 |
|
$namespace = $classMetadata->getNamespace(); |
|
42
|
|
|
|
|
43
|
82 |
|
if ($namespace === null) { |
|
44
|
|
|
throw new \Exception('Namespace not found'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
82 |
|
return $classMetadata; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $nodes |
|
52
|
|
|
* @param ClassMetadata $classMetadata |
|
53
|
|
|
* |
|
54
|
|
|
* @return ClassMetadata |
|
55
|
|
|
*/ |
|
56
|
82 |
|
public function build(array $nodes, ClassMetadata $classMetadata) |
|
57
|
|
|
{ |
|
58
|
82 |
|
$class = null; |
|
59
|
82 |
|
foreach ($this->nodeFetcher->foreachNodes($nodes) as $nodeData) { |
|
60
|
82 |
|
$node = $nodeData['node']; |
|
61
|
82 |
|
if ($node instanceof Stmt\UseUse) { |
|
62
|
4 |
|
$classMetadata->addUse($node); |
|
63
|
4 |
|
$classMetadata->addClasses($this->reservedWordReplacer->replace(implode('\\', $node->name->parts))); |
|
64
|
4 |
|
if ($node->name->getLast() !== $node->alias) { |
|
65
|
|
|
$classMetadata->addClassesAlias( |
|
66
|
|
|
$node->alias, |
|
67
|
|
|
$this->reservedWordReplacer->replace(implode('\\', $node->name->parts)) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
82 |
|
} elseif ($node instanceof Stmt\Namespace_) { |
|
71
|
82 |
|
$classMetadata->setNamespace(implode('\\', $node->name->parts)); |
|
72
|
82 |
|
} elseif ($node instanceof Stmt\Interface_ || $node instanceof Stmt\Class_) { |
|
73
|
82 |
|
if ($class !== null) { |
|
74
|
|
|
throw new \Exception('Multiple class find in '.$fileName); |
|
75
|
|
|
} |
|
76
|
82 |
|
$class = $this->reservedWordReplacer->replace($node->name); |
|
77
|
82 |
|
$classMetadata->setClass($class); |
|
78
|
|
|
|
|
79
|
82 |
|
if ($node instanceof Stmt\Interface_ ) { |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
82 |
|
} elseif ($node instanceof Stmt\Class_ && $node->implements !== null) { |
|
82
|
82 |
|
$implementsClean = array(); |
|
83
|
82 |
|
foreach ($node->implements as $implement) { |
|
84
|
5 |
|
$implementsClean[] = $this->reservedWordReplacer->replace(implode('\\', $implement->parts)); |
|
85
|
82 |
|
} |
|
86
|
82 |
|
$classMetadata->setImplements($implementsClean); |
|
87
|
82 |
|
} |
|
88
|
82 |
|
} elseif ($node instanceof Stmt\Interface_ || $node instanceof Stmt\Class_) { |
|
89
|
|
|
if ($class !== null) { |
|
90
|
|
|
throw new \Exception('Multiple class find in '.$fileName); |
|
91
|
|
|
} |
|
92
|
|
|
$class = $this->reservedWordReplacer->replace($node->name); |
|
93
|
|
|
$classMetadata->setClass($class); |
|
94
|
|
|
} |
|
95
|
82 |
|
} |
|
96
|
|
|
|
|
97
|
82 |
|
return $classMetadata; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.