Completed
Push — master ( de84eb...d1f862 )
by Jaap
18s queued 11s
created

InterfaceDescriptor::getInheritedMethods()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Descriptor;
15
16
use phpDocumentor\Reflection\Fqsen;
17
18
/**
19
 * Descriptor representing an Interface.
20
 */
21
class InterfaceDescriptor extends DescriptorAbstract implements Interfaces\InterfaceInterface
22
{
23
    /** @var Collection $extends */
24
    protected $parents;
25
26
    /** @var Collection $constants */
27
    protected $constants;
28
29
    /** @var Collection $methods */
30
    protected $methods;
31
32
    /**
33
     * Initializes the all properties representing a collection with a new Collection object.
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct();
38
39
        $this->setParent(new Collection());
40
        $this->setConstants(new Collection());
41
        $this->setMethods(new Collection());
42
    }
43
44 1
    public function setParent(Collection $parents) : void
45
    {
46 1
        $this->parents = $parents;
47 1
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 1
    public function getParent() : Collection
53
    {
54 1
        return $this->parents;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 1
    public function setConstants(Collection $constants) : void
61
    {
62 1
        $this->constants = $constants;
63 1
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 1
    public function getConstants() : Collection
69
    {
70 1
        return $this->constants;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 2
    public function getInheritedConstants() : Collection
77
    {
78 2
        $inheritedConstants = new Collection();
79
80
        /** @var self $parent */
81 2
        foreach ($this->getParent() as $parent) {
82 1
            if (!$parent instanceof Interfaces\InterfaceInterface) {
83 1
                continue;
84
            }
85
86 1
            $inheritedConstants = $inheritedConstants->merge($parent->getConstants());
0 ignored issues
show
Documentation introduced by
$parent->getConstants() is of type object<phpDocumentor\Descriptor\Collection>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87 1
            $inheritedConstants = $inheritedConstants->merge($parent->getInheritedConstants());
88
        }
89
90 2
        return $inheritedConstants;
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96 1
    public function setMethods(Collection $methods) : void
97
    {
98 1
        $this->methods = $methods;
99 1
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104 1
    public function getMethods() : Collection
105
    {
106 1
        return $this->methods;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112 2
    public function getInheritedMethods() : Collection
113
    {
114 2
        $inheritedMethods = new Collection();
115
116
        /** @var self $parent */
117 2
        foreach ($this->getParent() as $parent) {
118 1
            if (!$parent instanceof Interfaces\InterfaceInterface) {
119
                continue;
120
            }
121
122 1
            $inheritedMethods = $inheritedMethods->merge($parent->getMethods());
0 ignored issues
show
Documentation introduced by
$parent->getMethods() is of type object<phpDocumentor\Descriptor\Collection>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123 1
            $inheritedMethods = $inheritedMethods->merge($parent->getInheritedMethods());
0 ignored issues
show
Documentation introduced by
$parent->getInheritedMethods() is of type object<phpDocumentor\Descriptor\Collection>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
124
        }
125
126 2
        return $inheritedMethods;
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function setPackage($package) : void
133
    {
134
        parent::setPackage($package);
135
136
        foreach ($this->getConstants() as $constant) {
137
            $constant->setPackage($package);
138
        }
139
140
        foreach ($this->getMethods() as $method) {
141
            $method->setPackage($package);
142
        }
143
    }
144
145
    /**
146
     * @return InterfaceDescriptor|Fqsen|null
147
     */
148
    public function getInheritedElement()
149
    {
150
        return $this->getParent()->count() > 0
151
            ? $this->getParent()->getIterator()->current()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Traversable as the method current() does only exist in the following implementations of said interface: APCUIterator, AppendIterator, ArrayIterator, CachingIterator, CallbackFilterIterator, DirectoryIterator, EmptyIterator, FilesystemIterator, FilterIterator, Generator, GlobIterator, HttpMessage, HttpRequestPool, Imagick, ImagickPixelIterator, InfiniteIterator, IteratorIterator, LimitIterator, MongoCommandCursor, MongoCursor, MongoGridFSCursor, MultipleIterator, NoRewindIterator, ParentIterator, Phar, PharData, RecursiveArrayIterator, RecursiveCachingIterator, RecursiveCallbackFilterIterator, RecursiveDirectoryIterator, RecursiveFilterIterator, RecursiveIteratorIterator, RecursiveRegexIterator, RecursiveTreeIterator, RegexIterator, SQLiteResult, SebastianBergmann\FileIterator\Iterator, SimpleXMLIterator, SplDoublyLinkedList, SplFileObject, SplFixedArray, SplHeap, SplMaxHeap, SplMinHeap, SplObjectStorage, SplPriorityQueue, SplQueue, SplStack, SplTempFileObject, Symfony\Component\Finder...or\CustomFilterIterator, Symfony\Component\Finder...DateRangeFilterIterator, Symfony\Component\Finder...epthRangeFilterIterator, Symfony\Component\Finder...DirectoryFilterIterator, Symfony\Component\Finder...\FileTypeFilterIterator, Symfony\Component\Finder...lecontentFilterIterator, Symfony\Component\Finder...\FilenameFilterIterator, Symfony\Component\Finder...tiplePcreFilterIterator, Symfony\Component\Finder...ator\PathFilterIterator, Symfony\Component\Finder...ursiveDirectoryIterator, Symfony\Component\Finder...SizeRangeFilterIterator, Twig\Util\TemplateDirIterator, Twig_Util_TemplateDirIterator, org\bovigo\vfs\vfsStreamContainerIterator, phpDocumentor\Compiler\Compiler, phpDocumentor\Reflection...y\ClassConstantIterator, phpDocumentor\Reflection...actory\PropertyIterator.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
152
            : null;
153
    }
154
}
155