HasMiddlewareTrait::addMiddleware()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
crap 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Core\Routing\Traits;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Closure;
22
use LogicException;
23
use Psr\Container\ContainerInterface;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
use function array_merge;
27
28
/**
29
 * @package Limoncello\Core
30
 *
31
 * @method string getCallableToCacheMessage();
32
 */
33
trait HasMiddlewareTrait
34
{
35
    /**
36
     * @var callable[]
37
     */
38
    private $middleware = [];
39
40
    /**
41
     * @param callable[] $middleware
42
     *
43
     * @return self
44
     */
45 20
    public function setMiddleware(array $middleware): self
46
    {
47 20
        foreach ($middleware as $item) {
48 16
            $isValid = $this->checkPublicStaticCallable($item, [
0 ignored issues
show
Bug introduced by
It seems like checkPublicStaticCallable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49 16
                ServerRequestInterface::class,
50
                Closure::class,
51
                ContainerInterface::class,
52 16
            ], ResponseInterface::class);
53 16
            if ($isValid === false) {
54 16
                throw new LogicException($this->getCallableToCacheMessage());
55
            }
56
        }
57
58 18
        $this->middleware = $middleware;
59
60 18
        return $this;
61
    }
62
63
    /**
64
     * @param callable[] $middleware
65
     *
66
     * @return self
67
     */
68 1
    public function addMiddleware(array $middleware): self
69
    {
70 1
        return $this->setMiddleware(array_merge($this->middleware, $middleware));
71
    }
72
}
73