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

phpDocumentor/Descriptor/ConstantDescriptor.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 constant
23
 */
24
class ConstantDescriptor extends DescriptorAbstract implements Interfaces\ConstantInterface
25
{
26
    /** @var ClassDescriptor|InterfaceDescriptor|null $parent */
27
    protected $parent;
28
29
    /** @var Type $type */
30
    protected $types;
31
32
    /** @var string $value */
33
    protected $value;
34
35
    /**
36
     * Registers a parent class or interface with this constant.
37
     *
38
     * @param ClassDescriptor|InterfaceDescriptor|null $parent
39
     *
40
     * @throws \InvalidArgumentException if anything other than a class, interface or null was passed.
41
     */
42 3
    public function setParent($parent)
43
    {
44 3
        if (!$parent instanceof ClassDescriptor && !$parent instanceof InterfaceDescriptor && $parent !== null) {
45 1
            throw new \InvalidArgumentException('Constants can only have an interface or class as parent');
46
        }
47
48 2
        $fqsen = $parent !== null
49 2
            ? $parent->getFullyQualifiedStructuralElementName() . '::' . $this->getName()
50 2
            : $this->getName();
51
52 2
        $this->setFullyQualifiedStructuralElementName($fqsen);
53
54 2
        $this->parent = $parent;
55 2
    }
56
57
    /**
58
     * @return null|ClassDescriptor|InterfaceDescriptor
59
     */
60 2
    public function getParent()
61
    {
62 2
        return $this->parent;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 1
    public function setTypes(Type $types)
69
    {
70 1
        $this->types = $types;
71 1
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function getTypes()
77
    {
78
        return [$this->getType()];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($this->getType()); (phpDocumentor\Reflection\Type[]) is incompatible with the return type declared by the interface phpDocumentor\Descriptor...tantInterface::getTypes of type array[].

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...
79
    }
80
81
    public function getType()
82
    {
83
        if ($this->types === null) {
84
            /** @var VarDescriptor $var */
85
            $var = $this->getVar()->get(0);
86
            if ($var) {
87
                return $var->getType();
88
            }
89
        }
90
91
        return $this->types;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 1
    public function setValue($value)
98
    {
99 1
        $this->value = $value;
100 1
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 1
    public function getValue()
106
    {
107 1
        return $this->value;
108
    }
109
110
    /**
111
     * @return Collection
112
     */
113 3
    public function getVar()
114
    {
115
        /** @var Collection $var */
116 3
        $var = $this->getTags()->get('var', new Collection());
117 3
        if ($var->count() !== 0) {
118 2
            return $var;
119
        }
120
121 2
        $inheritedElement = $this->getInheritedElement();
122 2
        if ($inheritedElement) {
123 2
            return $inheritedElement->getVar();
124
        }
125
126
        return new Collection();
127
    }
128
129
    /**
130
     * Returns the file associated with the parent class, interface or trait when inside a container.
131
     *
132
     * @return FileDescriptor
133
     */
134 2
    public function getFile()
135
    {
136 2
        return parent::getFile() ?: $this->getParent()->getFile();
137
    }
138
139
    /**
140
     * Returns the Constant from which this one should inherit, if any.
141
     *
142
     * @return ConstantDescriptor|null
143
     */
144
    public function getInheritedElement()
145
    {
146
        /** @var ClassDescriptor|InterfaceDescriptor|null $associatedClass */
147
        $associatedClass = $this->getParent();
148
149
        if (($associatedClass instanceof ClassDescriptor || $associatedClass instanceof InterfaceDescriptor)
150
            && ($associatedClass->getParent() instanceof ClassDescriptor
151
                || $associatedClass->getParent() instanceof InterfaceDescriptor
152
            )
153
        ) {
154
            /** @var ClassDescriptor|InterfaceDescriptor $parentClass */
155
            $parentClass = $associatedClass->getParent();
156
157
            return $parentClass->getConstants()->get($this->getName());
158
        }
159
160
        return null;
161
    }
162
}
163