Completed
Push — php7 ( ab40a7 )
by personal
04:06
created

ReflectedClass::setNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\OOP\Reflected;
11
use Hal\Component\OOP\Resolver\NameResolver;
12
13
14
/**
15
 * Result (class)
16
 *
17
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
18
 */
19
class ReflectedClass {
20
21
    /**
22
     * @var string
23
     */
24
    protected $namespace;
25
26
    /**
27
     * @var string
28
     */
29
    protected $name;
30
31
    /**
32
     * Methods
33
     *
34
     * @var \SplObjectStorage[]
35
     */
36
    protected $methods;
37
38
    /**
39
     * Resolver for names
40
     *
41
     * @var NameResolver
42
     */
43
    protected $nameResolver;
44
45
    /**
46
     * Does the class is abstract ?
47
     *
48
     * @var bool
49
     */
50
    protected $isAbstract = false;
51
52
    /**
53
     * @var array
54
     */
55
    protected $interfaces = array();
56
57
    /**
58
     * Parent's name
59
     *
60
     * @var string
61
     */
62
    private $parent;
63
64
    /**
65
     * Constructor
66
     *
67
     * @param string $namespace
68
     * @param string $name
69
     */
70
    public function __construct($namespace, $name)
71
    {
72
        $this->name = (string) $name;
73
        $this->namespace = (string) $namespace;
74
        $this->methods = array();
75
        $this->nameResolver = new NameResolver();
76
    }
77
78
    /**
79
     * Get fullname (namespace + name)
80
     *
81
     * @return string
82
     */
83
    public function getFullname() {
84
        return $this->getNamespace().'\\'.$this->getName();
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getName()
91
    {
92
        return $this->name;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getNamespace()
99
    {
100
        return rtrim($this->namespace, '\\');
101
    }
102
103
    /**
104
     * @param string $namespace
105
     * @return ReflectedClass
106
     */
107
    protected function setNamespace($namespace)
108
    {
109
        $this->namespace = (string) $namespace;
110
        return $this;
111
    }
112
113
    /**
114
     * @return \SplObjectStorage[]
115
     */
116
    public function getMethods()
117
    {
118
        return $this->methods;
119
    }
120
121
    /**
122
     * Attach method
123
     * This method consolidated dependencies
124
     *
125
     * @param ReflectedMethod $method
126
     * @return $this
127
     */
128
    public function pushMethod(ReflectedMethod $method) {
129
        $this->methods[$method->getName()] = $method;
130
        return $this;
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    public function getDependencies()
137
    {
138
        $dependencies = array();
139
        foreach($this->getMethods() as $method) {
140
            $dependencies = array_merge($dependencies, $method->getDependencies());
141
        }
142
        foreach($dependencies as &$name) {
143
                $name = $this->nameResolver->resolve($name, $this->namespace);
144
        }
145
        return array_unique($dependencies);
146
    }
147
148
    /**
149
     * @param NameResolver $resolver
150
     * @return $this
151
     */
152
    public function setNameResolver(NameResolver $resolver)
153
    {
154
        $this->nameResolver = $resolver;
155
        return $this;
156
    }
157
158
    /**
159
     * Set abstractness of method
160
     *
161
     * @param boolean $bool
162
     * @return $this
163
     */
164
    public function setAbstract($bool) {
165
        $this->isAbstract = (bool) $bool;
166
        return $this;
167
    }
168
169
    /**
170
     * Is Abstract ?
171
     *
172
     * @return bool
173
     */
174
    public function isAbstract() {
175
        return $this->isAbstract;
176
    }
177
178
    /**
179
     * Set the parent name
180
     *
181
     * @param $parent
182
     * @return $this
183
     */
184
    public function setParent($parent) {
185
        $this->parent = $parent;
186
        return $this;
187
    }
188
189
    /**
190
     * Get the parent name
191
     *
192
     * @return string|null null when no parent class exists
193
     */
194
    public function getParent() {
195
        if ($this->parent === null) {
196
            return null;
197
        }
198
        return $this->nameResolver->resolve($this->parent, $this->namespace);
199
    }
200
201
    /**
202
     * @return array
203
     */
204
    public function getInterfaces()
205
    {
206
        $resolvedInterfaces = array();
207
        foreach($this->interfaces as $interface) {
208
            array_push($resolvedInterfaces, $this->nameResolver->resolve($interface, $this->namespace));
209
        }
210
        return $resolvedInterfaces;
211
    }
212
213
    /**
214
     * @param array $interfaces
215
     * @return $this
216
     */
217
    public function setInterfaces($interfaces)
218
    {
219
        $this->interfaces = $interfaces;
220
        return $this;
221
    }
222
223
    /**
224
     * Get anonymous classes contained in this class
225
     *
226
     * @return mixed
227
     */
228
    public function getAnonymousClasses() {
229
        $result = array();
230
        foreach($this->methods as $method) {
231
            $result = array_merge($result, $method->getAnonymousClasses());
232
        }
233
        return $result;
234
    }
235
236
};