Completed
Pull Request — 5.0 (#775)
by Michael
02:09
created

ObjectCallbackIterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 47.17 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 25
loc 53
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A current() 13 13 3
A offsetGet() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace ONGR\ElasticsearchBundle\Result;
4
5
class ObjectCallbackIterator extends \ArrayIterator
6
{
7
    /**
8
     * @var \Closure
9
     */
10
    private $callback;
11
12
    /**
13
     * Converts array data to document objects via the callback function.
14
     *
15
     * @param \Closure $callback
16
     * @param array $array
17
     */
18
    public function __construct(\Closure $callback, array $array = array())
19
    {
20
        $this->callback = $callback;
21
22
        parent::__construct($array);
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 View Code Duplication
    public function current()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        $value = parent::current();
31
32
        // Generate objects on demand
33
        if ($value === null && $this->valid()) {
34
            $key = $this->key();
35
            $callback = $this->callback;
36
            return $callback($key);
37
        }
38
39
        return $value;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 View Code Duplication
    public function offsetGet($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $value = parent::offsetGet($offset);
48
49
        // Generate objects on demand
50
        if ($value === null && $this->valid()) {
51
            $callback = $this->callback;
52
            return $callback($offset);
53
        }
54
55
        return $value;
56
    }
57
}
58