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 AsyncClient implements AsyncClientInterface |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * @var Client |
||
26 | */ |
||
27 | protected $client; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $options; |
||
33 | |||
34 | /** |
||
35 | * @var AsyncStreamingClient |
||
36 | */ |
||
37 | protected $streamingClient; |
||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $consumerKey; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $consumerSecret; |
||
47 | |||
48 | /** |
||
49 | * @var LoopInterface |
||
50 | */ |
||
51 | private $loop; |
||
52 | |||
53 | /** |
||
54 | * AsyncClient constructor. |
||
55 | * @param string $consumerKey |
||
56 | * @param string $consumerSecret |
||
57 | * @param LoopInterface $loop |
||
58 | * @param array $options |
||
59 | * @param Client|null $client |
||
60 | */ |
||
61 | 2 | public function __construct( |
|
62 | string $consumerKey, |
||
63 | string $consumerSecret, |
||
64 | LoopInterface $loop, |
||
65 | array $options = [], |
||
66 | Client $client = null |
||
67 | ) { |
||
68 | 2 | $this->consumerKey = $consumerKey; |
|
69 | 2 | $this->consumerSecret = $consumerSecret; |
|
70 | 2 | $this->loop = $loop; |
|
71 | |||
72 | 2 | if (!($client instanceof Client)) { |
|
73 | 2 | $this->options = ApiSettings::getOptions( |
|
74 | 2 | $consumerKey, |
|
75 | 2 | $consumerSecret, |
|
76 | 2 | 'Async', |
|
77 | 2 | $options |
|
78 | ); |
||
79 | |||
80 | 2 | $client = Factory::create($this->loop, $this->options); |
|
81 | } |
||
82 | |||
83 | 2 | $this->client = $client; |
|
84 | 2 | } |
|
85 | |||
86 | 2 | public function withAccessToken(string $accessToken, string $accessTokenSecret): AsyncClient |
|
87 | { |
||
88 | 2 | $options = $this->options; |
|
89 | // @codingStandardsIgnoreStart |
||
90 | 2 | $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken); |
|
91 | 2 | $options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret); |
|
92 | // @codingStandardsIgnoreEnd |
||
93 | |||
94 | 2 | return new self( |
|
95 | 2 | $this->consumerKey, |
|
96 | 2 | $this->consumerSecret, |
|
97 | 2 | $this->loop, |
|
98 | 2 | $options |
|
99 | ); |
||
100 | } |
||
101 | |||
102 | 1 | public function withOutAccessToken(): AsyncClient |
|
103 | { |
||
104 | 1 | $options = $this->options; |
|
105 | // @codingStandardsIgnoreStart |
||
106 | 1 | View Code Duplication | if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) { |
107 | 1 | unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]); |
|
108 | } |
||
109 | 1 | View Code Duplication | if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) { |
110 | 1 | unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]); |
|
111 | } |
||
112 | // @codingStandardsIgnoreEnd |
||
113 | |||
114 | 1 | return new self( |
|
115 | 1 | $this->consumerKey, |
|
116 | 1 | $this->consumerSecret, |
|
117 | 1 | $this->loop, |
|
118 | 1 | $options |
|
119 | ); |
||
120 | } |
||
121 | |||
122 | public function getCommandBus(): CommandBusInterface |
||
126 | |||
127 | View Code Duplication | public function profile(): PromiseInterface |
|
128 | { |
||
129 | return $this->client->handle(new RequestCommand( |
||
130 | new Request('GET', 'account/verify_credentials.json') |
||
131 | ))->then(function (ResponseInterface $response) { |
||
132 | return resolve($this->client->handle(new HydrateCommand('Profile', $response->getBody()->getParsedContents()))); |
||
133 | }); |
||
134 | } |
||
135 | |||
136 | View Code Duplication | public function user(string $user): PromiseInterface |
|
144 | |||
145 | public function tweet(string $status, array $tweet = []): PromiseInterface |
||
146 | { |
||
155 | |||
156 | public function stream(): AsyncStreamingClientInterface |
||
164 | } |
||
165 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: