MarketBook   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 29.84 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 37
loc 124
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getResults() 0 13 1
A getMarketBookFilterByMarketIds() 12 12 1
A getMarketBookFilterByMarketIdsWithPriceData() 13 13 1
A getMarketBookFilterByMarketIdsWithPriceProjection() 12 12 1
A withMarketIds() 0 5 1
A withPriceProjection() 0 5 1
A withOrderProjection() 0 5 1
A withMatchProjection() 0 5 1
A withCurrencyCode() 0 5 1
A withLocale() 0 4 1
A restoreDefaults() 0 8 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
 * This file is part of the Betfair library.
4
 *
5
 * (c) Daniele D'Angeli <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Betfair\BettingApi\MarketBook;
11
12
use Betfair\AbstractBetfair;
13
use Betfair\Adapter\AdapterInterface;
14
use Betfair\Client\BetfairClientInterface;
15
use Betfair\Factory\MarketFilterFactoryInterface;
16
use Betfair\Factory\ParamFactoryInterface;
17
use Betfair\Model\PriceProjection;
18
19
class MarketBook extends AbstractBetfair
20
{
21
    const API_METHOD_NAME = "listMarketBook";
22
23
    private $marketIds = [];
24
    private $priceProjection;
25
    private $orderProjection;
26
    private $matchProjection;
27
    private $currencyCode;
28
    private $locale;
29
30
    /**
31
     * @param BetfairClientInterface $betfairClient
32
     * @param AdapterInterface $adapter
33
     * @param ParamFactoryInterface $paramFactory
34
     * @param MarketFilterFactoryInterface $marketFilterFactory
35
     */
36
    public function __construct(
37
        BetfairClientInterface $betfairClient,
38
        AdapterInterface $adapter,
39
        ParamFactoryInterface $paramFactory,
40
        MarketFilterFactoryInterface $marketFilterFactory
41
    ) {
42
        parent::__construct($betfairClient, $adapter, $paramFactory, $marketFilterFactory);
43
    }
44
45
    public function getResults()
46
    {
47
        $param = $this->createParam();
48
        $param->setMarketIds($this->marketIds)
49
            ->setPriceProjection($this->priceProjection)
0 ignored issues
show
Bug introduced by
It seems like $this->priceProjection can be null; however, setPriceProjection() 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...
50
            ->setOrderProjection($this->orderProjection)
51
            ->setMarketProjection($this->matchProjection)
0 ignored issues
show
Documentation introduced by
$this->matchProjection is of type null, but the function expects a array.

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...
52
            ->setCurrencyCode($this->currencyCode)
53
            ->setLocale($this->locale);
54
55
        $this->restoreDefaults();
56
        return $this->executeCustomQuery($param, self::API_METHOD_NAME);
57
    }
58
59 View Code Duplication
    public function getMarketBookFilterByMarketIds(array $marketIds)
0 ignored issues
show
Duplication introduced by
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...
60
    {
61
        $param = $this->createParam();
62
63
        $param->setMarketIds($marketIds);
64
65
        $this->restoreDefaults();
66
67
        return $this->adapter->adaptResponse(
68
            $this->apiNgRequest(self::API_METHOD_NAME, $param)
69
        );
70
    }
71
72 View Code Duplication
    public function getMarketBookFilterByMarketIdsWithPriceData(array $marketIds, array $priceData)
0 ignored issues
show
Duplication introduced by
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...
73
    {
74
        $param = $this->createParam();
75
76
        $param->setMarketIds($marketIds)
77
            ->setPriceProjection(new PriceProjection($priceData));
78
79
        $this->restoreDefaults();
80
81
        return $this->adapter->adaptResponse(
82
            $this->apiNgRequest(self::API_METHOD_NAME, $param)
83
        );
84
    }
85
86 View Code Duplication
    public function getMarketBookFilterByMarketIdsWithPriceProjection(array $marketIds, PriceProjection $priceProjection)
0 ignored issues
show
Duplication introduced by
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...
87
    {
88
        $param = $this->createParam()
89
            ->setMarketIds($marketIds)
90
            ->setPriceProjection($priceProjection);
91
92
        $this->restoreDefaults();
93
94
        return $this->adapter->adaptResponse(
95
            $this->apiNgRequest(self::API_METHOD_NAME, $param)
96
        );
97
    }
98
99
    public function withMarketIds($marketIds)
100
    {
101
        $this->marketIds = $marketIds;
102
        return $marketIds;
103
    }
104
105
    public function withPriceProjection(PriceProjection $priceProjection)
106
    {
107
        $this->priceProjection = $priceProjection;
108
        return $this;
109
    }
110
111
    public function withOrderProjection($orderProjection)
112
    {
113
        $this->orderProjection = $orderProjection;
114
        return $this;
115
    }
116
117
    public function withMatchProjection($matchProjection)
118
    {
119
        $this->matchProjection = $matchProjection;
120
        return $this;
121
    }
122
123
    public function withCurrencyCode($currencyCode)
124
    {
125
        $this->currencyCode = $currencyCode;
126
        return $this;
127
    }
128
129
    public function withLocale($locale)
130
    {
131
        $this->locale = $locale;
132
    }
133
134
    private function restoreDefaults()
135
    {
136
        $this->marketIds = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $marketIds.

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...
137
        $this->priceProjection = null;
138
        $this->orderProjection = null;
139
        $this->matchProjection = null;
140
        $this->currencyCode = null;
141
    }
142
}
143