Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

phpDocumentor/Descriptor/ArgumentDescriptor.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Descriptor;
13
14
/**
15
 * Descriptor representing a single Argument of a method or function.
16
 */
17
class ArgumentDescriptor extends DescriptorAbstract implements Interfaces\ArgumentInterface
18
{
19
    /** @var MethodDescriptor $method */
20
    protected $method;
21
22
    /** @var string[] $type an array of normalized types that should be in this Argument */
23
    protected $types = [];
24
25
    /** @var string|null $default the default value for an argument or null if none is provided */
26
    protected $default;
27
28
    /** @var bool $byReference whether the argument passes the parameter by reference instead of by value */
29
    protected $byReference = false;
30
31
    /** @var boolean Determines if this Argument represents a variadic argument */
32
    protected $isVariadic = false;
33
34
    /**
35
     * To which method does this argument belong to
36
     */
37 1
    public function setMethod(MethodDescriptor $method)
38
    {
39 1
        $this->method = $method;
40 1
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 1
    public function setTypes($types)
46
    {
47 1
        $this->types = $types;
0 ignored issues
show
Documentation Bug introduced by
It seems like $types of type object<phpDocumentor\Descriptor\Collection> is incompatible with the declared type array<integer,string> of property $types.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48 1
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 2
    public function getTypes()
54
    {
55 2
        $countable = $this->types instanceof \Countable || is_array($this->types);
56 2
        if ((!$countable || count($this->types) === 0) && $this->getInheritedElement() !== null) {
57 1
            $this->setTypes($this->getInheritedElement()->getTypes());
58
        }
59
60 2
        return $this->types;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->types; (array) is incompatible with the return type declared by the interface phpDocumentor\Descriptor...mentInterface::getTypes of type phpDocumentor\Descriptor\Collection.

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...
61
    }
62
63
    /**
64
     * @return null|ArgumentDescriptor
65
     */
66 1
    public function getInheritedElement()
67
    {
68 1
        if ($this->method instanceof MethodDescriptor &&
69 1
            $this->method->getInheritedElement() instanceof MethodDescriptor) {
70 1
            $parents = $this->method->getInheritedElement()->getArguments();
71 1
            foreach ($parents as $parentArgument) {
72 1
                if ($parentArgument->getName() === $this->getName()) {
73 1
                    return $parentArgument;
74
                }
75
            }
76
        }
77
78 1
        return null;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 1
    public function setDefault($value)
85
    {
86 1
        $this->default = $value;
87 1
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92 1
    public function getDefault()
93
    {
94 1
        return $this->default;
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100 1
    public function setByReference($byReference)
101
    {
102 1
        $this->byReference = $byReference;
103 1
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108 1
    public function isByReference()
109
    {
110 1
        return $this->byReference;
111
    }
112
113
    /**
114
     * Sets whether this argument represents a variadic argument.
115
     *
116
     * @param boolean $isVariadic
117
     *
118
     * @return false
119
     */
120 1
    public function setVariadic($isVariadic)
121
    {
122 1
        $this->isVariadic = $isVariadic;
123 1
    }
124
125
    /**
126
     * Returns whether this argument represents a variadic argument.
127
     *
128
     * @return boolean
129
     */
130 1
    public function isVariadic()
131
    {
132 1
        return $this->isVariadic;
133
    }
134
}
135