Completed
Push — master ( 0b755b...d8f6c8 )
by Дмитрий
07:19
created

ClassDefinition::addProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Definition;
7
8
use PHPSA\CompiledExpression;
9
use PHPSA\Context;
10
use PhpParser\Node;
11
use PHPSA\Variable;
12
13
/**
14
 * Class ClassDefinition
15
 * @package PHPSA\Definition
16
 */
17
class ClassDefinition extends ParentDefinition
18
{
19
    /**
20
     * @var int
21
     */
22
    protected $type;
23
24
    /**
25
     * Class methods
26
     *
27
     * @var ClassMethod[]
28
     */
29
    protected $methods = array();
30
31
    /**
32
     * Class properties
33
     *
34
     * @var Node\Stmt\Property[]
35
     */
36
    protected $properties = array();
37
38
    /**
39
     * Class constants
40
     *
41
     * @var Node\Stmt\Const_[]
42
     */
43
    protected $constants = array();
44
45
    /**
46
     * @todo Use Finder
47
     *
48
     * @var string
49
     */
50
    protected $filepath;
51
52
    /**
53
     * @var string|null
54
     */
55
    protected $extendsClass;
56
57
    /**
58
     * @var ClassDefinition|null
59
     */
60
    protected $extendsClassDefinition;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $extendsClassDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
61
62
    /**
63
     * @var array
64
     */
65
    protected $interfaces = array();
66
67
    /**
68
     * @param string $name
69
     * @param integer $type
70
     */
71 393
    public function __construct($name, $type)
72
    {
73 393
        $this->name = $name;
74 393
        $this->type = $type;
75 393
    }
76
77
    /**
78
     * @param ClassMethod $methodDefintion
79
     */
80 15
    public function addMethod(ClassMethod $methodDefintion)
81
    {
82 15
        $this->methods[$methodDefintion->getName()] = $methodDefintion;
83 15
    }
84
85
    /**
86
     * @param Node\Stmt\Property $property
87
     */
88 2
    public function addProperty(Node\Stmt\Property $property)
89
    {
90 2
        foreach ($property->props as $propertyDefinition) {
91 2
            $this->properties[$propertyDefinition->name] = $propertyDefinition;
92 2
        }
93 2
    }
94
95
    /**
96
     * @param Node\Stmt\ClassConst $const
97
     */
98 2
    public function addConst(Node\Stmt\ClassConst $const)
99
    {
100 2
        $this->constants[$const->consts[0]->name] = $const;
101 2
    }
102
103
    /**
104
     * @param Context $context
105
     * @return $this
106
     */
107 14
    public function compile(Context $context)
108
    {
109 14
        if ($this->compiled) {
110
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by PHPSA\Definition\ClassDefinition::compile of type PHPSA\Definition\ClassDefinition.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
111
        }
112
113 14
        $this->compiled = true;
114 14
        $context->setFilepath($this->filepath);
115 14
        $context->setScope($this);
116
117 14
        foreach ($this->methods as $method) {
118 14
            $context->clearSymbols();
119
120 14
            if (!$method->isStatic()) {
121 14
                $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT);
122 14
                $thisPtr->incGets();
123 14
                $context->addVariable($thisPtr);
124 14
            }
125
126 14
            $method->compile($context);
127
128 14
            $symbols = $context->getSymbols();
129 14
            if (count($symbols) > 0) {
130 14
                foreach ($symbols as $name => $variable) {
131 14
                    if ($variable->isUnused()) {
132 1
                        $context->warning(
133 1
                            'unused-' . $variable->getSymbolType(),
134 1
                            sprintf(
135 1
                                'Unused ' . $variable->getSymbolType() . ' $%s in method %s()',
136 1
                                $variable->getName(),
137 1
                                $method->getName()
138 1
                            )
139 1
                        );
140 1
                    }
141 14
                }
142 14
            }
143 14
        }
144
145 14
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (PHPSA\Definition\ClassDefinition) is incompatible with the return type declared by the abstract method PHPSA\Definition\AbstractDefinition::compile of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
146
    }
