Loop   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 94
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A init() 0 9 3
A setParentLoop() 0 4 1
A getItems() 0 4 1
A __get() 0 4 2
A loop() 0 16 2
1
<?php
2
3
namespace Advmaker\BladeLoop;
4
5
class Loop
6
{
7
    /**
8
     * The array that is being iterated
9
     *
10
     * @var array
11
     */
12
    protected $items = [];
13
14
    /**
15
     * The data for the current $loop item
16
     *
17
     * @var array
18
     */
19
    protected $data;
20
21
    /**
22
     * Instantiates the class
23
     *
24
     * @param array $items The array that's being iterated
25
     */
26 30
    public function __construct($items)
27
    {
28 30
        $this->items = $items;
29 30
        $this->init($items);
30 30
    }
31
32
    /**
33
     * Sets the array to monitor
34
     *
35
     * @param array $items The array that's being iterated
36
     */
37 30
    protected function init($items)
38
    {
39 30
        $total = is_array($items) || $items instanceof \Countable ? count($items) : null;
40 30
        $this->data = [
41 30
            'index'     => -1,
42 30
            'revindex'  => $total,
43
            'length'    => $total
44 30
        ];
45 30
    }
46
47
    /**
48
     * Sets the parent loop
49
     *
50
     * @param Loop $parentLoop
51
     */
52 3
    public function setParentLoop(Loop $parentLoop)
53
    {
54 3
        $this->data['parent'] = $parentLoop;
55 3
    }
56
57
    /**
58
     * Return array that must be iterated.
59
     *
60
     * @return array
61
     */
62 30
    public function getItems()
63
    {
64 30
        return $this->items;
65
    }
66
67
    /**
68
     * Magic method to access the loop data properties
69
     *
70
     * @param $key
71
     *
72
     * @return mixed
73
     */
74 30
    public function __get($key)
75
    {
76 30
        return isset($this->data[$key]) ? $this->data[$key] : null;
77
    }
78
79
    /**
80
     * To be called first in a loop before anything else
81
     */
82 30
    public function loop()
83
    {
84 30
        $this->data['index']++;
85 30
        $this->data['index1'] = $this->data['index'] + 1;
86
87 30
        $this->data['even'] = $this->data['index'] % 2 === 0;
88 30
        $this->data['odd'] = ! $this->data['even'];
89
90 30
        $this->data['first'] = $this->data['index'] === 0;
91
92 30
        if (null !== $this->data['length']) {
93 24
            $this->data['revindex']--;
94 24
            $this->data['revindex1'] = $this->data['revindex'] + 1;
95 24
            $this->data['last'] = $this->data['revindex'] === 0;
96 24
        }
97 30
    }
98
}
99