Completed
Push — master ( 703b67...f51a8f )
by Emily
02:01
created

ReflectionFileFactory::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Factory\Reflection;
16
17
use Spaark\CompositeUtils\Factory\BaseFactory;
18
use Spaark\CompositeUtils\Model\Reflection\ReflectionFile;
19
use Spaark\CompositeUtils\Model\Reflection\NamespaceBlock;
20
use Spaark\CompositeUtils\Model\Reflection\UseStatement;
21
use Spaark\CompositeUtils\Service\RawPropertyAccessor;
22
23
/**
24
 * Builds a ReflectionFile for a given filename
25
 */
26
class ReflectionFileFactory extends ReflectorFactory
27
{
28
    /**
29
     * The filename to parse
30
     *
31
     * @var string
32
     */
33
    protected $filename;
34
35
    /**
36
     * @var ReflectionFile
37
     */
38
    protected $object;
39
40
    /**
41
     * Creates the ReflectionFileFactory with the given filename
42
     *
43
     * @param string $filename The filename to parse
44
     */
45 20
    public function __construct(string $filename)
46
    {
47 20
        $this->filename = $filename;
48 20
        $this->object = new ReflectionFile();
49 20
        $this->accessor = new RawPropertyAccessor($this->object);
50 20
    }
51
52
    /**
53
     * Builds the ReflectionFile from the provided parameters
54
     *
55
     * @return ReflectionFile
56
     */
57 20
    public function build()
58
    {
59 20
        $this->parseFile();
60
61 20
        return $this->object;
62
    }
63
64
    /**
65
     * Parses a file to obtain its namespace and use declarations
66
     */
67 20
    private function parseFile()
68
    {
69 20
        $tokens = token_get_all(file_get_contents($this->filename));
70
71 20
        $matching = null;
72 20
        $classname = '';
73 20
        $as = '';
74 20
        $currentNS = null;
75
76 20
        foreach ($tokens as $token)
77
        {
78 20
            if ($token === ';')
79
            {
80
                switch ($matching)
81
                {
82 20
                    case T_NAMESPACE:
83 20
                        $ns = new NamespaceBlock($classname);
84 20
                        $currentNS = new RawPropertyAccessor($ns);
85 20
                        $this->accessor->getRawValue
86
                        (
87 20
                            'namespaces'
88
                        )
89 20
                        ->add($classname, $ns);
90 20
                        $currentNS->setRawValue('file', $this->object);
91 20
                        $matching = null;
92 20
                        break;
93 20
                    case T_AS:
94 20
                    case T_USE:
95 20
                        if (!$as)
96
                        {
97 20
                            $as = explode('\\', $classname);
98 20
                            $as = end($as);
99
                        }
100
101 20
                        $currentNS->getRawValue
102
                        (
103 20
                            'useStatements'
104
                        )
105 20
                        ->add($as, new UseStatement($classname, $as));
106 20
                        $matching = null;
107 20
                        break;
108
                }
109 20
                continue;
110
            }
111
112 20
            if ($matching === T_AS)
113
            {
114 17
                if ($token[0] === T_STRING)
115
                {
116 17
                    $as .= $token[1];
117
                }
118
            }
119 20
            elseif ($matching)
120
            {
121 20
                switch ($token[0])
122
                {
123 20
                    case T_STRING:
124 20
                    case T_NS_SEPARATOR:
125 20
                        $classname .= $token[1];
126 20
                        break;
127 20
                    case T_AS:
128 20
                        $matching = T_AS;
129
                }
130
            }
131
            else
132
            {
133 20
                switch ($token[0])
134
                {
135 20
                    case T_NAMESPACE:
136 20
                    case T_USE:
137 20
                        $as = '';
138 20
                        $classname = '';
139 20
                        $matching = $token[0];
140
                }
141
            }
142
        }
143 20
    }
144
}
145