Loop   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 22
c 2
b 0
f 0
dl 0
loc 47
ccs 21
cts 21
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A __isset() 0 3 1
A __get() 0 3 1
A next() 0 7 1
A __set() 0 3 1
1
<?php
2
3
namespace Bavix\Flow;
4
5
use Bavix\Exceptions\Invalid;
6
7
/**
8
 * Class Loop
9
 *
10
 * @package Bavix\Flow
11
 *
12
 * @property-read $first
13
 * @property-read $firstIndex
14
 * @property-read $index
15
 * @property-read $iteration
16
 * @property-read $key
17
 * @property-read $last
18
 * @property-read $lastIndex
19
 */
20
class Loop
21
{
22
23
    protected $iteration = 0;
24
    protected $index     = -1;
25
    protected $key;
26
27
    protected $first;
28
    protected $firstIndex;
29
    protected $last;
30
    protected $lastIndex;
31
32 8
    public function __construct($data)
33
    {
34 8
        if (!empty($data))
35
        {
36 8
            $this->firstIndex = \key($data);
37 8
            $this->key        = $this->firstIndex;
38 8
            \end($data);
39
40 8
            $this->lastIndex = \key($data);
41 8
            \reset($data);
42
        }
43 8
    }
44
45 5
    public function next($key)
46
    {
47 5
        $this->index++;
48 5
        $this->iteration++;
49 5
        $this->key   = $key;
50 5
        $this->first = $this->firstIndex === $this->key;
51 5
        $this->last  = $this->lastIndex === $this->key;
52 5
    }
53
54 2
    public function __get(string $name)
55
    {
56 2
        return $this->{$name};
57
    }
58
59 1
    public function __set(string $name, $value)
60
    {
61 1
        throw new Invalid('Loop::' . $name . ' readonly');
62
    }
63
64 1
    public function __isset(string $name)
65
    {
66 1
        return \property_exists($this, $name);
67
    }
68
69
}
70