FindAccessor::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Underscore\Accessor;
4
5
use Underscore\Accessor;
6
use Underscore\Collection;
7
8
class FindAccessor extends Accessor
9
{
10
    /**
11
     * Iterates over elements of a collection, returning the first element that the callback returns truey for.
12
     *
13
     * Returns mixed
14
     *
15
     * @param Collection $collection
16
     * @param callable   $iterator
17
     * @return Collection
18
     */
19 4
    public function __invoke($collection, $iterator)
20
    {
21 4
        $collection = clone $collection;
22
23 4
        $found = false;
24 4
        foreach ($collection as $k => $v) {
25 4
            if (call_user_func($iterator, $v, $k, $collection)) {
26 4
                $found = true;
27 4
                break;
28
            }
29
        }
30
31 4
        return $found;
32
    }
33
}
34