Completed
Pull Request — master (#76)
by Luke
02:26
created

IsTraversable   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isTraversable() 0 4 1
A traverse() 0 12 4
1
<?php
2
/**
3
 * Nozavroni/Collections
4
 * Just another collections library for PHP5.6+.
5
 * @version   {version}
6
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
7
 * @author    Luke Visinoni <[email protected]>
8
 * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT)
9
 */
10
namespace Noz\Traits;
11
12
/**
13
 * Interface IsArrayable.
14
 *
15
 * Ensures a class can be converted to an array using toArray()
16
 *
17
 * @package Noz\Contracts
18
 */
19
trait IsTraversable
20
{
21
    /**
22
     * @var bool
23
     */
24
    protected $traversable = true;
25
26
    /**
27
     * Is this object traversable?
28
     *
29
     * @return bool
30
     */
31
    public function isTraversable()
32
    {
33
        return $this->traversable;
34
    }
35
36
    /**
37
     * Traverse this object with given callback.
38
     * Loops over this object, passing each iteration to callback function. Return false at any time from callback to exit loop and return false.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
39
     *
40
     * @param callable $callback
41
     *
42
     * @return bool
43
     */
44
    public function traverse(callable $callback)
45
    {
46
        if ($this->isTraversable()) {
47
            foreach ($this as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Noz\Traits\IsTraversable> is not traversable.
Loading history...
48
                if (!$callback($val, $key)) {
49
                    return false;
50
                }
51
            }
52
            return true;
53
        }
54
        return false;
55
    }
56
}