Completed
Pull Request — master (#42)
by
unknown
09:06
created

ArrayListingReplacer::leaveNode()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.3554
c 0
b 0
f 0
cc 5
nc 7
nop 1
1
<?php
2
3
4
namespace Spatie\Php7to5\NodeVisitors;
5
6
7
use PhpParser\Node;
8
use PhpParser\NodeVisitorAbstract;
9
10
/**
11
 * Class ArrayListingReplacer
12
 * @package Spatie\Php7to5\NodeVisitors
13
 * @author Jiri Vrba <[email protected]>
14
 *
15
 * Replaces [$a, $b, $c] = ['a', 'b', 'c'] with list()
16
 */
17
class ArrayListingReplacer extends NodeVisitorAbstract
18
{
19
	/**
20
	 * {@inheritdoc}
21
	 */
22
	public function leaveNode(Node $node)
23
	{
24
		if (!$node instanceof Node\Expr\Array_)
25
			return;
26
27
		$stack = [];
28
29
		foreach ($node->items as $arrayItem)
30
		{
31
			if ($arrayItem->value instanceof Node\Expr\Variable)
32
				$stack[] = $arrayItem->value;
33
		}
34
35
		if (!empty($stack))
36
		{
37
			return new Node\Expr\List_($stack);
38
		}
39
	}
40
}