Completed
Push — master ( 509b14...929e7e )
by Barney
04:52 queued 03:02
created

StopFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
wmc 4
dl 52
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A stop() 8 8 1
A levelStop() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @codingStandardsIgnoreStart
4
 *
5
 * @author       Barney Hanlon <[email protected]>
6
 * @copyright    Barney Hanlon 2017
7
 * @license      https://opensource.org/licenses/MIT
8
 *
9
 * @codingStandardsIgnoreEnd
10
 */
11
12
namespace Shrikeh\GuzzleMiddleware\TimerLogger\Formatter;
13
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Log\LogLevel;
17
use Shrikeh\GuzzleMiddleware\TimerLogger\Timer\TimerInterface;
18
19
/**
20
 * Class StopFormatter.
21
 */
22 View Code Duplication
class StopFormatter implements RequestStopInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    /**
25
     * @var callable
26
     */
27
    private $msg;
28
29
    /**
30
     * @var string|callable
31
     */
32
    private $level;
33
34
    /**
35
     * StartFormatter constructor.
36
     *
37
     * @param callable        $msg   A callable to format the messages
38
     * @param callable|string $level The log level for when the timer ends
39
     */
40
    public function __construct(callable $msg, $level = LogLevel::DEBUG)
41
    {
42
        $this->msg = $msg;
43
        $this->level = $level;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function stop(
50
        TimerInterface $timer,
51
        RequestInterface $request,
52
        ResponseInterface $response
53
    ) {
54
        $msg = $this->msg;
55
56
        return $msg($timer, $request, $response);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function levelStop(
63
        TimerInterface $timer,
64
        RequestInterface $request,
65
        ResponseInterface $response
66
    ) {
67
        $level = $this->level;
68
69
        if (is_callable($level)) {
70
            $level = $level($timer, $request, $response);
71
        }
72
73
        return $level;
74
    }
75
}
76