Completed
Push — master ( 3f204e...7dc412 )
by Beñat
01:49
created

UndoUpvoteAnswer   C

Complexity

Total Complexity 66

Size/Duplication

Total Lines 493
Duplicated Lines 13.39 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 66
lcom 1
cbo 1
dl 66
loc 493
rs 5.7474
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 9 1
A url() 0 4 1
A mergeAuthenticationIntoParameters() 0 4 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like UndoUpvoteAnswer 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 UndoUpvoteAnswer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace BenatEspina\StackExchangeApiClient\Api\Answer;
15
16
use BenatEspina\StackExchangeApiClient\Api\AnswerApi;
17
use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
18
use BenatEspina\StackExchangeApiClient\Http\HttpClient;
19
use BenatEspina\StackExchangeApiClient\Serializer\Serializer;
20
21
/**
22
 * https://api.stackexchange.com/docs/undo-upvote-answer.
23
 *
24
 * @author Beñat Espiña <[email protected]>
25
 */
26
class UndoUpvoteAnswer
27
{
28
    private const URL = '/answers/{id}/upvote/undo';
29
30
    private $client;
31
    private $serializer;
32
    private $authentication;
33
34
    public function __construct(HttpClient $client, Serializer $serializer, Authentication $authentication)
35
    {
36
        $this->client = $client;
37
        $this->serializer = $serializer;
38
        $this->authentication = $authentication;
39
    }
40
41
    public function __invoke(string $id, array $parameters = AnswerApi::QUERY_PARAMS)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
42
    {
43
        return $this->serializer->serialize(
44
            $this->client->put(
45
                $this->url($id),
46
                $this->mergeAuthenticationIntoParameters($parameters)
47
            )
48
        );
49
    }
50
51
    private function url(string $id) : string
52
    {
53
        return str_replace('{id}', $id, self::URL);
54
    }
55
56
    private function mergeAuthenticationIntoParameters(array $parameters) : array
57
    {
58
        return array_merge($parameters, $this->authentication->toArray());
59
    }
60
}
61