Completed
Push — master ( 1b2525...eaa10a )
by Albert
11:28
created

App   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 0
cbo 6
dl 0
loc 40
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A run() 0 15 1
1
<?php
2
3
namespace Albert221\Blog;
4
5
use Albert221\Blog\Controller\PostController;
6
use Albert221\Blog\ServiceProvider\HttpServiceProvider;
7
use Albert221\Blog\ServiceProvider\TwigServiceProvider;
8
use League\Container\Container;
9
use League\Container\ReflectionContainer;
10
use League\Route\RouteCollection;
11
use Zend\Diactoros\Request;
12
use Zend\Diactoros\Response;
13
use Zend\Diactoros\Response\SapiEmitter;
14
use Zend\Diactoros\ServerRequest;
15
16
class App
17
{
18
    /**
19
     * @var Container
20
     */
21
    protected $container;
22
23
    public function __construct()
24
    {
25
        $this->container = new Container;
26
        $this->container->delegate(new ReflectionContainer);
27
28
        $this->container->add('baseDir', dirname(__DIR__));
29
        
30
        $this->container->addServiceProvider(new HttpServiceProvider);
31
        $this->container->addServiceProvider(new TwigServiceProvider);
32
33
        $this->container->add(PostController::class)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface League\Container\Definition\DefinitionInterface as the method withMethodCall() does only exist in the following implementations of said interface: League\Container\Definition\ClassDefinition.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
34
            ->withMethodCall('setTwig', ['twig']);
35
    }
36
37
    /**
38
     * Sends response.
39
     */
40
    public function run()
41
    {
42
        /** @var RouteCollection $route */
43
        $route = $this->container->get('route');
44
        /** @var ServerRequest $request */
45
        $request = $this->container->get('request');
46
        /** @var Response $response */
47
        $response = $this->container->get('response');
48
        /** @var SapiEmitter $emitter */
49
        $emitter = $this->container->get('emitter');
50
51
        $response = $route->dispatch($request, $response);
52
53
        $emitter->emit($response);
54
    }
55
}