Completed
Pull Request — master (#35)
by Saif Eddin
02:55 queued 38s
created

ClassInformation::build()   C

Complexity

Conditions 15
Paths 12

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 21.4215

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 25
cts 36
cp 0.6944
rs 5.0504
c 0
b 0
f 0
cc 15
eloc 30
nc 12
nop 2
crap 21.4215

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
3
namespace PhpToZephir\Converter;
4
5
use PhpParser\Node\Stmt;
6
use PhpToZephir\ReservedWordReplacer;
7
use PhpToZephir\NodeFetcher;
8
9
class ClassInformation
10
{
11
    /**
12
     * @var ReservedWordReplacer
13
     */
14
    private $reservedWordReplacer = null;
15
    /**
16
     * @var NodeFetcher
17
     */
18
    private $nodeFetcher = null;
19
20
    /**
21
     * @param ReservedWordReplacer $reservedWordReplacer
22
     * @param NodeFetcher          $nodeFetcher
23
     */
24 82
    public function __construct(ReservedWordReplacer $reservedWordReplacer, NodeFetcher $nodeFetcher)
25
    {
26 82
        $this->reservedWordReplacer = $reservedWordReplacer;
27 82
        $this->nodeFetcher = $nodeFetcher;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
28 82
    }
29
30
    /**
31
     * @param array $nodes
32
     *
33
     * @return \PhpToZephir\Converter\ClassMetadata
34
     */
35 82
    public function getClassesMetdata(array $nodes)
36
    {
37 82
        $classMetadata = new ClassMetadata();
38
39 82
        $classMetadata = $this->build($nodes, $classMetadata);
40
41 82
        $namespace = $classMetadata->getNamespace();
42
43 82
        if ($namespace === null) {
44
            throw new \Exception('Namespace not found');
45
        }
46
47 82
        return $classMetadata;
48
    }
49
50
    /**
51
     * @param array         $nodes
52
     * @param ClassMetadata $classMetadata
53
     *
54
     * @return ClassMetadata
55
     */
56 82
    public function build(array $nodes, ClassMetadata $classMetadata)
57
    {
58 82
        $class = null;
59 82
        foreach ($this->nodeFetcher->foreachNodes($nodes) as $nodeData) {
60 82
            $node = $nodeData['node'];
61 82
            if ($node instanceof Stmt\UseUse) {
62 4
                $classMetadata->addUse($node);
63 4
                $classMetadata->addClasses($this->reservedWordReplacer->replace(implode('\\', $node->name->parts)));
64 4
                if ($node->name->getLast() !== $node->alias) {
65
                    $classMetadata->addClassesAlias(
66
                        $node->alias,
67
                        $this->reservedWordReplacer->replace(implode('\\', $node->name->parts))
68
                    );
69
                }
70 82
            } elseif ($node instanceof Stmt\Namespace_) {
71 82
                $classMetadata->setNamespace(implode('\\', $node->name->parts));
72 82
            } elseif ($node instanceof Stmt\Interface_ || $node instanceof Stmt\Class_) {
73 82
                if ($class !== null) {
74
                    throw new \Exception('Multiple class find in '.$fileName);
75
                }
76 82
                $class = $this->reservedWordReplacer->replace($node->name);
77 82
                $classMetadata->setClass($class);
78
79 82
                if ($node instanceof Stmt\Interface_ ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
80
                	
81 82
                } elseif ($node instanceof Stmt\Class_ && $node->implements !== null) {
82 82
                    $implementsClean = array();
83 82
                    foreach ($node->implements as $implement) {
84 5
                        $implementsClean[] = $this->reservedWordReplacer->replace(implode('\\', $implement->parts));
85 82
                    }
86 82
                    $classMetadata->setImplements($implementsClean);
87 82
                }
88 82
            } elseif ($node instanceof Stmt\Interface_ || $node instanceof Stmt\Class_) {
89
                if ($class !== null) {
90
                    throw new \Exception('Multiple class find in '.$fileName);
91
                }
92
                $class = $this->reservedWordReplacer->replace($node->name);
93
                $classMetadata->setClass($class);
94
            }
95 82
        }
96
97 82
        return $classMetadata;
98
    }
99
}
100