GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ReflectionParameter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 66
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getClass() 0 9 2
A getDeclaringClass() 0 9 2
A getDeclaringFunction() 0 8 2
1
<?php
2
3
namespace Wingu\OctopusCore\Reflection;
4
5
/**
6
 * Reflection about a parameter.
7
 */
8
class ReflectionParameter extends \ReflectionParameter
9
{
10
11
    /**
12
     * Parameter function.
13
     *
14
     * @var mixed
15
     */
16
    private $function;
17
18
    /**
19
     * Constructor.
20
     *
21
     * @param string $function The function to reflect parameters from. It can be also be in the format: array('className', 'methodName').
22
     * @param string $parameter The parameter name.
23
     */
24 18
    public function __construct($function, $parameter)
25
    {
26 18
        $this->function = $function;
27 18
        parent::__construct($function, $parameter);
28 18
    }
29
30
    /**
31
     * Gets a class.
32
     *
33
     * @return \Wingu\OctopusCore\Reflection\ReflectionClass
34
     */
35 3
    public function getClass()
36
    {
37 3
        $class = parent::getClass();
38 3
        if ($class !== null) {
39 3
            $class = new ReflectionClass($class->getName());
40 1
        }
41
42 3
        return $class;
43
    }
44
45
    /**
46
     * Gets the declaring class.
47
     *
48
     * @return \Wingu\OctopusCore\Reflection\ReflectionClass
49
     */
50 6
    public function getDeclaringClass()
51
    {
52 6
        $class = parent::getDeclaringClass();
53 6
        if ($class !== null) {
54 3
            $class = new ReflectionClass($class->getName());
55 1
        }
56
57 6
        return $class;
58
    }
59
60
    /**
61
     * Gets the declaring function.
62
     *
63
     * @return \Wingu\OctopusCore\Reflection\ReflectionMethod|\Wingu\OctopusCore\Reflection\ReflectionFunction
64
     */
65 3
    public function getDeclaringFunction()
66
    {
67 3
        if (is_array($this->function) === true) {
68 3
            return new ReflectionMethod($this->function[0], $this->function[1]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Wingu\Octopu...], $this->function[1]); (Wingu\OctopusCore\Reflection\ReflectionMethod) is incompatible with the return type of the parent method ReflectionParameter::getDeclaringFunction of type ReflectionFunction.

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...
69
        } else {
70 3
            return new ReflectionFunction($this->function);
71
        }
72
    }
73
}
74