1 | <?php |
||
2 | |||
3 | namespace Zurbaev\ApiClient\Traits; |
||
4 | |||
5 | /** |
||
6 | * Trait LazyIterator |
||
7 | * |
||
8 | * @property array $keys |
||
9 | * @property array $items |
||
10 | * @property int $position |
||
11 | */ |
||
12 | trait LazyIterator |
||
13 | { |
||
14 | /** |
||
15 | * Performs lazy load checks and loads items if required. |
||
16 | */ |
||
17 | abstract protected function checkLazyLoad(); |
||
18 | |||
19 | /** |
||
20 | * Current iterator item. |
||
21 | * |
||
22 | * @return mixed|null |
||
23 | */ |
||
24 | View Code Duplication | public function current() |
|
0 ignored issues
–
show
|
|||
25 | { |
||
26 | $this->checkLazyLoad(); |
||
27 | |||
28 | $currentKey = $this->keys[$this->position]; |
||
29 | |||
30 | return isset($this->items[$currentKey]) ? $this->items[$currentKey] : null; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Current iterator position. |
||
35 | * |
||
36 | * @return mixed |
||
37 | */ |
||
38 | public function key() |
||
39 | { |
||
40 | $this->checkLazyLoad(); |
||
41 | |||
42 | return $this->keys[$this->position]; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Increments iterator position. |
||
47 | */ |
||
48 | public function next() |
||
49 | { |
||
50 | $this->checkLazyLoad(); |
||
51 | |||
52 | $this->position++; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Rewinds iterator back to first position. |
||
57 | */ |
||
58 | public function rewind() |
||
59 | { |
||
60 | $this->checkLazyLoad(); |
||
61 | |||
62 | $this->position = 0; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Determines if there is some value at current iterator position. |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | View Code Duplication | public function valid() |
|
0 ignored issues
–
show
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. ![]() |
|||
71 | { |
||
72 | $this->checkLazyLoad(); |
||
73 | |||
74 | if (!isset($this->keys[$this->position])) { |
||
75 | return false; |
||
76 | } |
||
77 | |||
78 | $currentKey = $this->keys[$this->position]; |
||
79 | |||
80 | return isset($this->items[$currentKey]); |
||
81 | } |
||
82 | } |
||
83 |
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.