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

PrettyPrinter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 65%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 3
dl 0
loc 45
ccs 13
cts 20
cp 0.65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B pStmts() 0 30 10
A pExpr_Array() 0 3 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