AnyAccessor::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Underscore\Accessor;
4
5
use Underscore\Accessor;
6
use Underscore\Collection;
7
8
class AnyAccessor extends Accessor
9
{
10
    /**
11
     * Checks if the $iterator returns a truey value for ANY element of a collection.
12
     * The function returns as soon as it finds a passing value and does not iterate
13
     * over entire collection.
14
     *
15
     * Returns boolean
16
     *
17
     * @param Collection $collection
18
     * @param callable   $iterator
19
     * @return Collection
20
     */
21
    public function __invoke($collection, $iterator)
22
    {
23 1
        $iterator = function ($accumulator, $item) use ($iterator) {
24 1
            $accumulator = $accumulator || $iterator($item);
25
26 1
            return $accumulator;
27 1
        };
28
29 1
        $reduce = new ReduceAccessor();
30
31 1
        return $reduce($collection, $iterator, false);
32
    }
33
}
34