Completed
Push — develop ( 9dbd4b...3bc77a )
by Jaap
16s
created

PropertyDescriptor::getType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 12
ccs 4
cts 4
cp 1
crap 3
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Descriptor;
17
18
use phpDocumentor\Descriptor\Tag\VarDescriptor;
19
use phpDocumentor\Reflection\Type;
20
21
/**
22
 * Descriptor representing a property.
23
 */
24
class PropertyDescriptor extends DescriptorAbstract implements Interfaces\PropertyInterface, Interfaces\VisibilityInterface
25
{
26
    /** @var ClassDescriptor|TraitDescriptor $parent */
27
    protected $parent;
28
29
    /** @var string[]|null $types */
30
    protected $types;
31
32
    /** @var string $default */
33
    protected $default;
34
35
    /** @var bool $static */
36
    protected $static = false;
37
38
    /** @var string $visibility */
39
    protected $visibility = 'public';
40
41
    /**
42
     * @param ClassDescriptor|TraitDescriptor $parent
43
     */
44 2
    public function setParent($parent)
45
    {
46 2
        $this->setFullyQualifiedStructuralElementName(
47 2
            $parent->getFullyQualifiedStructuralElementName() . '::$' . $this->getName()
48
        );
49
50 2
        $this->parent = $parent;
51 2
    }
52
53
    /**
54
     * @return ClassDescriptor|TraitDescriptor
55
     */
56 1
    public function getParent()
57
    {
58 1
        return $this->parent;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 1
    public function setDefault($default)
65
    {
66 1
        $this->default = $default;
67 1
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72 1
    public function getDefault()
73
    {
74 1
        return $this->default;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 1
    public function setStatic($static)
81
    {
82 1
        $this->static = $static;
83 1
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 1
    public function isStatic()
89
    {
90 1
        return $this->static;
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96 1
    public function setTypes(Type $types)
97
    {
98 1
        $this->types = $types;
0 ignored issues
show
Documentation Bug introduced by
It seems like $types of type object<phpDocumentor\Reflection\Type> is incompatible with the declared type array<integer,string>|null 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...
99 1
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104 2
    public function getTypes()
105
    {
106 2
        return [$this->getType()];
0 ignored issues
show
Best Practice introduced by
The expression return array($this->getType()); seems to be an array, but some of its elements' types (phpDocumentor\Reflection\Type|string[]) are incompatible with the return type declared by the interface phpDocumentor\Descriptor...ertyInterface::getTypes of type string[].

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
107 2
    }
108
109
    public function getType()
110 2
    {
111 2
        if ($this->types === null) {
112 1
            /** @var VarDescriptor $var */
113
            $var = $this->getVar()->getIterator()->current();
114
            if ($var) {
115
                return $var->getType();
116 2
            }
117
        }
118
119
        return $this->types;
120
    }
121
122 1
    /**
123
     * {@inheritDoc}
124 1
     */
125 1
    public function setVisibility($visibility)
126
    {
127
        $this->visibility = $visibility;
128
    }
129
130 1
    /**
131
     * {@inheritDoc}
132 1
     */
133
    public function getVisibility()
134
    {
135
        return $this->visibility;
136
    }
137
138 2
    /**
139
     * @return Collection
140
     */
141 2
    public function getVar()
142 2
    {
143 1
        /** @var Collection $var */
144
        $var = $this->getTags()->get('var', new Collection());
145
        if ($var->count() !== 0) {
146 2
            return $var;
147 2
        }
148 1
149
        $inheritedElement = $this->getInheritedElement();
150
        if ($inheritedElement) {
151 1
            return $inheritedElement->getVar();
152
        }
153
154
        return new Collection();
155
    }
156
157
    /**
158
     * Returns the file associated with the parent class or trait.
159 1
     *
160
     * @return FileDescriptor
161 1
     */
162
    public function getFile()
163
    {
164
        return $this->getParent()->getFile();
165
    }
166
167
    /**
168
     * Returns the property from which this one should inherit, if any.
169 2
     *
170
     * @return PropertyDescriptor|null
171
     */
172 2
    public function getInheritedElement()
173
    {
174 2
        /** @var ClassDescriptor|InterfaceDescriptor|null $associatedClass */
175 1
        $associatedClass = $this->getParent();
176 2
177
        if (($associatedClass instanceof ClassDescriptor || $associatedClass instanceof InterfaceDescriptor)
178
            && ($associatedClass->getParent() instanceof ClassDescriptor
179
                || $associatedClass->getParent() instanceof InterfaceDescriptor
180 1
            )
181
        ) {
182 1
            /** @var ClassDescriptor|InterfaceDescriptor $parentClass */
183
            $parentClass = $associatedClass->getParent();
184
185 1
            return $parentClass->getProperties()->get($this->getName());
0 ignored issues
show
Bug introduced by
The method getProperties does only exist in phpDocumentor\Descriptor\ClassDescriptor, but not in phpDocumentor\Descriptor\InterfaceDescriptor.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
186
        }
187
188
        return null;
189
    }
190
}
191