CallbackRecursiveIterator::rewind()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace itertools;
4
5
use RecursiveIterator;
6
use IteratorIterator;
7
use EmptyIterator;
8
9
10
class CallbackRecursiveIterator extends CachingIterator implements RecursiveIterator
11
{
12
	protected $getChildrenCallback;
13
	protected $currentChildrenCache;
14
	protected $isCurrentChildrenCacheValid = false;
15
16
	public function __construct($startNodes, $getChildrenCallback)
17
	{
18
		parent::__construct(IterUtil::asIterator($startNodes));
19
		$this->getChildrenCallback = $getChildrenCallback;
20
	}
21
22
	public function getChildren()
23
	{
24
		if($this->isCurrentChildrenCacheValid) {
25
			return $this->currentChildrenCache;
26
		}
27
		$children = call_user_func($this->getChildrenCallback, $this->current());
28
		if($children === null || $children === false) {
29
			$children = new EmptyIterator();
30
		}
31
		$this->currentChildrenCache = new self($children, $this->getChildrenCallback);
32
		$this->isCurrentChildrenCacheValid = true;
33
		return $this->currentChildrenCache;
34
	}
35
36
	public function hasChildren()
37
	{
38
		return $this->getChildren()->hasNext();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface RecursiveIterator as the method hasNext() does only exist in the following implementations of said interface: RecursiveCachingIterator, itertools\CallbackRecursiveIterator.

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...
39
	}
40
41
	public function next()
42
	{
43
		$this->isCurrentChildrenCacheValid = false;
44
		return parent::next();
45
	}
46
47
	public function rewind()
48
	{
49
		$this->isCurrentChildrenCacheValid = false;
50
		return parent::rewind();
51
	}
52
}
53
54