Completed
Pull Request — master (#157)
by Kévin
03:46
created

RuntimeInterfaceDefinition::hasConst()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 20
ccs 0
cts 9
cp 0
crap 20
rs 9.2
c 1
b 0
f 0
1
<?php
2
/**
3
 * @author Kévin Gomez https://github.com/K-Phoen <[email protected]>
4
 */
5
6
namespace PHPSA\Definition;
7
8
use PHPSA\Context;
9
use PHPSA\Exception\NotImplementedException;
10
use ReflectionClass;
11
12
class RuntimeInterfaceDefinition extends InterfaceDefinition
13
{
14
    /**
15
     * @var ReflectionClass
16
     */
17
    protected $reflection;
18
19
    /**
20
     * @param ReflectionClass $reflection
21
     */
22
    public function __construct(ReflectionClass $reflection)
23
    {
24
        parent::__construct($reflection->getName());
0 ignored issues
show
Bug introduced by
Consider using $reflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
25
26
        $this->reflection = $reflection;
27
    }
28
29
    /**
30
     * @param Context $context
31
     * @return $this
32
     */
33
    public function compile(Context $context)
34
    {
35
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (PHPSA\Definition\RuntimeInterfaceDefinition) 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...
36
    }
37
38
    /**
39
     * @param string $name
40
     * @param boolean|false $inherit
41
     * @return bool
42
     */
43
    public function hasMethod($name, $inherit = false)
44
    {
45
        return $this->reflection->hasMethod($name);
46
    }
47
48
    /**
49
     * @param string $name
50
     * @param bool $inherit
51
     * @return bool
52
     */
53
    public function hasConst($name, $inherit = false)
54
    {
55
        if (!$this->reflection->hasConstant($name)) {
56
            return false;
57
        }
58
59
        // NOTE: ReflectionClass::hasConstant also checks parent classes, so if $inherit is true, the job is already done.
60
        if ($inherit) {
61
            return true;
62
        }
63
64
        // but if it's not, we need to make sure that the constant is defined only in the current class. It means that
65
        // we have to check that it has no parent or that the parent does not define the constant.
66
        $parent = $this->reflection->getParentClass();
67
        if (!$parent) {
68
            return true;
69
        }
70
71
        return !$parent->hasConstant($name);
72
    }
73
74
    /**
75
     * @param $name
76
     * @param boolean|false $inherit
77
     * @return ReflectionClassMethod
78
     */
79
    public function getMethod($name, $inherit = false)
80
    {
81
        return new ReflectionClassMethod($this->reflection->getMethod($name));
82
    }
83
84
    /**
85
     * @param $name
86
     * @param bool $inherit
87
     * @return bool
88
     */
89
    public function hasProperty($name, $inherit = false)
0 ignored issues
show
Unused Code introduced by
The parameter $inherit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        return $this->reflection->hasProperty($name);
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getFilepath()
98
    {
99
        throw new NotImplementedException(__FUNCTION__);
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function isAbstract()
106
    {
107
        throw new NotImplementedException(__FUNCTION__);
108
    }
109
}
110