Completed
Push — master ( 19c440...922200 )
by Дмитрий
06:07 queued 02:02
created

RuntimeClassDefinition::hasMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
        // NOTE: ReflectionClass::hasConstant also checks parent classes, so if $inherit is true, the job is already done.
58 1
        if ($inherit) {
59 1
            return true;
60
        }
61
62
        // but if it's not, we need to make sure that the constant is defined only in the current class. It means that
63
        // we have to check that it has no parent or that the parent does not define the constant.
64 1
        $parent = $this->reflection->getParentClass();
65 1
        if (!$parent) {
66
            return true;
67
        }
68
69 1
        return !$parent->hasConstant($name);
70
    }
71
72
    /**
73
     * @param $name
74
     * @param boolean|false $inherit
75
     * @return ReflectionClassMethod
76
     */
77
    public function getMethod($name, $inherit = false)
78
    {
79
        return new ReflectionClassMethod($this->reflection->getMethod($name));
80
    }
81
82
    /**
83
     * @param $name
84
     * @param bool $inherit
85
     * @return bool
86
     */
87 1
    public function hasProperty($name, $inherit = false)
88
    {
89 1
        return $this->reflection->hasProperty($name);
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getFilepath()
96
    {
97
        throw new NotImplementedException(__FUNCTION__);
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function isAbstract()
104
    {
105
        throw new NotImplementedException(__FUNCTION__);
106
    }
107
108
    /**
109
     * @return null|ClassDefinition
110
     */
111
    public function getExtendsClassDefinition()
112
    {
113
        $parentReflection = $this->reflection->getParentClass();
114
115
        if (!$parentReflection) {
116
            return null;
117
        }
118
119
        return new static($parentReflection);
120
    }
121
122
    /**
123
     * @return null|string
124
     */
125
    public function getExtendsClass()
126
    {
127
        $parentReflection = $this->reflection->getParentClass();
128
129
        if (!$parentReflection) {
130
            return null;
131
        }
132
133
        return $parentReflection->getName();
134
    }
135
}
136