1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpToZephir\Converter\Printer\Expr; |
4
|
|
|
|
5
|
|
|
use PhpToZephir\Converter\Dispatcher; |
6
|
|
|
use PhpToZephir\Logger; |
7
|
|
|
use PhpParser\Node\Expr; |
8
|
|
|
use PhpToZephir\Converter\Manipulator\ArrayManipulator; |
9
|
|
|
|
10
|
|
|
class ArrayDimFetchPrinter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Dispatcher |
14
|
|
|
*/ |
15
|
|
|
private $dispatcher = null; |
16
|
|
|
/** |
17
|
|
|
* @var Logger |
18
|
|
|
*/ |
19
|
|
|
private $logger = null; |
20
|
|
|
/** |
21
|
|
|
* @var ArrayManipulator |
22
|
|
|
*/ |
23
|
|
|
private $arrayManipulator = null; |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $createdVars = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Dispatcher $dispatcher |
31
|
|
|
* @param Logger $logger |
32
|
|
|
* @param ArrayManipulator $arrayManipulator |
33
|
|
|
*/ |
34
|
1 |
|
public function __construct(Dispatcher $dispatcher, Logger $logger, ArrayManipulator $arrayManipulator) |
35
|
|
|
{ |
36
|
1 |
|
$this->dispatcher = $dispatcher; |
|
|
|
|
37
|
1 |
|
$this->logger = $logger; |
|
|
|
|
38
|
1 |
|
$this->arrayManipulator = $arrayManipulator; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
79 |
|
public static function resetCreatedVars() |
42
|
|
|
{ |
43
|
79 |
|
self::$createdVars = array(); |
44
|
79 |
|
} |
45
|
|
|
|
46
|
1 |
|
public static function getType() |
47
|
|
|
{ |
48
|
1 |
|
return 'pExpr_ArrayDimFetch'; |
49
|
|
|
} |
50
|
|
|
|
51
|
20 |
|
public function convert(Expr\ArrayDimFetch $node, $returnAsArray = false) |
52
|
|
|
{ |
53
|
20 |
|
$collected = $this->arrayManipulator->arrayNeedToBeSplit($node); |
54
|
|
|
|
55
|
20 |
|
if ($collected !== false) { |
56
|
5 |
|
return $this->splitArray($collected, $returnAsArray); |
57
|
|
|
} |
58
|
|
|
|
59
|
15 |
|
$result = $this->dispatcher->pVarOrNewExpr($node->var) |
60
|
15 |
|
.'['.(null !== $node->dim ? $this->dispatcher->p($node->dim) : '').']'; |
61
|
|
|
|
62
|
15 |
|
if ($returnAsArray === true) { |
63
|
|
|
return array( |
64
|
15 |
|
'head' => '', |
65
|
15 |
|
'lastExpr' => $result, |
66
|
15 |
|
'vars' => array() |
67
|
15 |
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
15 |
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param bool $returnAsArray |
75
|
|
|
*/ |
76
|
5 |
|
private function splitArray(array $collected, $returnAsArray) |
77
|
|
|
{ |
78
|
5 |
|
$var = $collected[0]; |
79
|
5 |
|
$lastExpr = $this->dispatcher->p($var); |
80
|
5 |
|
$createAsTmp = array(); |
81
|
5 |
|
$head = array(); |
82
|
5 |
|
$vars = array(); |
83
|
|
|
|
84
|
5 |
|
unset($collected[0]); |
85
|
|
|
|
86
|
5 |
|
foreach ($collected as $expr) { |
87
|
5 |
|
if ($expr['splitTab'] === true) { |
88
|
5 |
|
$createAsTmp = $this->addAsTmp($createAsTmp, $expr['var'] ); |
89
|
5 |
|
$tmpVarName = 'tmp' . ucfirst($expr['var']) . $createAsTmp[$expr['var']]; |
90
|
5 |
|
$vars[] = $tmpVarName; |
91
|
5 |
|
$head[] = $expr['expr']; |
92
|
5 |
|
$head[] = 'let ' . $tmpVarName . ' = ' . $expr['var'] . ";\n"; |
93
|
5 |
|
$lastExpr .= '[' . $tmpVarName .']'; |
|
|
|
|
94
|
5 |
|
} else { |
95
|
2 |
|
$lastExpr .= '['.$expr['expr'].']'; |
96
|
|
|
} |
97
|
5 |
|
} |
98
|
|
|
|
99
|
5 |
|
if ($returnAsArray === true) { |
100
|
|
|
return array( |
101
|
5 |
|
'head' => !empty($head) ? "\n" . implode("", $head) . "\n" : "", |
|
|
|
|
102
|
5 |
|
'lastExpr' => $lastExpr, |
103
|
|
|
'vars' => $vars |
104
|
5 |
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return (!empty($head) ? "\n" . implode("", $head) . "\n" : "").$lastExpr; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
5 |
|
private function addAsTmp(array $createAsTmp, $varname) |
111
|
|
|
{ |
112
|
5 |
|
if (!isset(self::$createdVars[$this->dispatcher->getLastMethod()])) { |
113
|
5 |
|
self::$createdVars[$this->dispatcher->getLastMethod()] = array(); |
114
|
5 |
|
} |
115
|
|
|
|
116
|
5 |
|
if (isset(self::$createdVars[$this->dispatcher->getLastMethod()][$varname])) { |
117
|
3 |
|
self::$createdVars[$this->dispatcher->getLastMethod()][$varname]++; |
118
|
3 |
|
$createAsTmp[$varname] = self::$createdVars[$this->dispatcher->getLastMethod()][$varname]; |
119
|
3 |
|
} else { |
120
|
5 |
|
self::$createdVars[$this->dispatcher->getLastMethod()][$varname] = 1; |
121
|
5 |
|
$createAsTmp[$varname] = 1; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
5 |
|
return $createAsTmp; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.