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

PrettyPrinter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 2
dl 0
loc 44
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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