BackTraceCallerPositionFinder::getCallerPosition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace DicDoc;
3
4
use DicDoc\Exceptions\CallerResolverException;
5
6
/**
7
 * Class BackTraceCallerPositionFinder
8
 *
9
 * @package DicDoc
10
 */
11
class BackTraceCallerPositionFinder implements Interfaces\CallerPositionFinder
12
{
13
    /**
14
     * @var int
15
     */
16
    private $deep;
17
18
    /**
19
     * BackTraceCallerPositionFinder constructor.
20
     *
21
     * @param int $deep
22
     */
23
    public function __construct($deep = 4)
24
    {
25
26
        $this->deep = $deep;
27
    }
28
29
30
    /**
31
     * @return CallerPosition
32
     * @throws Exceptions\CallerResolverException
33
     */
34
    public function getCallerPosition()
35
    {
36
        $caller = $this->findCaller();
37
38
        return new CallerPosition($caller['file'], $caller['line']);
39
    }
40
41
    /**
42
     * @return mixed
43
     * @throws Exceptions\CallerResolverException
44
     */
45
    public function findCaller()
46
    {
47
        $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $this->deep);
48
        foreach ($bt as $call) {
49
            if (!isset($call['class']) || strpos($call['class'], __NAMESPACE__) === false) {
50
                return $call;
51
            }
52
        }
53
54
        throw new CallerResolverException("Error finding the caller");
55
    }
56
}