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

ClassInformation   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 74.47%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 7
dl 0
loc 91
ccs 35
cts 47
cp 0.7447
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getClassesMetdata() 0 14 2
C build() 0 43 15
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