-
-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathReadDetector.php
71 lines (61 loc) · 2.02 KB
/
ReadDetector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Zephir\Detectors;
use Zephir\Variable\Variable;
use function in_array;
use function is_array;
use function strpos;
use function substr;
/**
* Detects if a variable is used in a given expression context
* Since zvals are collected between executions to the same section of code
* We need to ensure that a variable is not contained in the "right-side" expression
* used to assign the variable, avoiding premature initializations.
*/
class ReadDetector
{
public function detect($variable, array $expression): bool
{
if (!isset($expression['type'])) {
return false;
}
/**
* Remove branch from variable name.
*/
$pos = strpos($variable, Variable::BRANCH_MAGIC);
if ($pos > -1) {
$variable = substr($variable, 0, $pos);
}
if ('variable' === $expression['type'] && $variable === $expression['value']) {
return true;
}
if (in_array($expression['type'], ['fcall', 'mcall', 'scall']) && isset($expression['parameters'])) {
foreach ($expression['parameters'] as $parameter) {
if (
is_array($parameter['parameter']) &&
'variable' === $parameter['parameter']['type'] &&
$variable == $parameter['parameter']['value']
) {
return true;
}
}
}
return (
isset($expression['left']) &&
is_array($expression['left']) &&
$this->detect($variable, $expression['left'])
) || (
isset($expression['right']) &&
is_array($expression['right']) &&
$this->detect($variable, $expression['right'])
);
}
}