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 declare(strict_types=1); |
||
21 | final class Client implements ClientInterface |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * @var LoopInterface |
||
26 | */ |
||
27 | protected $loop; |
||
28 | |||
29 | /** |
||
30 | * @var AsyncClient |
||
31 | */ |
||
32 | protected $asyncClient; |
||
33 | |||
34 | /** |
||
35 | * @var FoundationClient |
||
36 | */ |
||
37 | protected $client; |
||
38 | |||
39 | /** |
||
40 | * @var StreamingClient |
||
41 | */ |
||
42 | protected $streamingClient; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $options; |
||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | private $consumerKey; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $consumerSecret; |
||
57 | |||
58 | 1 | public function __construct( |
|
59 | string $consumerKey, |
||
60 | string $consumerSecret |
||
61 | ) { |
||
62 | 1 | $this->consumerKey = $consumerKey; |
|
63 | 1 | $this->consumerSecret = $consumerSecret; |
|
64 | 1 | $this->loop = LoopFactory::create(); |
|
65 | |||
66 | 1 | $this->options = ApiSettings::getOptions( |
|
67 | 1 | $consumerKey, |
|
68 | 1 | $consumerSecret, |
|
69 | 1 | 'Sync' |
|
70 | ); |
||
71 | |||
72 | 1 | $this->client = Factory::create($this->loop, $this->options); |
|
73 | |||
74 | 1 | $this->asyncClient = new AsyncClient($consumerKey, $consumerSecret, $this->loop, [], $this->client); |
|
75 | 1 | } |
|
76 | |||
77 | 1 | public function withAccessToken(string $accessToken, string $accessTokenSecret): Client |
|
97 | |||
98 | 1 | public function withOutAccessToken(): Client |
|
99 | { |
||
100 | 1 | $options = $this->options; |
|
101 | // @codingStandardsIgnoreStart |
||
102 | 1 | View Code Duplication | if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) { |
|
|||
103 | unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]); |
||
104 | } |
||
105 | 1 | View Code Duplication | if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) { |
106 | unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]); |
||
107 | } |
||
108 | // @codingStandardsIgnoreEnd |
||
109 | |||
110 | 1 | $clone = clone $this; |
|
111 | 1 | $clone->client = Factory::create($this->loop, $options); |
|
112 | 1 | $clone->asyncClient = (new AsyncClient( |
|
113 | 1 | $this->consumerKey, |
|
114 | 1 | $this->consumerSecret, |
|
115 | 1 | $this->loop, |
|
116 | 1 | [], |
|
117 | 1 | $this->client |
|
118 | )); |
||
119 | |||
120 | 1 | return $clone; |
|
121 | } |
||
122 | |||
123 | public function stream(): StreamingClient |
||
135 | |||
136 | public function tweet(string $tweet): TweetInterface |
||
143 | |||
144 | public function profile(): ProfileInterface |
||
153 | |||
154 | public function user(string $tweet): UserInterface |
||
161 | } |
||
162 |
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.