GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( fc5653...e1c659 )
by Thomas
23:54
created

ShuffleIterator::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Gielfeldt\Iterators;
4
5
class ShuffleIterator extends IteratorIterator implements \Countable
6
{
7
    protected $min = INF;
8
    protected $max = -INF;
9
10
    public function __construct(\Traversable $iterator)
11
    {
12
        parent::__construct($this->getShuffledIterator($iterator));
13
    }
14
15
    public function getShuffledIterator($iterator)
16
    {
17
        $sortedIterator = new \ArrayIterator();
18
        $sorted = [];
19
        foreach ($iterator as $key => $value) {
20
            $sorted[] = $this->generateElement($key, $value, $iterator);
21
            $this->min = $this->min < $value ? $this->min : $value;
22
            $this->max = $this->max > $value ? $this->max : $value;
23
        }
24
25
        shuffle($sorted);
26
27
        foreach ($sorted as $data) {
28
            $sortedIterator->append($data);
29
        }
30
        return $sortedIterator;
31
    }
32
33
    protected function generateElement($key, $value, $iterator)
0 ignored issues
show
Unused Code introduced by
The parameter $iterator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        return (object) ['key' => $key, 'current' => $value];
36
    }
37
38
    public function key()
39
    {
40
        return $this->getInnerIterator()->current()->key ?? null;
41
    }
42
43
    public function current()
44
    {
45
        return $this->getInnerIterator()->current()->current ?? null;
46
    }
47
48
    public function count()
49
    {
50
        return $this->getInnerIterator()->count();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Iterator as the method count() does only exist in the following implementations of said interface: ArrayIterator, CachingIterator, DoctrineTest\InstantiatorTestAsset\PharAsset, Gielfeldt\Iterators\CountableIterator, Gielfeldt\Iterators\CsvFileObject, Gielfeldt\Iterators\GlobIterator, Gielfeldt\Iterators\RecursiveSortIterator, Gielfeldt\Iterators\RepeatIterator, Gielfeldt\Iterators\ShuffleIterator, Gielfeldt\Iterators\SortIterator, GlobIterator, HttpMessage, HttpRequestPool, Issue523, MongoCursor, MongoGridFSCursor, PHP_Token_Stream, Phar, PharData, RecursiveArrayIterator, RecursiveCachingIterator, SQLiteResult, SimpleXMLIterator, SplDoublyLinkedList, SplFixedArray, SplHeap, SplMaxHeap, SplMinHeap, SplObjectStorage, SplPriorityQueue, SplQueue, SplStack, TheSeer\Tokenizer\TokenCollection.

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...
51
    }
52
53
    public function first()
54
    {
55
        $count = $this->getInnerIterator()->count();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Iterator as the method count() does only exist in the following implementations of said interface: ArrayIterator, CachingIterator, DoctrineTest\InstantiatorTestAsset\PharAsset, Gielfeldt\Iterators\CountableIterator, Gielfeldt\Iterators\CsvFileObject, Gielfeldt\Iterators\GlobIterator, Gielfeldt\Iterators\RecursiveSortIterator, Gielfeldt\Iterators\RepeatIterator, Gielfeldt\Iterators\ShuffleIterator, Gielfeldt\Iterators\SortIterator, GlobIterator, HttpMessage, HttpRequestPool, Issue523, MongoCursor, MongoGridFSCursor, PHP_Token_Stream, Phar, PharData, RecursiveArrayIterator, RecursiveCachingIterator, SQLiteResult, SimpleXMLIterator, SplDoublyLinkedList, SplFixedArray, SplHeap, SplMaxHeap, SplMinHeap, SplObjectStorage, SplPriorityQueue, SplQueue, SplStack, TheSeer\Tokenizer\TokenCollection.

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...
56
        return $count ? $this->getInnerIterator()[0]->current : null;
57
    }
58
59
    public function last()
60
    {
61
        $count = $this->getInnerIterator()->count();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Iterator as the method count() does only exist in the following implementations of said interface: ArrayIterator, CachingIterator, DoctrineTest\InstantiatorTestAsset\PharAsset, Gielfeldt\Iterators\CountableIterator, Gielfeldt\Iterators\CsvFileObject, Gielfeldt\Iterators\GlobIterator, Gielfeldt\Iterators\RecursiveSortIterator, Gielfeldt\Iterators\RepeatIterator, Gielfeldt\Iterators\ShuffleIterator, Gielfeldt\Iterators\SortIterator, GlobIterator, HttpMessage, HttpRequestPool, Issue523, MongoCursor, MongoGridFSCursor, PHP_Token_Stream, Phar, PharData, RecursiveArrayIterator, RecursiveCachingIterator, SQLiteResult, SimpleXMLIterator, SplDoublyLinkedList, SplFixedArray, SplHeap, SplMaxHeap, SplMinHeap, SplObjectStorage, SplPriorityQueue, SplQueue, SplStack, TheSeer\Tokenizer\TokenCollection.

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...
62
        return $count ? $this->getInnerIterator()[$count - 1]->current : null;
63
    }
64
65
    public function min()
66
    {
67
        return $this->min;
68
    }
69
70
    public function max()
71
    {
72
        return $this->max;
73
    }
74
}
75