ReflectionClassMethod::isPrivate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
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\CompiledExpression;
9
use PHPSA\Context;
10
use ReflectionMethod;
11
12
/**
13
 * Class Method created from Reflection
14
 */
15
class ReflectionClassMethod extends ClassMethod
16
{
17
    /**
18
     * Return type
19
     *
20
     * @var int
21
     */
22
    protected $returnType = CompiledExpression::VOID;
23
24
    /**
25
     * Array of possible return values
26
     *
27
     * @var array
28
     */
29
    protected $possibleReturnValues = [];
30
31
    /**
32
     * @var ReflectionMethod
33
     */
34
    protected $reflection;
35
36
    /**
37
     * ReflectionClassMethod constructor.
38
     * @param ReflectionMethod $reflection
39
     */
40
    public function __construct(ReflectionMethod $reflection)
41
    {
42
        $this->reflection = $reflection;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getName()
49
    {
50
        return $this->reflection->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->reflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
51
    }
52
53
    /**
54
     * @param Context $context
55
     * @return ReflectionClassMethod
56
     */
57
    public function compile(Context $context)
58
    {
59
        $this->compiled = true;
60
        $context->scopePointer = $this->getPointer();
61
62
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (PHPSA\Definition\ReflectionClassMethod) 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...
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function isAbstract()
69
    {
70
        return $this->reflection->isAbstract();
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    public function isStatic()
77
    {
78
        return $this->reflection->isStatic();
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    public function isPublic()
85
    {
86
        return $this->reflection->isPublic();
87
    }
88
89
    /**
90
     * @return bool
91
     */
92
    public function isProtected()
93
    {
94
        return $this->reflection->isProtected();
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function isPrivate()
101
    {
102
        return $this->reflection->isPrivate();
103
    }
104
}
105