Completed
Push — master ( 83c363...ccf8c1 )
by Thomas
03:58
created

PrettyPrinter::pExpr_Array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace gossi\codegen\parser;
3
4
use PhpParser\Node;
5
use PhpParser\Node\Expr;
6
use PhpParser\Node\Expr\Array_;
7
use PhpParser\Node\Stmt;
8
use PhpParser\PrettyPrinter\Standard;
9
10
class PrettyPrinter extends Standard {
11
12
 /**
13
     * Pretty prints an array of nodes (statements) and indents them optionally.
14
     *
15
     * @param Node[] $nodes  Array of nodes
16
     * @param bool   $indent Whether to indent the printed nodes
17
     *
18
     * @return string Pretty printed statements
19
     */
20 1
    protected function pStmts(array $nodes, bool $indent = true): string {
21 1
        $result = '';
22 1
		$prevNode = null;
23
24 1
        foreach ($nodes as $node) {
25 1
            $comments = $node->getAttribute('comments', []);
26 1
            if ($comments) {
27
                $result .= "\n" . $this->pComments($comments);
28
                if ($node instanceof Stmt\Nop) {
29
                    continue;
30
                }
31
            }
32
33 1
		  if ($prevNode && $prevNode->getLine() && $node->getLine()) {
34
			  $diff = $node->getLine()- $prevNode->getLine();
35
			  if ($diff > 0) {
36
				  $result .= str_repeat("\n", $diff - 1);
37
			  }
38
		  }
39
40 1
          $result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : '');
41 1
		  $prevNode = $node;
42
        }
43
44 1
	   if ($indent) {
45
            return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n    ", $result);
0 ignored issues
show
Bug introduced by
The property noIndentToken does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
46
        } else {
47 1
            return $result;
48
        }
49
    }
50
51 2
	public function pExpr_Array(Array_ $node) {
52 2
		return '[' . $this->pCommaSeparated($node->items) . ']';
53
	}
54
}
55