Completed
Push — master ( 296f07...0da13b )
by Дмитрий
07:54
created

ClassDefinition   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 8

Test Coverage

Coverage 25%

Importance

Changes 15
Bugs 2 Features 2
Metric Value
wmc 39
c 15
b 2
f 2
lcom 4
cbo 8
dl 0
loc 269
ccs 22
cts 88
cp 0.25
rs 8.2857

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addMethod() 0 4 1
A addProperty() 0 4 1
A addConst() 0 4 1
C compile() 0 40 7
A hasConst() 0 4 1
B hasMethod() 0 13 7
A getMethod() 0 12 4
A hasProperty() 0 8 4
A getProperty() 0 10 4
A getFilepath() 0 4 1
A setFilepath() 0 4 1
A isAbstract() 0 4 1
A setExtendsClass() 0 4 1
A getExtendsClassDefinition() 0 4 1
A setExtendsClassDefinition() 0 4 1
A addInterface() 0 4 1
A getExtendsClass() 0 4 1
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 370
    public function __construct($name, $type)
72
    {
73 370
        $this->name = $name;
74 370
        $this->type = $type;
75 370
    }
76
77
    /**
78
     * @param ClassMethod $methodDefintion
79
     */
80 1
    public function addMethod(ClassMethod $methodDefintion)
81
    {
82 1
        $this->methods[$methodDefintion->getName()] = $methodDefintion;
83 1
    }
84
85
    /**
86
     * @param Node\Stmt\Property $property
87
     */
88 1
    public function addProperty(Node\Stmt\Property $property)
89
    {
90 1
        $this->properties[$property->props[0]->name] = $property;
91 1
    }
92
93
    /**
94
     * @param Node\Stmt\ClassConst $const
95
     */
96
    public function addConst(Node\Stmt\ClassConst $const)
97
    {
98
        $this->constants[$const->consts[0]->name] = $const;
99
    }
100
101
    /**
102
     * @param Context $context
103
     * @return $this
104
     */
105
    public function compile(Context $context)
106
    {
107
        if ($this->compiled) {
108
            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...
109
        }
110
111
        $this->compiled = true;
112
        $context->setFilepath($this->filepath);
113
        $context->setScope($this);
114
115
        foreach ($this->methods as $method) {
116
            $context->clearSymbols();
117
118
            if (!$method->isStatic()) {
119
                $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT);
120
                $thisPtr->incGets();
121
                $context->addVariable($thisPtr);
122
            }
123
124
            $method->compile($context);
125
126
            $symbols = $context->getSymbols();
127
            if (count($symbols) > 0) {
128
                foreach ($symbols as $name => $variable) {
129
                    if ($variable->isUnused()) {
130
                        $context->warning(
131
                            'unused-' . $variable->getSymbolType(),
132
                            sprintf(
133
                                'Unused ' . $variable->getSymbolType() . ' $%s in method %s()',
134
                                $variable->getName(),
135
                                $method->getName()
136
                            )
137
                        );
138
                    }
139
                }
140
            }
141
        }
142
143
        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...
144
    }
145
146
    /**
147
     * @param string $name
148
     * @param boolean|false $inherit
149
     * @return bool
150
     */
151 1
    public function hasMethod($name, $inherit = false)
152
    {
153 1
        if (isset($this->methods[$name])) {
154 1
            return true;
155
        }
156
157 1
        if ($inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasMethod($name, true)) {
158
            $method = $this->extendsClassDefinition->getMethod($name);
159
            return $method && ($method->isPublic() || $method->isProtected());
160
        }
161
162 1
        return false;
163
    }
164
165
    /**
166
     * @param $name
167
     * @return bool
168
     */
169
    public function hasConst($name)
170
    {
171
        return isset($this->constants[$name]);
172
    }
173
174
    /**
175
     * @param $name
176
     * @param boolean|false $inherit
177
     * @return ClassMethod|null
178
     */
179 1
    public function getMethod($name, $inherit = false)
180
    {
181 1
        if (isset($this->methods[$name])) {
182 1
            return $this->methods[$name];
183
        }
184
185
        if ($inherit && $this->extendsClassDefinition) {
186
            return $this->extendsClassDefinition->getMethod($name, true);
187
        }
188
189
        return null;
190
    }
191
192
    /**
193
     * @param $name
194
     * @param bool $inherit
195
     * @return bool
196
     */
197 1
    public function hasProperty($name, $inherit = false)
198
    {
199 1
        if (isset($this->properties[$name])) {
200 1
            return isset($this->properties[$name]);
201
        }
202
203 1
        return $inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->hasProperty($name, true);
204
    }
205
206
    /**
207
     * @param $name
208
     * @param bool $inherit
209
     * @return Node\Stmt\Property
210
     */
211
    public function getProperty($name, $inherit = false)
212
    {
213
        assert($this->hasProperty($name, $inherit));
214
215
        if (isset($this->properties[$name])) {
216
            return $this->properties[$name];
217
        }
218
219
        return $inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->getProperty($name, true);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $inherit && $this...tProperty($name, true); (boolean) is incompatible with the return type documented by PHPSA\Definition\ClassDefinition::getProperty of type PhpParser\Node\Stmt\Property.

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...
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    public function getFilepath()
226
    {
227
        return $this->filepath;
228
    }
229
230
    /**
231
     * @param string $filepath
232
     */
233
    public function setFilepath($filepath)
234
    {
235
        $this->filepath = $filepath;
236
    }
237
238
    /**
239
     * @return bool
240
     */
241
    public function isAbstract()
242
    {
243
        return (bool) ($this->type & Node\Stmt\Class_::MODIFIER_ABSTRACT);
244
    }
245
246
    /**
247
     * @param null|string $extendsClass
248
     */
249
    public function setExtendsClass($extendsClass)
250
    {
251
        $this->extendsClass = $extendsClass;
252
    }
253
254
    /**
255
     * @return null|ClassDefinition
256
     */
257
    public function getExtendsClassDefinition()
258
    {
259
        return $this->extendsClassDefinition;
260
    }
261
262
    /**
263
     * @param ClassDefinition $extendsClassDefinition
264
     */
265
    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...
266
    {
267
        $this->extendsClassDefinition = $extendsClassDefinition;
268
    }
269
270
    /**
271
     * @param array $interface
272
     */
273
    public function addInterface($interface)
274
    {
275
        $this->interfaces[] = $interface;
276
    }
277
278
    /**
279
     * @return null|string
280
     */
281
    public function getExtendsClass()
282
    {
283
        return $this->extendsClass;
284
    }
285
}
286