Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

phpDocumentor/Descriptor/PropertyDescriptor.php (1 issue)

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