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

PrettyPrinter::pStmts()   B

Complexity

Conditions 10
Paths 28

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 15.8818

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 11
cts 18
cp 0.6111
rs 7.6666
c 0
b 0
f 0
cc 10
nc 28
nop 2
crap 15.8818

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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