1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpToZephir\Converter; |
4
|
|
|
|
5
|
|
|
use PhpToZephir\Logger; |
6
|
|
|
use PhpToZephir\NodeFetcher; |
7
|
|
|
use PhpParser\Node\Expr; |
8
|
|
|
use PhpParser\Node\Stmt; |
9
|
|
|
use PhpToZephir\Converter\Printer\Expr\ClosurePrinter; |
10
|
|
|
use PhpToZephir\ClassCollector; |
11
|
|
|
|
12
|
|
|
class Converter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Dispatcher |
16
|
|
|
*/ |
17
|
|
|
private $dispatcher = null; |
18
|
|
|
/** |
19
|
|
|
* @var NodeFetcher |
20
|
|
|
*/ |
21
|
|
|
private $nodeFetcher = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Dispatcher $dispatcher |
25
|
|
|
* @param NodeFetcher $nodeFetcher |
26
|
|
|
*/ |
27
|
1 |
|
public function __construct(Dispatcher $dispatcher, NodeFetcher $nodeFetcher) |
28
|
|
|
{ |
29
|
1 |
|
$this->dispatcher = $dispatcher; |
|
|
|
|
30
|
1 |
|
$this->nodeFetcher = $nodeFetcher; |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $stmts |
35
|
|
|
* @param ClassCollector $classCollector |
36
|
|
|
* @param Logger $logger |
37
|
|
|
* @param string $fileName |
38
|
|
|
* @param array $classCollected |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
82 |
|
public function nodeToZephir(array $stmts, ClassCollector $classCollector, Logger $logger, $fileName = null, array $classCollected = array()) |
|
|
|
|
43
|
|
|
{ |
44
|
82 |
|
$classInformation = ClassInformationFactory::getInstance(); |
45
|
82 |
|
$metadata = $classInformation->getClassesMetdata($stmts); |
|
|
|
|
46
|
|
|
|
47
|
82 |
|
$this->implementsExist($metadata, $classCollector); |
48
|
|
|
|
49
|
|
|
return array( |
50
|
81 |
|
'code' => $this->dispatcher->convert($stmts, $metadata, $classCollector, $logger), |
51
|
81 |
|
'namespace' => $metadata->getNamespace(), |
52
|
81 |
|
'additionalClass' => $this->findAdditionalClasses($stmts, $logger), |
53
|
81 |
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ClassMetadata $metadata |
58
|
|
|
* @param ClassCollector $classCollector |
59
|
|
|
*/ |
60
|
82 |
|
private function implementsExist(ClassMetadata $metadata, ClassCollector $classCollector) |
61
|
|
|
{ |
62
|
82 |
|
foreach ($metadata->getImplements() as $implements) { |
63
|
5 |
|
$this->implementExist($metadata, $classCollector, $implements); |
64
|
81 |
|
} |
65
|
81 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param ClassMetadata $metadata |
69
|
|
|
* @param ClassCollector $classCollector |
70
|
|
|
* @param string $implements |
71
|
|
|
* @throws \Exception |
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
5 |
|
private function implementExist(ClassMetadata $metadata, ClassCollector $classCollector, $implements) |
75
|
|
|
{ |
76
|
|
|
// Class is in actual namespace |
77
|
5 |
|
if (array_key_exists($metadata->getNamespace() . '\\' . $implements, $classCollector->getCollected())) { |
78
|
3 |
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
foreach ($metadata->getClasses() as $use) { |
82
|
1 |
|
if (substr(strrchr($use, "\\"), 1) === $implements) { |
|
|
|
|
83
|
1 |
|
return true; |
84
|
|
|
} |
85
|
2 |
|
} |
86
|
|
|
|
87
|
1 |
|
if (interface_exists($implements)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
throw new \Exception(sprintf('interface %s does not exist', $implements)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $stmts |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
81 |
|
private function findAdditionalClasses(array $stmts, Logger $logger) |
100
|
|
|
{ |
101
|
81 |
|
$closurePrinter = new ClosurePrinter($this->dispatcher, $logger); |
102
|
81 |
|
$lastMethod = null; |
|
|
|
|
103
|
81 |
|
$aditionalClass = array(); |
104
|
81 |
|
$number = 0; |
|
|
|
|
105
|
|
|
|
106
|
81 |
|
foreach ($this->nodeFetcher->foreachNodes($stmts) as $nodeData) { |
107
|
81 |
|
$node = $nodeData['node']; |
108
|
81 |
|
if ($node instanceof Stmt\ClassMethod) { |
109
|
79 |
|
$lastMethod = $node->name; |
110
|
81 |
|
} elseif ($node instanceof Expr\Closure) { |
111
|
1 |
|
$aditionalClass[] = $closurePrinter->createClosureClass($node, $lastMethod, $number); |
112
|
1 |
|
++$number; |
113
|
1 |
|
} |
114
|
81 |
|
} |
115
|
|
|
|
116
|
81 |
|
return $aditionalClass; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.