Completed
Push — master ( 74dde8...83c363 )
by Thomas
02:00
created

PrettyPrinter::pStmts()   B

Complexity

Conditions 10
Paths 28

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 0
cp 0
rs 7.6666
c 0
b 0
f 0
cc 10
nc 28
nop 2
crap 110

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\PrettyPrinter\Standard;
5
use PhpParser\Node\Expr\Array_;
6
7
class PrettyPrinter extends Standard {
8
	
9 2
 /**
10 2
     * Pretty prints an array of nodes (statements) and indents them optionally.
11
     *
12
     * @param Node[] $nodes  Array of nodes
13
     * @param bool   $indent Whether to indent the printed nodes
14
     *
15
     * @return string Pretty printed statements
16
     */
17
    protected function pStmts(array $nodes, $indent = true) {
18
        $result = '';
19
	   $nodeBefore = NULL;
20
        foreach ($nodes as $node) {
21
            $comments = $node->getAttribute('comments', array());
22
            if ($comments) {
23
                $result .= "\n" . $this->pComments($comments);
24
                if ($node instanceof Stmt\Nop) {
0 ignored issues
show
Bug introduced by
The class gossi\codegen\parser\Stmt\Nop does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
25
                    continue;
26
                }
27
            }
28
		  
29
		  if ($nodeBefore && $nodeBefore->getLine() && $node->getLine()) {
30
			  $diff = $node->getLine()- $nodeBefore->getLine();
31
			  if ($diff > 0) {
32
				  $result .= str_repeat("\n", $diff - 1);
33
			  }
34
		  }
35
		  
36
            $result .= "\n" . $this->p($node) . ($node instanceof Expr ? ';' : '');
0 ignored issues
show
Bug introduced by
The class gossi\codegen\parser\Expr does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
37
		  $nodeBefore = $node;
38
        }
39
40
	   if ($indent) {
41
            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...
42
        } else {
43
            return $result;
44
        }
45
    }
46
    
47
	public function pExpr_Array(Array_ $node) {
48
		return '[' . $this->pCommaSeparated($node->items) . ']';
49
	}
50
}