Passed
Push — master ( 3a00a5...2b727f )
by ma
01:51
created

TException::setHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace tinymeng\tools\exception;
3
4
use Exception;
5
6
/**
7
 * Class TException
8
 * @package tinymeng\tools\exception
9
 * @Author: TinyMeng <[email protected]>
10
 * @Created: 2020/8/17
11
 */
12
class TException extends \Exception
13
{
14
    /**
15
     * @var int
16
     */
17
    private $headers;
18
19
    public function __construct(int $statusCode, string $message = '', Exception $previous = null, array $headers = [], $code = 0)
20
    {
21
        $this->setHeaders($headers);
22
23
        /** message */
24
        if(empty($message)) $message = isset(StatusCode::$status_code[$statusCode]) ? StatusCode::$status_code[$statusCode] :StatusCode::$status_code[StatusCode::COMMON_UNKNOWN];
25
        parent::__construct('ERROR_TINYMENG_TOOL: '.$message, $code, $previous);
26
    }
27
28
    public function getHeaders(): int
29
    {
30
        return $this->headers;
31
    }
32
33
    /**
34
     * @param array|int $headers
35
     * @return TException
36
     */
37
    public function setHeaders($headers): TException
38
    {
39
        $this->headers = $headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $headers can also be of type array. However, the property $headers is declared as type integer. 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...
40
        return $this;
41
    }
42
}
43