UsageException::rewindStackWhile()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
rs 9.9332
c 0
b 0
f 0
cc 4
nc 8
nop 2
1
<?php
2
namespace JClaveau\Exceptions;
3
4
/**
5
 */
6
class UsageException extends \Exception
7
{
8
    /**
9
     * /
10
    public function new_($message)
11
    {
12
        return new static($message);
13
    }
14
15
    /**
16
     */
17
    public function __construct()
18
    {
19
        parent::__construct(...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $message of Exception::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

19
        parent::__construct(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
20
21
        $this->rewindStackWhile( function($backtrace, $level) {
22
            // Finds the closest caller
23
            // TODO get the class of the caller instead of comparing
24
            // with UsageException
25
            return  isset($backtrace[ $level ]['class'])
26
                &&  $backtrace[ $level ]['class'] == __CLASS__;
27
        }, 0 );
28
    }
29
30
    /**
31
     */
32
    public function setStackLocationHere()
33
    {
34
        $this->rewindStackWhile( function($backtrace, $level) {
35
            // Finds the closest caller
36
            return $level < 2;
37
        }, 4 );
38
39
        return $this;
40
    }
41
42
    /**
43
     */
44
    protected function rewindStackWhile(callable $scope_checker, $stack_max_depth=20)
45
    {
46
        $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $stack_max_depth);
47
        $i         = 1;
48
        $caller    = $backtrace[$i];
49
        while ( $scope_checker( $backtrace, $i ) ) {
50
            $i++;
51
            $caller = $backtrace[$i];
52
            // TODO remove the previous levels of the stack?
53
        }
54
55
        // var_export($backtrace);
56
        // var_export($caller);
57
58
        // TODO How to handle perfectly the missing fields of the backtrace?
59
        if (isset($caller['file']))
60
            $this->file = $caller['file'];
61
62
        if (isset($caller['line']))
63
            $this->line = $caller['line'];
64
65
        // var_export($this->stack);
66
    }
67
68
    /**/
69
}
70