SimpleErrorHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 55
loc 55
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A __invoke() 6 6 1
A logException() 16 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ErrorHandler\Slim;
6
7
use Chubbyphp\ErrorHandler\ErrorResponseProviderInterface;
8
use Chubbyphp\ErrorHandler\HttpException;
9
use Psr\Http\Message\ResponseInterface as Response;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Psr\Log\LoggerInterface;
12
use Psr\Log\NullLogger;
13
14
/**
15
 * @deprecated use Chubbyphp\ErrorHandler\SimpleErrorHandler
16
 */
17 View Code Duplication
final class SimpleErrorHandler implements ErrorHandlerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Chubbyphp\ErrorHandler\Slim\ErrorHandlerInterface has been deprecated with message: use Chubbyphp\ErrorHandler\ErrorHandlerInterface

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    /**
20
     * @var ErrorResponseProviderInterface
21
     */
22
    private $provider;
23
24
    /**
25
     * @var LoggerInterface
26
     */
27
    private $logger;
28
29
    /**
30
     * @param ErrorResponseProviderInterface $provider
31
     */
32 2
    public function __construct(ErrorResponseProviderInterface $provider, LoggerInterface $logger = null)
33
    {
34 2
        $this->provider = $provider;
35 2
        $this->logger = $logger ?? new NullLogger();
36 2
    }
37
38
    /**
39
     * @param Request    $request
40
     * @param Response   $response
41
     * @param \Exception $exception
42
     *
43
     * @return Response
44
     */
45 2
    public function __invoke(Request $request, Response $response, \Exception $exception): Response
46
    {
47 2
        $this->logException($exception);
48
49 2
        return $this->provider->get($request, $response, $exception);
50
    }
51
52
    /**
53
     * @param \Exception $exception
54
     */
55 2
    private function logException(\Exception $exception)
56
    {
57 2
        if ($exception instanceof HttpException) {
58 1
            $this->logger->warning(
59 1
                'error-handler: {code} {message}',
60 1
                ['status' => $exception->getCode(), 'message' => $exception->getMessage()]
61
            );
62
63 1
            return;
64
        }
65
66 1
        $this->logger->error(
67 1
            'error-handler: {code} {message}',
68 1
            ['status' => 500, 'message' => $exception->getMessage()]
69
        );
70 1
    }
71
}
72