Builder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 128
Duplicated Lines 7.81 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 10
loc 128
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 6 1
A useHttpClient() 10 10 3
A useRequestFactory() 0 6 1
A useSimpleCache() 0 6 1
A build() 0 14 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
declare(strict_types=1);
4
5
/*
6
 * This file is part of Swap.
7
 *
8
 * (c) Florian Voutzinos <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Swap;
15
16
use Exchanger\Exchanger;
17
use Exchanger\Service\Chain;
18
use Http\Client\HttpClient;
19
use Http\Message\RequestFactory;
20
use Psr\SimpleCache\CacheInterface;
21
use Swap\Service\Factory;
22
use Psr\Http\Client\ClientInterface;
23
use Psr\Http\Message\RequestFactoryInterface;
24
25
/**
26
 * Helps building Swap.
27
 *
28
 * @author Florian Voutzinos <[email protected]>
29
 */
30
final class Builder
31
{
32
    /**
33
     * The services.
34
     *
35
     * @var array
36
     */
37
    private $services = [];
38
39
    /**
40
     * The options.
41
     *
42
     * @var array
43
     */
44
    private $options = [];
45
46
    /**
47
     * The http client.
48
     *
49
     * @var HttpClient
50
     */
51
    private $httpClient;
52
53
    /**
54
     * The request factory.
55
     *
56
     * @var RequestFactory
57
     */
58
    private $requestFactory;
59
60
    /**
61
     * The cache.
62
     *
63
     * @var CacheInterface
64
     */
65
    private $cache;
66
67
    /**
68
     * Constructor.
69
     *
70
     * @param array $options
71
     */
72
    public function __construct(array $options = [])
73
    {
74
        $this->options = $options;
75
    }
76
77
    /**
78
     * Adds a service.
79
     *
80
     * @param string $serviceName
81
     * @param array  $options
82
     *
83
     * @return Builder
84
     */
85
    public function add(string $serviceName, array $options = []): self
86
    {
87
        $this->services[$serviceName] = $options;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Uses the given http client.
94
     *
95
     * @param HttpClient|ClientInterface $httpClient
96
     *
97
     * @return Builder
98
     */
99 View Code Duplication
    public function useHttpClient($httpClient): self
0 ignored issues
show
Duplication introduced by florianv
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...
100
    {
101
        if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) {
102
            throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
103
        }
104
105
        $this->httpClient = $httpClient;
0 ignored issues
show
Documentation Bug introduced by florianv
$httpClient is of type object<Psr\Http\Client\ClientInterface>, but the property $httpClient was declared to be of type object<Http\Client\HttpClient>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
106
107
        return $this;
108
    }
109
110
    /**
111
     * Uses the given request factory.
112
     *
113
     * @param RequestFactoryInterface $requestFactory
114
     *
115
     * @return Builder
116
     */
117
    public function useRequestFactory(RequestFactoryInterface $requestFactory): self
118
    {
119
        $this->requestFactory = $requestFactory;
0 ignored issues
show
Documentation Bug introduced by florianv
It seems like $requestFactory of type object<Psr\Http\Message\RequestFactoryInterface> is incompatible with the declared type object<Http\Message\RequestFactory> of property $requestFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
120
121
        return $this;
122
    }
123
124
    /**
125
     * Uses the given simple cache.
126
     *
127
     * @param CacheInterface $cache
128
     *
129
     * @return Builder
130
     */
131
    public function useSimpleCache(CacheInterface $cache): self
132
    {
133
        $this->cache = $cache;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Builds Swap.
140
     *
141
     * @return Swap
142
     */
143
    public function build(): Swap
144
    {
145
        $serviceFactory = new Factory($this->httpClient, $this->requestFactory);
0 ignored issues
show
Documentation introduced by florianv
$this->requestFactory is of type object<Http\Message\RequestFactory>, but the function expects a null|object<Psr\Http\Mes...equestFactoryInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
146
        $services = [];
147
148
        foreach ($this->services as $name => $options) {
149
            $services[] = $serviceFactory->create($name, $options);
150
        }
151
152
        $service = new Chain($services);
153
        $exchanger = new Exchanger($service, $this->cache, $this->options);
154
155
        return new Swap($exchanger);
156
    }
157
}
158