Completed
Push — master ( 76f46c...a66e80 )
by Dominik
02:49
created

AdvancedErrorHandlerProvider::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ErrorHandler\Slim;
6
7
use Chubbyphp\ErrorHandler\ContentTypeResolver;
8
use Negotiation\Negotiator;
9
use Pimple\Container;
10
use Pimple\ServiceProviderInterface;
11
12
final class AdvancedErrorHandlerProvider implements ServiceProviderInterface
13
{
14
    /**
15
     * @param Container $container
16
     */
17
    public function register(Container $container)
18
    {
19
        $this->registerRequirements($container);
0 ignored issues
show
Unused Code introduced by
The call to the method Chubbyphp\ErrorHandler\S...:registerRequirements() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
20
21
        $container['errorHandler.defaultProvider'] = function () use ($container) {
22
            throw new \RuntimeException('Please configure your default provider for error handler!');
23
        };
24
25
        $container['errorHandler.providers'] = function () use ($container) {
26
            return [];
27
        };
28
29
        $container['errorHandler'] = function () use ($container) {
30
            return new AdvancedErrorHandler(
31
                $container['errorHandler.contentTypeResolver'],
32
                $container['errorHandler.defaultProvider'],
33
                $container['errorHandler.providers']
34
            );
35
        };
36
    }
37
38
    /**
39
     * @param Container $container
40
     */
41
    private function registerRequirements(Container $container)
42
    {
43
        $container['errorHandler.acceptNegation'] = function () use ($container) {
44
            return new Negotiator();
45
        };
46
47
        $container['errorHandler.contentTypeResolver'] = function () use ($container) {
48
            return new ContentTypeResolver($container['errorHandler.acceptNegation']);
49
        };
50
    }
51
}
52