Completed
Push — master ( 113bf8...019067 )
by Neomerx
04:28
created

HasConfiguratorsTrait::addConfigurators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 1
crap 2
1
<?php namespace Limoncello\Core\Routing\Traits;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use LogicException;
20
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
21
22
/**
23
 * @package Limoncello\Core
24
 *
25
 * @method string getCallableToCacheMessage();
26
 */
27
trait HasConfiguratorsTrait
28
{
29
    /**
30
     * @var callable[]
31
     */
32
    private $configurators = [];
33
34
    /**
35
     * @param callable[] $configurators
36
     *
37
     * @return self
38
     */
39 21
    public function setConfigurators(array $configurators): self
40
    {
41 21
        foreach ($configurators as $configurator) {
42 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...
43 18
            if ($isValid === false) {
44 18
                throw new LogicException($this->getCallableToCacheMessage());
45
            }
46
        }
47
48 19
        $this->configurators = $configurators;
49
50 19
        return $this;
51
    }
52
53
    /**
54
     * @param callable[] $configurators
55
     *
56
     * @return self
57
     */
58
    public function addConfigurators(array $configurators): self
59
    {
60
        return $this->setConfigurators(array_merge($this->configurators, $configurators));
61
    }
62
}
63