Completed
Push — master ( 02d922...ecbcd5 )
by n
01:50
created

LazyDelegateHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 65
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A handle() 0 19 2
1
<?php
2
3
namespace N1215\Jugoya;
4
5
use Interop\Http\Server\MiddlewareInterface;
6
use Interop\Http\Server\RequestHandlerInterface;
7
use N1215\Jugoya\Resolver\MiddlewareResolverInterface;
8
use N1215\Jugoya\Resolver\RequestHandlerResolverInterface;
9
use N1215\Jugoya\Resolver\UnresolvedException;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
13
class LazyDelegateHandler implements RequestHandlerInterface
14
{
15
    /**
16
     * @var RequestHandlerResolverInterface
17
     */
18
    private $handlerResolver;
19
20
    /**
21
     * @var MiddlewareResolverInterface
22
     */
23
    private $middlewareResolver;
24
25
    /**
26
     * @var RequestHandlerInterface|callable|string $coreHandlerRef
27
     */
28
    private $coreHandlerRef;
29
30
    /**
31
     * @var MiddlewareInterface[]|callable[]|string[]
32
     */
33
    private $middlewareRefs = [];
34
35
    /**
36
     * @param RequestHandlerResolverInterface $handlerResolver
37
     * @param MiddlewareResolverInterface $middlewareResolver
38
     * @param RequestHandlerInterface|callable|string $coreHandlerRef
39
     * @param MiddlewareInterface[]|callable[]|string[] $middlewareRefs
40
     */
41 6
    public function __construct(
42
        RequestHandlerResolverInterface $handlerResolver,
43
        MiddlewareResolverInterface $middlewareResolver,
44
        $coreHandlerRef,
45
        array $middlewareRefs
46
    ) {
47 6
        $this->handlerResolver = $handlerResolver;
48 6
        $this->middlewareResolver = $middlewareResolver;
49 6
        $this->coreHandlerRef = $coreHandlerRef;
0 ignored issues
show
Documentation Bug introduced by
It seems like $coreHandlerRef can also be of type object<Interop\Http\Serv...equestHandlerInterface>. However, the property $coreHandlerRef is declared as type callable. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
50 6
        $this->middlewareRefs = $middlewareRefs;
0 ignored issues
show
Documentation Bug introduced by
It seems like $middlewareRefs of type array<integer,object<Int...areInterface>|callable> is incompatible with the declared type array<integer,callable> of property $middlewareRefs.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51 6
    }
52
53
    /**
54
     * @param ServerRequestInterface $request
55
     * @return ResponseInterface
56
     * @throws UnresolvedException
57
     */
58 6
    public function handle(ServerRequestInterface $request): ResponseInterface
59
    {
60 6
        $count = count($this->middlewareRefs);
61
62 6
        if ($count === 0) {
63 6
            $coreHandler = $this->handlerResolver->resolve($this->coreHandlerRef);
64 6
            return $coreHandler->handle($request);
65
        }
66
67 5
        $headMiddleware = $this->middlewareResolver->resolve($this->middlewareRefs[0]);
68 5
        $tailMiddlewareRefs = array_slice($this->middlewareRefs, 1, $count - 1);
69 5
        $innerDelegate = new self(
70 5
            $this->handlerResolver,
71 5
            $this->middlewareResolver,
72 5
            $this->coreHandlerRef,
73 5
            $tailMiddlewareRefs
74
        );
75 5
        return $headMiddleware->process($request, $innerDelegate);
76
    }
77
}
78