Completed
Push — develop ( 2993ce...48a3ec )
by Mike
09:32
created

PropertyDescriptor::getTypes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
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
25
    Interfaces\PropertyInterface,
26
    Interfaces\VisibilityInterface
27
{
28
    /** @var ClassDescriptor|TraitDescriptor $parent */
29
    protected $parent;
30
31
    /** @var Type $type */
32
    protected $type;
33
34
    /** @var string $default */
35
    protected $default;
36
37
    /** @var bool $static */
38
    protected $static = false;
39
40
    /** @var string $visibility */
41
    protected $visibility = 'public';
42
43
    /**
44
     * @param ClassDescriptor|TraitDescriptor $parent
45
     */
46 2
    public function setParent($parent)
47
    {
48 2
        $this->setFullyQualifiedStructuralElementName(
49 2
            $parent->getFullyQualifiedStructuralElementName() . '::$' . $this->getName()
50
        );
51
52 2
        $this->parent = $parent;
53 2
    }
54
55
    /**
56
     * @return ClassDescriptor|TraitDescriptor
57
     */
58 1
    public function getParent()
59
    {
60 1
        return $this->parent;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 1
    public function setDefault($default)
67
    {
68 1
        $this->default = $default;
69 1
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 1
    public function getDefault()
75
    {
76 1
        return $this->default;
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82 1
    public function setStatic($static)
83
    {
84 1
        $this->static = $static;
85 1
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 1
    public function isStatic()
91
    {
92 1
        return $this->static;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98 1
    public function setType(Type $type)
99
    {
100 1
        $this->type = $type;
101 1
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 1
    public function getTypes()
107
    {
108 1
        if ($this->getType() === null) {
109 1
            return [];
110
        }
111
112 1
        return [(string)$this->getType()];
113
    }
114
115 2
    public function getType()
116
    {
117 2
        if ($this->type === null) {
118
            /** @var VarDescriptor $var */
119 2
            $var = $this->getVar()->getIterator()->current();
120 2
            if ($var) {
121 1
                return $var->getType();
122
            }
123
        }
124
125 1
        return $this->type;
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131 1
    public function setVisibility($visibility)
132
    {
133 1
        $this->visibility = $visibility;
134 1
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139 1
    public function getVisibility()
140
    {
141 1
        return $this->visibility;
142
    }
143
144
    /**
145
     * @return Collection
146
     */
147 2
    public function getVar()
148
    {
149
        /** @var Collection $var */
150 2
        $var = $this->getTags()->get('var', new Collection());
151 2
        if ($var->count() !== 0) {
152 1
            return $var;
153
        }
154
155 2
        $inheritedElement = $this->getInheritedElement();
156 2
        if ($inheritedElement) {
157 1
            return $inheritedElement->getVar();
158
        }
159
160 1
        return new Collection();
161
    }
162
163
    /**
164
     * Returns the file associated with the parent class or trait.
165
     *
166
     * @return FileDescriptor
167
     */
168 1
    public function getFile()
169
    {
170 1
        return $this->getParent()->getFile();
171
    }
172
173
    /**
174
     * Returns the property from which this one should inherit, if any.
175
     *
176
     * @return PropertyDescriptor|null
177
     */
178 2
    public function getInheritedElement()
179
    {
180
        /** @var ClassDescriptor|InterfaceDescriptor|null $associatedClass */
181 2
        $associatedClass = $this->getParent();
182
183 2
        if (($associatedClass instanceof ClassDescriptor || $associatedClass instanceof InterfaceDescriptor)
184 1
            && ($associatedClass->getParent() instanceof ClassDescriptor
185 2
                || $associatedClass->getParent() instanceof InterfaceDescriptor
186
            )
187
        ) {
188
            /** @var ClassDescriptor|InterfaceDescriptor $parentClass */
189 1
            $parentClass = $associatedClass->getParent();
190
191 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...
192
        }
193
194 1
        return null;
195
    }
196
}
197