Passed
Push — master ( 166f04...544c4b )
by mcfog
02:38
created

MiddlewareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 2
cts 12
cp 0.1666
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 7 1
A when() 0 3 1
A prepend() 0 7 1
A catch() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Nimo\Traits;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Lit\Nimo\Interfaces\RequestPredictionInterface;
9
use Lit\Nimo\MiddlewarePipe;
10
use Lit\Nimo\Middlewares\CatchMiddleware;
11
use Lit\Nimo\Middlewares\PredictionWrapperMiddleware;
12
13
trait MiddlewareTrait
14
{
15
    use AttachToRequestTrait;
16
17
    /**
18
     * append $middleware after this one, return the new $middlewareStack
19
     *
20
     * @param $middleware
21
     * @return MiddlewarePipe
22
     */
23
    public function append(MiddlewareInterface $middleware): MiddlewarePipe
24
    {
25
        $stack = new MiddlewarePipe();
26
27
        return $stack
28
            ->append($this)
0 ignored issues
show
Bug introduced by
$this of type Lit\Nimo\Traits\MiddlewareTrait is incompatible with the type Psr\Http\Server\MiddlewareInterface expected by parameter $middleware of Lit\Nimo\MiddlewarePipe::append(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
            ->append(/** @scrutinizer ignore-type */ $this)
Loading history...
29
            ->append($middleware);
30
    }
31
32
    /**
33
     * prepend $middleware before this one, return the new $middlewareStack
34
     *
35
     * @param $middleware
36
     * @return MiddlewarePipe
37
     */
38
    public function prepend(MiddlewareInterface $middleware): MiddlewarePipe
39
    {
40
        $stack = new MiddlewarePipe();
41
42
        return $stack
43
            ->prepend($this)
0 ignored issues
show
Bug introduced by
$this of type Lit\Nimo\Traits\MiddlewareTrait is incompatible with the type Psr\Http\Server\MiddlewareInterface expected by parameter $middleware of Lit\Nimo\MiddlewarePipe::prepend(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
            ->prepend(/** @scrutinizer ignore-type */ $this)
Loading history...
44
            ->prepend($middleware);
45
    }
46
47
    public function when(RequestPredictionInterface $requestPrediction): MiddlewareInterface
48
    {
49
        return new PredictionWrapperMiddleware($this, $requestPrediction);
0 ignored issues
show
Bug introduced by
$this of type Lit\Nimo\Traits\MiddlewareTrait is incompatible with the type Psr\Http\Server\MiddlewareInterface expected by parameter $innerMiddleware of Lit\Nimo\Middlewares\Pre...ddleware::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        return new PredictionWrapperMiddleware(/** @scrutinizer ignore-type */ $this, $requestPrediction);
Loading history...
50
    }
51
52 1
    public function catch (callable $catcher, string $catchClass = \Throwable::class): MiddlewareInterface
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
53
    {
54 1
        return new CatchMiddleware($this, $catcher, $catchClass);
55
    }
56
}
57