|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Php55Features.php |
|
4
|
|
|
* |
|
5
|
|
|
* MIT LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* LICENSE: This source file is subject to the MIT license. |
|
8
|
|
|
* A copy of the licenses text was distributed alongside this |
|
9
|
|
|
* file (usually the repository or package root). The text can also |
|
10
|
|
|
* be obtained through one of the following sources: |
|
11
|
|
|
* * http://opensource.org/licenses/MIT |
|
12
|
|
|
* * https://github.com/suralc/pvra/blob/master/LICENSE |
|
13
|
|
|
* |
|
14
|
|
|
* @author suralc <[email protected]> |
|
15
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
16
|
|
|
*/ |
|
17
|
|
|
namespace Pvra\Analysers; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
use PhpParser\Node; |
|
21
|
|
|
use Pvra\AnalyserAwareInterface; |
|
22
|
|
|
use Pvra\Result\Reason; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class Php55Features |
|
26
|
|
|
* |
|
27
|
|
|
* Supports the detection of following features: |
|
28
|
|
|
* * Generator definitions using the `yield` - keyword |
|
29
|
|
|
* * `finally` |
|
30
|
|
|
* * Usage of list in foreach `foreach($array() as list($a, $b)) {}` |
|
31
|
|
|
* * Arbitrary expressions in the `empty` construct |
|
32
|
|
|
* * Array and string dereferencing |
|
33
|
|
|
* * Classname resolution using `Name::class` |
|
34
|
|
|
* |
|
35
|
|
|
* @package Pvra\Analysers |
|
36
|
|
|
*/ |
|
37
|
|
|
class Php55Features extends LanguageFeatureAnalyser implements AnalyserAwareInterface |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
*/ |
|
43
|
34 |
|
public function enterNode(Node $node) |
|
44
|
|
|
{ |
|
45
|
34 |
|
if ($this->mode & self::MODE_ADDITION) { |
|
46
|
32 |
|
if ($node instanceof Node\Expr\Yield_) { |
|
47
|
12 |
|
$this->getResult()->addRequirement( |
|
48
|
12 |
|
Reason::GENERATOR_DEFINITION, |
|
49
|
12 |
|
$node->getLine() |
|
50
|
6 |
|
); |
|
51
|
32 |
|
} elseif ($node instanceof Node\Stmt\TryCatch && $node->finallyStmts !== null) { |
|
52
|
12 |
|
$this->getResult()->addRequirement( |
|
53
|
12 |
|
Reason::TRY_CATCH_FINALLY, |
|
54
|
12 |
|
$node->getLine() |
|
55
|
6 |
|
); |
|
56
|
32 |
|
} elseif ($node instanceof Node\Stmt\Foreach_ && $node->valueVar instanceof Node\Expr\List_) { |
|
57
|
10 |
|
$this->getResult()->addRequirement( |
|
58
|
10 |
|
Reason::LIST_IN_FOREACH, |
|
59
|
10 |
|
$node->getLine() |
|
60
|
5 |
|
); |
|
61
|
21 |
|
} elseif ($node instanceof Node\Expr\Empty_ |
|
62
|
21 |
|
&& !($node->expr instanceof Node\Expr\Variable |
|
63
|
10 |
|
|| $node->expr instanceof Node\Expr\PropertyFetch |
|
64
|
10 |
|
|| $node->expr instanceof Node\Expr\StaticPropertyFetch |
|
65
|
21 |
|
|| $node->expr instanceof Node\Expr\ArrayDimFetch) |
|
66
|
16 |
|
) { |
|
67
|
10 |
|
$this->getResult()->addRequirement( |
|
68
|
10 |
|
Reason::EXPR_IN_EMPTY, |
|
69
|
10 |
|
$node->getLine() |
|
70
|
5 |
|
); |
|
71
|
21 |
|
} elseif ($node instanceof Node\Expr\ArrayDimFetch |
|
72
|
26 |
|
&& ($node->var instanceof Node\Expr\Array_ |
|
73
|
26 |
|
|| $node->var instanceof Node\Scalar\String_ |
|
74
|
10 |
|
) |
|
75
|
16 |
|
) { |
|
76
|
10 |
|
$this->getResult()->addRequirement( |
|
77
|
10 |
|
Reason::ARRAY_OR_STRING_DEREFERENCING, |
|
78
|
10 |
|
$node->getLine() |
|
79
|
5 |
|
); |
|
80
|
32 |
|
} elseif ($node instanceof Node\Expr\ClassConstFetch && strcasecmp($node->name, 'class') === 0) { |
|
81
|
10 |
|
$this->getResult()->addRequirement( |
|
82
|
10 |
|
Reason::CLASS_NAME_RESOLUTION, |
|
83
|
10 |
|
$node->getLine() |
|
84
|
5 |
|
); |
|
85
|
5 |
|
} |
|
86
|
16 |
|
} |
|
87
|
34 |
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|