Completed
Push — develop ( 455a01...67be7e )
by Jaap
10:31 queued 52s
created

ArgumentDescriptor::getTypes()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 5
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 9
rs 8.8571
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 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
namespace phpDocumentor\Descriptor;
12
13
/**
14
 * Descriptor representing a single Argument of a method or function.
15
 */
16
class ArgumentDescriptor extends DescriptorAbstract implements Interfaces\ArgumentInterface
17
{
18
    /** @var MethodDescriptor $method */
19
    protected $method;
20
21
    /** @var string[] $type an array of normalized types that should be in this Argument */
22
    protected $types = array();
23
24
    /** @var string|null $default the default value for an argument or null if none is provided */
25
    protected $default;
26
27
    /** @var bool $byReference whether the argument passes the parameter by reference instead of by value */
28
    protected $byReference = false;
29
30
    /** @var boolean Determines if this Argument represents a variadic argument */
31
    protected $isVariadic = false;
32
33
    /**
34
     * To which method does this argument belong to
35
     *
36
     * @param MethodDescriptor $method
37
     */
38
    public function setMethod(MethodDescriptor $method)
39
    {
40
        $this->method = $method;
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function setTypes($types)
47
    {
48
        $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...
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function getTypes()
55
    {
56
        $countable = $this->types instanceof \Countable || is_array($this->types);
57
        if ((!$countable || count($this->types) == 0) && $this->getInheritedElement() !== null) {
58
            $this->setTypes($this->getInheritedElement()->getTypes());
0 ignored issues
show
Documentation introduced by
$this->getInheritedElement()->getTypes() is of type array, but the function expects a object<phpDocumentor\Descriptor\Collection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

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