Completed
Pull Request — 2.x (#29)
by Akihito
01:35
created

TwigErrorPageHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * This file is part of the Madapaja.TwigModule package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Madapaja\TwigModule;
8
9
use BEAR\Resource\Code;
10
use BEAR\Resource\Exception\BadRequestException as BadRequest;
11
use BEAR\Resource\Exception\ResourceNotFoundException as NotFound;
12
use BEAR\Resource\Exception\ServerErrorException as ServerError;
13
use BEAR\Resource\TransferInterface;
14
use BEAR\Sunday\Extension\Error\ErrorInterface;
15
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
16
use Psr\Log\LoggerInterface;
17
18
final class TwigErrorPageHandler implements ErrorInterface
19
{
20
    /**
21
     * @var TransferInterface
22
     */
23
    private $transfer;
24
    /**
25
     * @var TwigErrorPage
26
     */
27
    private $errorPage;
28
29
    /**
30
     * @var LoggerInterface
31
     */
32
    private $logger;
33
34
    public function __construct(TwigErrorPage $errorPage, TransferInterface $transfer, LoggerInterface $logger)
35
    {
36
        $this->transfer = $transfer;
37
        $this->errorPage = $errorPage;
38
        $this->logger = $logger;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function handle(\Exception $e, Request $request)
45
    {
46
        unset($request);
47
        $code = $this->isCodeExists($e) ? $e->getCode() : 503;
48
        if ($code >= 500) {
49
            $eStr = (string) $e;
50
            $this->logger->error(\sprintf('logref:%s %s', \crc32($eStr), $eStr));
51
        }
52
        $this->errorPage->code = $code;
53
        $this->errorPage->body = [
54
            'status' => [
55
                'code' => $code,
56
                'message' => (new Code)->statusText[$code]
57
            ],
58
            'e' => [
59
                'code' => $e->getCode(),
60
                'message' => $e->getMessage()
61
            ]
62
        ];
63
64
        return $this;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function transfer()
71
    {
72
        ($this->transfer)($this->errorPage, []);
73
    }
74
75
    private function isCodeExists(\Exception $e) : bool
76
    {
77
        if (! ($e instanceof NotFound) && ! ($e instanceof BadRequest) && ! ($e instanceof ServerError)) {
78
            return false;
79
        }
80
81
        return \array_key_exists($e->getCode(), (new Code)->statusText);
0 ignored issues
show
Bug introduced by
The method getCode does only exist in BEAR\Resource\Exception\BadRequestException, but not in BEAR\Resource\Exception\ServerErrorException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
82
    }
83
}
84