Completed
Push — master ( bbf21d...d72023 )
by Jean
01:53
created

UsageException::setStackLocationHere()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
            return  isset($backtrace[ $level ]['class'])
24
                &&  $backtrace[ $level ]['class'] == __CLASS__;
25
        }, 0 );
26
    }
27
28
    /**
29
     */
30
    public function setStackLocationHere()
31
    {
32
        $this->rewindStackWhile( function($backtrace, $level) {
33
            // Finds the closest caller
34
            return $level == 0;
35
        }, 2 );
36
    }
37
38
    /**
39
     */
40
    protected function rewindStackWhile(callable $scope_checker, $stack_max_depth=20)
41
    {
42
        $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $stack_max_depth);
43
        $i         = 1;
44
        $caller    = $backtrace[$i];
45
        while ( $scope_checker( $backtrace, $i ) ) {
46
            $i++;
47
            $caller = $backtrace[$i];
48
            // TODO remove the prevuce levels of the stack?
49
        }
50
51
        // var_export($backtrace);
52
        // var_export($caller);
53
54
        $this->file = $caller['file'];
55
        $this->line = $caller['line'];
56
57
        // var_export($this->stack);
58
    }
59
60
    /**/
61
}
62