AbstractLazyCollection   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 82
wmc 7
lcom 1
cbo 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 6 1
current() 0 1 ?
A key() 0 6 1
A next() 0 6 1
A rewind() 0 6 1
A valid() 0 6 1
A toArray() 0 9 2
initialize() 0 1 ?
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Component\DocumentManager\Collection;
13
14
/**
15
 * Lazily hydrate query results.
16
 */
17
abstract class AbstractLazyCollection implements \Iterator, \Countable
18
{
19
    /**
20
     * @var \Iterator
21
     */
22
    protected $documents;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function count()
28
    {
29
        $this->initialize();
30
31
        return $this->documents->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, Doctrine\ODM\PHPCR\Tools\Console\MetadataFilter, GlobIterator, HttpMessage, HttpRequestPool, Issue523, Jackalope\NodePathIterator, Jackalope\Query\NodeIterator, Jackalope\Query\RowIterator, MongoCursor, MongoGridFSCursor, PHP_Token_Stream, Phar, PharData, RecursiveArrayIterator, RecursiveCachingIterator, SQLiteResult, SimpleXMLIterator, SplDoublyLinkedList, SplFixedArray, SplHeap, SplMaxHeap, SplMinHeap, SplObjectStorage, SplPriorityQueue, SplQueue, SplStack, Sulu\Component\DocumentM...\AbstractLazyCollection, Sulu\Component\DocumentM...tion\ChildrenCollection, Sulu\Component\DocumentM...n\QueryResultCollection, Sulu\Component\DocumentM...tion\ReferrerCollection, Symfony\Component\Finder...rator\FilePathsIterator, Symfony\Component\Finder...rator\InnerNameIterator, Symfony\Component\Finder...rator\InnerSizeIterator, Symfony\Component\Finder...rator\InnerTypeIterator, Symfony\Component\Finder...or\MockFileListIterator.

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...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    abstract public function current();
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function key()
43
    {
44
        $this->initialize();
45
46
        return $this->documents->key();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function next()
53
    {
54
        $this->initialize();
55
56
        $this->documents->next();
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function rewind()
63
    {
64
        $this->initialize();
65
66
        $this->documents->rewind();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function valid()
73
    {
74
        $this->initialize();
75
76
        return $this->documents->valid();
77
    }
78
79
    /**
80
     * Returns a array of all documents in the collection.
81
     *
82
     * @return array
83
     */
84
    public function toArray()
85
    {
86
        $copy = [];
87
        foreach ($this as $document) {
88
            $copy[] = $document;
89
        }
90
91
        return $copy;
92
    }
93
94
    /**
95
     * Initialize the collection documents.
96
     */
97
    abstract protected function initialize();
98
}
99