Completed
Push — master ( 887cf7...2ea773 )
by Emily
02:13
created

ReflectionFileFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 119
ccs 36
cts 54
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A build() 0 6 1
C parseFile() 0 77 15
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 1
    public function __construct(string $filename)
46
    {
47 1
        $this->filename = $filename;
48 1
        $this->object = new ReflectionFile();
49 1
        $this->accessor = new RawPropertyAccessor($this->object);
50 1
    }
51
52
    /**
53
     * Builds the ReflectionFile from the provided parameters
54
     *
55
     * @return ReflectionFile
56
     */
57 1
    public function build()
58
    {
59 1
        $this->parseFile();
60
61
        return $this->object;
62
    }
63
64
    /**
65
     * Parses a file to obtain its namespace and use declarations
66
     */
67 1
    private function parseFile()
68
    {
69 1
        $tokens = token_get_all(file_get_contents($this->filename));
70
71 1
        $matching = null;
72 1
        $classname = '';
73 1
        $as = '';
74 1
        $currentNS = null;
75
76 1
        foreach ($tokens as $token)
77
        {
78 1
            if ($token === ';')
79
            {
80
                switch ($matching)
81
                {
82 1
                    case T_NAMESPACE:
83 1
                        $ns = new NamespaceBlock($classname);
84 1
                        $currentNS = new RawPropertyAccessor($ns);
85 1
                        $this->accessor->getRawValue
86
                        (
87 1
                            'namespaces'
88
                        )
89 1
                        ->add($classname, $ns);
90
                        $currentNS->setRawValue('file', $this->object);
91
                        $matching = null;
92
                        break;
93
                    case T_AS:
94
                    case T_USE:
95
                        if (!$as)
96
                        {
97
                            $as = explode('\\', $classname);
98
                            $as = end($as);
99
                        }
100
101
                        $currentNS->getRawValue
102
                        (
103
                            'useStatements'
104
                        )
105
                        ->add($as, new UseStatement($classname, $as));
106
                        $matching = null;
107
                        break;
108
                }
109
                continue;
110
            }
111
112 1
            if ($matching === T_AS)
113
            {
114
                if ($token[0] === T_STRING)
115
                {
116
                    $as .= $token[1];
117
                }
118
            }
119 1
            elseif ($matching)
120
            {
121 1
                switch ($token[0])
122
                {
123 1
                    case T_STRING:
124 1
                    case T_NS_SEPARATOR:
125 1
                        $classname .= $token[1];
126 1
                        break;
127 1
                    case T_AS:
128 1
                        $matching = T_AS;
129
                }
130
            }
131
            else
132
            {
133 1
                switch ($token[0])
134
                {
135 1
                    case T_NAMESPACE:
136 1
                    case T_USE:
137 1
                        $as = '';
138 1
                        $classname = '';
139 1
                        $matching = $token[0];
140
                }
141
            }
142
        }
143
    }
144
}
145