Passed
Push — master ( 7ce976...84a9ea )
by Atanas
02:24
created

ExceptionHandler::rethrow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WPEmerge\Exceptions;
4
5
use Exception as PhpException;
6
use WPEmerge\Facades\Response;
7
8
class ExceptionHandler implements ExceptionHandlerInterface {
9
	/**
10
	 * Whether debug mode is enabled.
11
	 *
12
	 * @var boolean
13
	 */
14
	protected $debug = false;
15
16
	/**
17
	 * Stack trace handler in debug mode.
18
	 *
19
	 * @var callable
20
	 */
21
	protected $stack_trace_handler = null;
22
23
	/**
24
	 * Constructor.
25
	 *
26
	 * @codeCoverageIgnore
27
	 * @param boolean       $debug
28
	 * @param callable|null $stack_trace_handler
29
	 */
30
	public function __construct( $debug = false, $stack_trace_handler = null ) {
31
		$this->debug = $debug;
32
		$this->stack_trace_handler = is_callable( $stack_trace_handler ) ? $stack_trace_handler : array( $this, 'rethrow' );
0 ignored issues
show
Documentation Bug introduced by
It seems like is_callable($stack_trace...array($this, 'rethrow') can also be of type array<integer,string|WPE...tions\ExceptionHandler>. However, the property $stack_trace_handler is declared as type callable. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
33
	}
34
35
	/**
36
	 * Throw an exception.
37
	 *
38
	 * @codeCoverageIgnore
39
	 * @throws PhpException
40
	 * @param  PhpException $exception;
41
	 * @return void
42
	 */
43
	protected function rethrow( PhpException $exception ) {
44
		throw $exception;
45
	}
46
47
	/**
48
	 * {@inheritDoc}
49
	 */
50 3
	public function handle( PhpException $exception ) {
51
		// @codeCoverageIgnoreStart
52
		if ( $exception instanceof InvalidCsrfTokenException ) {
53
			wp_nonce_ays( '' );
54
		}
55
		// @codeCoverageIgnoreEnd
56
57 3
		if ( $exception instanceof NotFoundException ) {
58 1
			return Response::error( 404 );
59
		}
60
61 2
		if ( $this->debug ) {
62 1
			return call_user_func( $this->stack_trace_handler, $exception );
63
		}
64
65 1
		$this->rethrow( $exception );
66
	}
67
}
68