HasConfiguratorsTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 36
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfigurators() 0 13 3
A addConfigurators() 0 4 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 LogicException;
22
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
23
use function array_merge;
24
25
/**
26
 * @package Limoncello\Core
27
 *
28
 * @method string getCallableToCacheMessage();
29
 */
30
trait HasConfiguratorsTrait
31
{
32
    /**
33
     * @var callable[]
34
     */
35
    private $configurators = [];
36
37
    /**
38
     * @param callable[] $configurators
39
     *
40
     * @return self
41
     */
42 21
    public function setConfigurators(array $configurators): self
43
    {
44 21
        foreach ($configurators as $configurator) {
45 18
            $isValid = $this->checkPublicStaticCallable($configurator, [LimoncelloContainerInterface::class]);
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...
46 18
            if ($isValid === false) {
47 18
                throw new LogicException($this->getCallableToCacheMessage());
48
            }
49
        }
50
51 19
        $this->configurators = $configurators;
52
53 19
        return $this;
54
    }
55
56
    /**
57
     * @param callable[] $configurators
58
     *
59
     * @return self
60
     */
61 1
    public function addConfigurators(array $configurators): self
62
    {
63 1
        return $this->setConfigurators(array_merge($this->configurators, $configurators));
64
    }
65
}
66