Test Failed
Push — master ( 7da8a2...7ea984 )
by n
02:25
created

MiddlewareResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 38
loc 38
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C resolve() 29 29 8

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
namespace N1215\Jugoya\Resolver;
4
5
use Interop\Http\ServerMiddleware\MiddlewareInterface;
6
use N1215\Jugoya\Wrapper\CallableMiddleware;
7
use Psr\Container\NotFoundExceptionInterface;
8
use Psr\Container\ContainerExceptionInterface;
9
10 View Code Duplication
class MiddlewareResolver implements MiddlewareResolverInterface
0 ignored issues
show
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...
11
{
12
13
    /**
14
     * @param string|callable|MiddlewareInterface $ref
15
     * @return MiddlewareInterface
16 10
     * @throws UnresolvedException
17
     */
18 10
    public function resolve($ref)
19
    {
20
        if ($ref instanceof MiddlewareInterface) {
21
            return $ref;
22
        }
23
24
        if (is_callable($ref)) {
25
            return new CallableMiddleware($ref);
26
        }
27
28
        if (!is_string($ref)) {
29
            throw new \InvalidArgumentException("Argument 1 \$ref must be one of an instance of MiddlewareInterface, a callable or string.");
30
        }
31
32
        try {
33
            $entry = $this->container->get($ref);
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
        } catch (NotFoundExceptionInterface $e) {
35
            throw new UnresolvedException('Could not found an entry from the container.', 0, $e);
36
        } catch (ContainerExceptionInterface $e) {
37
            throw new UnresolvedException('Something wrong with the container.', 0, $e);
38
        }
39
40
        if (!$entry instanceof MiddlewareInterface) {
41
            $type = is_object($entry) ? get_class($entry) : gettype($entry);
42
            throw new UnresolvedException("Expected container returns an instance of MiddlewareInterface, {$type} given.");
43
        }
44
45
        return $entry;
46
    }
47
}
48