Completed
Push — master ( ccabef...42c495 )
by Marcos
02:26
created

CallbackMapIterator::current()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 4
nc 2
nop 0
1
<?php
2
/*
3
* The MIT License (MIT)
4
* Copyright © 2017 Marcos Lois Bermudez <[email protected]>
5
* *
6
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
* *
11
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
* of the Software.
13
* *
14
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
* DEALINGS IN THE SOFTWARE.
19
*/
20
21
namespace TTIC\Commons\Iterator;
22
23
use IteratorIterator;
24
use Traversable;
25
26
/**
27
 * A Iterator that applies the callback to each element accessed
28
 * in the inner iterator.
29
 *
30
 * @package \TTIC\Commons\Iterator
31
 * @author Marcos Lois Bermúdez <[email protected]>
32
 */
33
class CallbackMapIterator extends IteratorIterator
34
{
35
    /** @var callable */
36
    private $callback;
37
38
    private $current;
39
40
    /**
41
     * Create CallbackMapIterator from anything that is traversable
42
     *
43
     * @param \Traversable $iterator
44
     * @since 5.1.0
45
     */
46
    public function __construct(Traversable $iterator, $callback)
47
    {
48
        if (!is_callable($callback)) {
49
            throw new \InvalidArgumentException("The supplied callback is not callable.");
50
        }
51
52
        parent::__construct($iterator);
53
        $this->callback = $callback;
54
    }
55
56
    /**
57
     * Get the current value after calling the registered callback.
58
     * The callback will only be fired once on first access to current value.
59
     *
60
     * @return mixed The value of the current element.
61
     */
62
    public function current()
63
    {
64
        // Avoid call the callback more than once
65
        if (!$this->current && parent::valid()) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (valid() instead of current()). Are you sure this is correct? If so, you might want to change this to $this->valid().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
66
            $this->current = ($this->callback)(parent::current(), parent::key(), $this);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (key() instead of current()). Are you sure this is correct? If so, you might want to change this to $this->key().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
67
        }
68
69
        return $this->current;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function next()
76
    {
77
        $this->current = null;
78
        parent::next();
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function rewind()
85
    {
86
        $this->current = null;
87
        parent::rewind();
88
    }
89
}
90