Completed
Push — master ( dd0bc2...b5fd08 )
by Oscar
03:27
created

HandlerTrait   C

Complexity

Total Complexity 76

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 76
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 48
rs 5.4881

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A handler() 0 6 1
A executeHandler() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like HandlerTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use HandlerTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Utilities used by middlewares with handler options.
10
 */
11
trait HandlerTrait
12
{
13
    use CallableTrait;
14
15
    /**
16
     * @var callable|string|null The handler used
17
     */
18
    protected $handler;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param callable|string|null $handler
24
     */
25
    public function __construct($handler = null)
26
    {
27
        if ($handler !== null) {
28
            $this->handler($handler);
29
        }
30
    }
31
32
    /**
33
     * Set the handler.
34
     *
35
     * @param string|callable $handler
36
     *
37
     * @return self
38
     */
39
    public function handler($handler)
40
    {
41
        $this->handler = $handler;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Execute the target.
48
     *
49
     * @param RequestInterface  $request
50
     * @param ResponseInterface $response
51
     *
52
     * @return ResponseInterface
53
     */
54
    protected function executeHandler(RequestInterface $request, ResponseInterface $response)
55
    {
56
        return $this->executeCallable($this->handler, $request, $response);
57
    }
58
}
59