HistorySubscriber   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 9.68 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 9
loc 93
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getJournal() 0 4 1
A onRequestCreated() 0 4 1
A onRequestSent() 0 4 1
A onMultiRequestCreated() 0 7 2
A onMultiRequestSent() 9 9 2
A getSubscribedEvents() 0 9 1
A record() 0 7 1

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
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Event\Subscriber;
13
14
use Ivory\HttpAdapter\Event\Events;
15
use Ivory\HttpAdapter\Event\History\Journal;
16
use Ivory\HttpAdapter\Event\History\JournalInterface;
17
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent;
18
use Ivory\HttpAdapter\Event\MultiRequestSentEvent;
19
use Ivory\HttpAdapter\Event\RequestCreatedEvent;
20
use Ivory\HttpAdapter\Event\RequestSentEvent;
21
use Ivory\HttpAdapter\Event\Timer\TimerInterface;
22
use Ivory\HttpAdapter\Message\InternalRequestInterface;
23
use Ivory\HttpAdapter\Message\ResponseInterface;
24
25
/**
26
 * @author GeLo <[email protected]>
27
 */
28
class HistorySubscriber extends AbstractTimerSubscriber
29
{
30
    /**
31
     * @var JournalInterface
32
     */
33
    private $journal;
34
35
    /**
36
     * @param JournalInterface|null $journal
37
     * @param TimerInterface|null   $timer
38
     */
39 45
    public function __construct(JournalInterface $journal = null, TimerInterface $timer = null)
40
    {
41 45
        parent::__construct($timer);
42
43 45
        $this->journal = $journal ?: new Journal();
44 45
    }
45
46
    /**
47
     * @return JournalInterface
48
     */
49 18
    public function getJournal()
50
    {
51 18
        return $this->journal;
52
    }
53
54
    /**
55
     * @param RequestCreatedEvent $event
56
     */
57 9
    public function onRequestCreated(RequestCreatedEvent $event)
58
    {
59 9
        $event->setRequest($this->getTimer()->start($event->getRequest()));
60 9
    }
61
62
    /**
63
     * @param RequestSentEvent $event
64
     */
65 9
    public function onRequestSent(RequestSentEvent $event)
66
    {
67 9
        $event->setRequest($this->record($event->getRequest(), $event->getResponse()));
0 ignored issues
show
Bug introduced by Eric GELOEN
It seems like $event->getResponse() can be null; however, record() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
68 9
    }
69
70
    /**
71
     * @param MultiRequestCreatedEvent $event
72
     */
73 9
    public function onMultiRequestCreated(MultiRequestCreatedEvent $event)
74
    {
75 9
        foreach ($event->getRequests() as $request) {
76 9
            $event->removeRequest($request);
77 9
            $event->addRequest($this->getTimer()->start($request));
78 7
        }
79 9
    }
80
81
    /**
82
     * @param MultiRequestSentEvent $event
83
     */
84 9 View Code Duplication
    public function onMultiRequestSent(MultiRequestSentEvent $event)
0 ignored issues
show
Duplication introduced by Sági-Kazár Márk
This method 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...
85
    {
86 9
        foreach ($event->getResponses() as $response) {
87 9
            $request = $this->record($response->getParameter('request'), $response);
88
89 9
            $event->removeResponse($response);
90 9
            $event->addResponse($response->withParameter('request', $request));
0 ignored issues
show
Compatibility introduced by Eric GELOEN
$response->withParameter('request', $request) of type object<Ivory\HttpAdapter...ssage\MessageInterface> is not a sub-type of object<Ivory\HttpAdapter...sage\ResponseInterface>. It seems like you assume a child interface of the interface Ivory\HttpAdapter\Message\MessageInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
91 7
        }
92 9
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 9
    public static function getSubscribedEvents()
98
    {
99
        return [
100 9
            Events::REQUEST_CREATED       => ['onRequestCreated', 100],
101 7
            Events::REQUEST_SENT          => ['onRequestSent', 100],
102 7
            Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 100],
103 7
            Events::MULTI_REQUEST_SENT    => ['onMultiRequestSent', 100],
104 7
        ];
105
    }
106
107
    /**
108
     * @param InternalRequestInterface $internalRequest
109
     * @param ResponseInterface        $response
110
     *
111
     * @return InternalRequestInterface
112
     */
113 18
    private function record(InternalRequestInterface $internalRequest, ResponseInterface $response)
114
    {
115 18
        $internalRequest = $this->getTimer()->stop($internalRequest);
116 18
        $this->journal->record($internalRequest, $response);
117
118 18
        return $internalRequest;
119
    }
120
}
121