Completed
Pull Request — master (#88)
by Kévin
02:57
created

RuntimeClassDefinition::getExtendsClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
c 1
b 0
f 1
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Definition;
7
8
use PHPSA\Context;
9
use PHPSA\Exception\NotImplementedException;
10
use ReflectionClass;
11
12
class RuntimeClassDefinition extends ClassDefinition
13
{
14
    /**
15
     * @var ReflectionClass
16
     */
17
    protected $reflection;
18
19
    /**
20
     * @param ReflectionClass $reflection
21
     */
22 5
    public function __construct(ReflectionClass $reflection)
23
    {
24 5
        $this->reflection = $reflection;
25 5
    }
26
27
    /**
28
     * @param Context $context
29
     * @return $this
30
     */
31 1
    public function compile(Context $context)
32
    {
33 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (PHPSA\Definition\RuntimeClassDefinition) 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...
34
    }
35
36
    /**
37
     * @param string $name
38
     * @param boolean|false $inherit
39
     * @return bool
40
     */
41 1
    public function hasMethod($name, $inherit = false)
42
    {
43 1
        return $this->reflection->hasMethod($name);
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param bool $inherit
49
     * @return bool
50
     */
51 2
    public function hasConst($name, $inherit = false)
52
    {
53 2
        if (!$this->reflection->hasConstant($name)) {
54 1
            return false;
55
        }
56
57 1
        if ($inherit) {
58 1
            return true;
59
        }
60
61 1
        $parent = $this->reflection->getParentClass();
62 1
        if (!$parent) {
63
            return true;
64
        }
65
66 1
        return !$parent->hasConstant($name);
67
    }
68
69
    /**
70
     * @param $name
71
     * @param boolean|false $inherit
72
     * @return ReflectionClassMethod
73
     */
74
    public function getMethod($name, $inherit = false)
75
    {
76
        return new ReflectionClassMethod($this->reflection->getMethod($name));
77
    }
78
79
    /**
80
     * @param $name
81
     * @param bool $inherit
82
     * @return bool
83
     */
84 1
    public function hasProperty($name, $inherit = false)
85
    {
86 1
        return $this->reflection->hasProperty($name);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getFilepath()
93
    {
94
        throw new NotImplementedException(__FUNCTION__);
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function isAbstract()
101
    {
102
        throw new NotImplementedException(__FUNCTION__);
103
    }
104
105
    /**
106
     * @return null|ClassDefinition
107
     */
108
    public function getExtendsClassDefinition()
109
    {
110
        $parentReflection = $this->reflection->getParentClass();
111
112
        if (!$parentReflection) {
113
            return null;
114
        }
115
116
        return new static($parentReflection);
117
    }
118
119
    /**
120
     * @return null|string
121
     */
122
    public function getExtendsClass()
123
    {
124
        $parentReflection = $this->reflection->getParentClass();
125
126
        if (!$parentReflection) {
127
            return null;
128
        }
129
130
        return $parentReflection->getName();
131
    }
132
}
133