CodeGenerator   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUseArray() 0 8 2
A getUseLines() 0 12 3
B getClassNameInContext() 0 11 5
C getDeclarationLine() 0 41 13
1
<?php
2
/**
3
 * Class Dumper
4
 *
5
 * @link      https://github.com/mtymek/class-dumper
6
 * @copyright Copyright (c) 2015 Mateusz Tymek
7
 * @license   BSD 2-Clause
8
 */
9
10
namespace ClassDumper;
11
12
use Zend\Code\Reflection\ClassReflection;
13
14
class CodeGenerator
15
{
16
    /**
17
     * @param ClassReflection $class
18
     * @return array
19
     */
20
    private function getUseArray(ClassReflection $class)
21
    {
22
        $usesNames = [];
23
        foreach ($class->getDeclaringFile()->getUses() as $use) {
24
            $usesNames[$use['use']] = $use['as'];
25
        }
26
        return $usesNames;
27
    }
28
29
    /**
30
     * @param ClassReflection $class
31
     * @return string
32
     */
33
    public function getUseLines(ClassReflection $class)
34
    {
35
        $useLines = [];
36
        foreach ($this->getUseArray($class) as $name => $as) {
37
            $useString = "use $name";
38
            if ($as) {
39
                $useString .= " as $as";
40
            }
41
            $useLines[] = $useString . ';';
42
        }
43
        return $useLines;
44
    }
45
46
    /**
47
     * @param ClassReflection $class
48
     * @param string $namespaceName
49
     * @param array $useArray
50
     * @return string
51
     */
52
    private function getClassNameInContext(ClassReflection $class, $namespaceName, $useArray)
53
    {
54
        if (!$namespaceName) {
55
            return '\\' . $class->getName();
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
56
        }
57
        return array_key_exists($class->getName(), $useArray)
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
58
            ? ($useArray[$class->getName()] ?: $class->getShortName())
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
59
            : ((0 === strpos($class->getName(), $namespaceName))
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
60
                ? substr($class->getName(), strlen($namespaceName) + 1)
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
61
                : '\\' . $class->getName());
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
62
    }
63
64
    /**
65
     * @param ClassReflection $class
66
     * @return string
67
     */
68
    public function getDeclarationLine(ClassReflection $class)
69
    {
70
        $usesNames = $this->getUseArray($class);
71
72
        $declaration = '';
73
        if ($class->isAbstract() && !$class->isInterface() && !$class->isTrait()) {
74
            $declaration .= 'abstract ';
75
        }
76
        if ($class->isFinal()) {
77
            $declaration .= 'final ';
78
        }
79
        if ($class->isInterface()) {
80
            $declaration .= 'interface ';
81
        } elseif ($class->isTrait()) {
82
            $declaration .= 'trait ';
83
        } else {
84
            $declaration .= 'class ';
85
        }
86
        $declaration .= $class->getShortName();
87
        $parentName = false;
88
        if ($parent = $class->getParentClass()) {
89
            $parentName = $this->getClassNameInContext($parent, $class->getNamespaceName(), $usesNames);
90
        }
91
        if ($parentName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parentName of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
92
            $declaration .= " extends {$parentName}";
93
        }
94
        $interfaces = array_diff($class->getInterfaceNames(), $parent ? $parent->getInterfaceNames() : array());
95
        if (count($interfaces)) {
96
            foreach ($interfaces as $interface) {
97
                $iReflection = new ClassReflection($interface);
98
                $interfaces  = array_diff($interfaces, $iReflection->getInterfaceNames());
99
            }
100
            $declaration .= $class->isInterface() ? ' extends ' : ' implements ';
101
            $declaration .= implode(', ', array_map(function($interface) use ($usesNames, $class) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
102
                $iReflection = new ClassReflection($interface);
103
                return $this->getClassNameInContext($iReflection, $class->getNamespaceName(), $usesNames);
104
            }, $interfaces));
105
        }
106
107
        return $declaration;
108
    }
109
}
110