HasRequestFactoryTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 43
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequestFactory() 0 12 3
A isRequestFactorySet() 0 4 1
A getDefaultRequestFactory() 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 Limoncello\Contracts\Core\SapiInterface;
22
use Limoncello\Core\Application\Application;
23
use LogicException;
24
use Psr\Container\ContainerInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
27
/**
28
 * @package Limoncello\Core
29
 *
30
 * @method string getCallableToCacheMessage();
31
 */
32
trait HasRequestFactoryTrait
33
{
34
    /**
35
     * @var callable|false|null
36
     */
37
    private $requestFactory = false;
38
39
    /**
40
     * @param callable|null $requestFactory
41
     *
42
     * @return self
43
     */
44 22
    public function setRequestFactory(callable $requestFactory = null): self
45
    {
46 22
        $parameters = [SapiInterface::class, ContainerInterface::class];
47 22
        if ($requestFactory !== null &&
48 22
            $this->checkPublicStaticCallable($requestFactory, $parameters, ServerRequestInterface::class) === false
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
        ) {
50 2
            throw new LogicException($this->getCallableToCacheMessage());
51
        }
52 20
        $this->requestFactory = $requestFactory;
53
54 20
        return $this;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60 17
    protected function isRequestFactorySet()
61
    {
62 17
        return $this->requestFactory !== false;
63
    }
64
65
    /**
66
     * @return callable
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
67
     *
68
     * @SuppressWarnings(PHPMD.StaticAccess)
69
     */
70 8
    protected function getDefaultRequestFactory(): callable
71
    {
72 8
        return Application::getDefaultRequestFactory();
73
    }
74
}
75