| Conditions | 3 |
| Paths | 3 |
| Total Lines | 19 |
| Code Lines | 12 |
| 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 | parent::__construct(new SplObjectStorage()); |
||
| 26 | |||
| 27 | foreach ($objects as $key => $value) { |
||
| 28 | $this->append($value, $key); |
||
|
|
|||
| 29 | } |
||
| 30 | $this->getStorage()->rewind(); |
||
| 31 | } |
||
| 32 | |||
| 40 |
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: