Caller::guess()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.8666
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Vehikl\Flip;
4
5
class Caller
6
{
7
    /**
8
     * Guess and return the object a feature is mixed into
9
     *
10
     * @throws \LogicException
11
     * @return object
12
     */
13 8
    public static function guess()
14
    {
15 8
        foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 10) as $trace) {
16 6
            if (self::shouldIgnore($trace['object'] ?? null)) {
17 6
                continue;
18
            }
19
20 6
            return $trace['object'];
21
        }
22
23 2
        throw new \LogicException('No caller found.');
24
    }
25
26
    /**
27
     * Determine if the guesser should ignore the passed object from the
28
     * stack trace.
29
     *
30
     * @param $object
31
     *
32
     * @return bool
33
     */
34 6
    private static function shouldIgnore($object)
35
    {
36 6
        return $object == null || $object instanceof Feature;
37
    }
38
}
39