Completed
Push — master ( 4f3de5...9b9e7a )
by Pavel
05:51
created

ErrorHandlerServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 24 3
1
<?php
2
3
namespace App\Providers;
4
5
use App\Common\JsonException;
6
use Pimple\Container;
7
8
final class ErrorHandlerServiceProvider extends BaseServiceProvider
9
{
10
    /**
11
     * Register error handlers service provider.
12
     *
13
     * @param Container $container
14
     */
15
    public function register(Container $container)
16
    {
17
        $container['errorHandler'] = function (Container $c) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $c. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
18
            return function($request, $response, $exception) use ($c) {
19
                $details = (defined('DEBUG_MODE') && DEBUG_MODE == 1) ? $exception->getMessage() : 'Internal server error';
20
                $e       = new JsonException(null, 500, 'Internal server error', $details);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $e. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
21
22
                return $c->get('renderer')->jsonApiRender($response, $e->statusCode, $e->encodeError());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Pimple\Container as the method get() does only exist in the following sub-classes of Pimple\Container: Slim\Container. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
23
            };
24
        };
25
26
        $container['notAllowedHandler'] = function (Container $c) {
27
            return function($request, $response, $methods) use ($c) {
28
                throw new JsonException(null, 405, 'Method Not Allowed', 'Method must be one of: '.implode(', ', $methods));
29
            };
30
        };
31
32
        $container['notFoundHandler'] = function (Container $c) {
33
            return function() use ($c) {
34
                throw new JsonException(null, 404, 'Not found', 'Entity not found');
35
            };
36
        };
37
38
    }
39
}
40