147
148
    /**
149
     * @param string $name
150
     * @param boolean|false $inherit
151
     * @return bool
152
     */
153 1
    public function hasMethod($name, $inherit = false)
154
    {
155 1
        if (isset($this->methods[$name])) {
156 1
            return true;
157
        }
158
159 1
        if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, $inherit)) {
160
            $method = $this->extendsClassDefinition->getMethod($name, $inherit);
161
            return $method && ($method->isPublic() || $method->isProtected());
162
        }
163
164 1
        return false;
165
    }
166
167
    /**
168
     * @param string $name
169
     * @param bool $inherit
170
     * @return bool
171
     */
172 2
    public function hasConst($name, $inherit = false)
173
    {
174 2
        if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasConst($name, $inherit)) {
175 1
            return true;
176
        }
177
178 2
        return isset($this->constants[$name]);
179
    }
180
181
    /**
182
     * @param $name
183
     * @param boolean|false $inherit
184
     * @return ClassMethod|null
185
     */
186 1
    public function getMethod($name, $inherit = false)
187
    {
188 1
        if (isset($this->methods[$name])) {
189 1
            return $this->methods[$name];
190
        }
191
192
        if ($inherit && $this->extendsClassDefinition) {
193
            return $this->extendsClassDefinition->getMethod($name, $inherit);
194
        }
195
196
        return null;
197
    }
198
199
    /**
200
     * @param $name
201
     * @param bool $inherit
202
     * @return bool
203
     */
204 1
    public function hasProperty($name, $inherit = false)
205
    {
206 1
        if (isset($this->properties[$name])) {
207 1
            return isset($this->properties[$name]);
208
        }
209
210 1
        return $inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasProperty($name, true);
211
    }
212
213
    /**
214
     * @param string $name
215
     * @param bool $inherit
216
     * @return Node\Stmt\Property
217
     */
218
    public function getProperty($name, $inherit = false)
219
    {
220
        assert($this->hasProperty($name, $inherit));
221
222
        if (isset($this->properties[$name])) {
223
            return $this->properties[$name];
224
        }
225
226
        if ($inherit && $this->extendsClassDefinition) {
227
            return $this->extendsClassDefinition->getProperty($name, true);
228
        }
229
230
        return null;
231
    }
232
233
    /**
234
     * @return string
235
     */
236 14
    public function getFilepath()
237
    {
238 14
        return $this->filepath;
239
    }
240
241
    /**
242
     * @param string $filepath
243
     */
244 14
    public function setFilepath($filepath)
245
    {
246 14
        $this->filepath = $filepath;
247 14
    }
248
249
    /**
250
     * @return bool
251
     */
252
    public function isAbstract()
253
    {
254
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT);
255
    }
256
257
    /**
258
     * @param null|string $extendsClass
259
     */
260
    public function setExtendsClass($extendsClass)
261
    {
262
        $this->extendsClass = $extendsClass;
263
    }
264
265
    /**
266
     * @return null|ClassDefinition
267
     */
268
    public function getExtendsClassDefinition()
269
    {
270
        return $this->extendsClassDefinition;
271
    }
272
273
    /**
274
     * @param ClassDefinition $extendsClassDefinition
275
     */
276 1
    public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $extendsClassDefinition exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
277
    {
278 1
        $this->extendsClassDefinition = $extendsClassDefinition;
279 1
    }
280
281
    /**
282
     * @param array $interface
283
     */
284
    public function addInterface($interface)
285
    {
286
        $this->interfaces[] = $interface;
287
    }
288
289
    /**
290
     * @return null|string
291
     */
292 14
    public function getExtendsClass()
293
    {
294 14
        return $this->extendsClass;
295
    }
296
}
297