Conditions | 4 |
Paths | 5 |
Total Lines | 23 |
Code Lines | 15 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
13 | public function __construct(Traversable $objects) |
||
14 | { |
||
15 | if (!$this instanceof OuterIterator) { |
||
16 | $msg = 'class %s uses trait %s but it is not an %s'; |
||
17 | throw new IncorrectInterface( |
||
18 | sprintf($msg, |
||
19 | __CLASS__, |
||
20 | __TRAIT__, |
||
21 | 'OuterIterator' |
||
22 | ) |
||
23 | ); |
||
24 | } |
||
25 | $arr = []; |
||
26 | foreach ($objects as $object) { |
||
27 | $arr[] = $object; |
||
28 | } |
||
29 | parent::__construct(new SplFixedArray(count($arr))); |
||
30 | |||
31 | foreach ($arr as $index => $obj) { |
||
32 | $this->append($obj, $index); |
||
|
|||
33 | } |
||
34 | $this->getStorage()->rewind(); |
||
35 | } |
||
36 | |||
45 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: