Completed
Push — master ( bc8922...0303f9 )
by Emily
02:07
created

ReflectionFileFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Spaark\CompositeUtils\Factory\Reflection;
4
5
use Spaark\CompositeUtils\Factory\BaseFactory;
6
use Spaark\CompositeUtils\Model\Reflection\ReflectionFile;
7
use Spaark\CompositeUtils\Model\Reflection\NamespaceBlock;
8
use Spaark\CompositeUtils\Model\Reflection\UseStatement;
9
use Spaark\CompositeUtils\Service\RawPropertyAccessor;
10
use \ReflectionClass as PHPNativeReflectionClass;
11
12
class ReflectionFileFactory extends ReflectorFactory
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $filename;
18
19
    /**
20
     * @var int
21
     */
22
    protected $i = 0;
23
24
    /**
25
     * @var array
26
     */
27
    protected $tokens;
28
29 6
    public function __construct(string $filename)
30
    {
31 6
        $this->filename = $filename;
32 6
        $this->object = new ReflectionFile();
33 6
        $this->accessor = new RawPropertyAccessor($this->object);
34 6
    }
35
36 6
    public function build()
37
    {
38 6
        $this->parseFile();
39
40 6
        return $this->object;
41
    }
42
43 6
    public function parseFile()
44
    {
45 6
        $tokens = token_get_all(file_get_contents($this->filename));
46
47 6
        $matching = null;
48 6
        $classname = '';
49 6
        $as = '';
50 6
        $currentNS = null;
51
52 6
        foreach ($tokens as $token)
53
        {
54 6
            if ($token === ';')
55
            {
56 6
                $readingClassName = false;
0 ignored issues
show
Unused Code introduced by
$readingClassName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
58
                switch ($matching)
59
                {
60 6
                    case T_NAMESPACE:
61 6
                        $ns = new NamespaceBlock($classname);
62 6
                        $currentNS = new RawPropertyAccessor($ns);
63 6
                        $this->accessor->rawAddToValue
64
                        (
65 6
                            'namespaces',
66
                            $ns
67
                        );
68 6
                        $currentNS->setRawValue('file', $this->object);
69 6
                        $matching = null;
70 6
                        break;
71 6
                    case T_AS:
72 6
                    case T_USE:
73 6
                        if (!$as)
74
                        {
75 6
                            $as = explode('\\', $classname);
76 6
                            $as = end($as);
77
                        }
78
79 6
                        $currentNS->rawAddToValue
80
                        (
81 6
                            'useStatements',
82 6
                            new UseStatement($classname, $as)
83
                        );
84 6
                        $matching = null;
85 6
                        break;
86
                }
87 6
                continue;
88
            }
89
90 6
            if ($matching === T_AS)
91
            {
92 5
                if ($token[0] === T_STRING)
93
                {
94 5
                    $as .= $token[1];
95
                }
96
            }
97 6
            elseif ($matching)
98
            {
99 6
                switch ($token[0])
100
                {
101 6
                    case T_STRING:
102 6
                    case T_NS_SEPARATOR:
103 6
                        $classname .= $token[1];
104 6
                        break;
105 6
                    case T_AS:
106 6
                        $matching = T_AS;
107
                }
108
            }
109
            else
110
            {
111 6
                switch ($token[0])
112
                {
113 6
                    case T_NAMESPACE:
114 6
                    case T_USE:
115 6
                        $as = '';
116 6
                        $classname = '';
117 6
                        $matching = $token[0];
118
                }
119
            }
120
        }
121 6
    }
122
}
123