Completed
Push — develop ( bd0ea7...cbc256 )
by Mike
08:51
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 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;
99 1
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function getTypes()
105
    {
106
        return [$this->getType()];
0 ignored issues
show
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
    }
108
109
    public function getType()
110
    {
111
        if ($this->types === null) {
112
            /** @var VarDescriptor $var */
113
            $var = $this->getVar()->getIterator()->current();
114
            if ($var) {
115
                return $var->getType();
116
            }
117
        }
118
119
        return $this->types;
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125 1
    public function setVisibility($visibility)
126
    {
127 1
        $this->visibility = $visibility;
128 1
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 1
    public function getVisibility()
134
    {
135 1
        return $this->visibility;
136
    }
137
138
    /**
139
     * @return Collection
140
     */
141 2
    public function getVar()
142
    {
143
        /** @var Collection $var */
144 2
        $var = $this->getTags()->get('var', new Collection());
145 2
        if ($var->count() !== 0) {
146 1
            return $var;
147
        }
148
149 2
        $inheritedElement = $this->getInheritedElement();
150 2
        if ($inheritedElement) {
151 1
            return $inheritedElement->getVar();
152
        }
153
154 1
        return new Collection();
155
    }
156
157
    /**
158
     * Returns the file associated with the parent class or trait.
159
     *
160
     * @return FileDescriptor
161
     */
162 1
    public function getFile()
163
    {
164 1
        return $this->getParent()->getFile();
165
    }
166
167
    /**
168
     * Returns the property from which this one should inherit, if any.
169
     *
170
     * @return PropertyDescriptor|null
171
     */
172 2
    public function getInheritedElement()
173
    {
174
        /** @var ClassDescriptor|InterfaceDescriptor|null $associatedClass */
175 2
        $associatedClass = $this->getParent();
176
177 2
        if (($associatedClass instanceof ClassDescriptor || $associatedClass instanceof InterfaceDescriptor)
178 1
            && ($associatedClass->getParent() instanceof ClassDescriptor
179 2
                || $associatedClass->getParent() instanceof InterfaceDescriptor
180
            )
181
        ) {
182
            /** @var ClassDescriptor|InterfaceDescriptor $parentClass */
183 1
            $parentClass = $associatedClass->getParent();
184
185 1
            return $parentClass->getProperties()->get($this->getName());
186
        }
187
188 1
        return null;
189
    }
190
}
191