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 |
||
30 | class PluginProvider implements Provider |
||
31 | { |
||
32 | /** |
||
33 | * @var Provider |
||
34 | */ |
||
35 | private $provider; |
||
36 | |||
37 | /** |
||
38 | * @var Plugin[] |
||
39 | */ |
||
40 | private $plugins; |
||
41 | |||
42 | /** |
||
43 | * A list of options. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private $options; |
||
48 | |||
49 | /** |
||
50 | * @param Provider $provider |
||
51 | * @param Plugin[] $plugins |
||
52 | * @param array $options { |
||
53 | * |
||
54 | * @var int $max_restarts |
||
55 | * } |
||
56 | */ |
||
57 | 6 | public function __construct(Provider $provider, array $plugins = [], array $options = []) |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 5 | View Code Duplication | public function geocodeQuery(GeocodeQuery $query): Collection |
|
|||
68 | { |
||
69 | 5 | $pluginChain = $this->createPluginChain($this->plugins, function (GeocodeQuery $query) { |
|
70 | try { |
||
71 | 4 | return new GeocoderFulfilledPromise($this->provider->geocodeQuery($query)); |
|
72 | 1 | } catch (Exception $exception) { |
|
73 | 1 | return new GeocoderRejectedPromise($exception); |
|
74 | } |
||
75 | 5 | }); |
|
76 | |||
77 | 5 | return $pluginChain($query)->wait(); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 2 | View Code Duplication | public function reverseQuery(ReverseQuery $query): Collection |
84 | { |
||
85 | 2 | $pluginChain = $this->createPluginChain($this->plugins, function (ReverseQuery $query) { |
|
86 | try { |
||
87 | 2 | return new GeocoderFulfilledPromise($this->provider->reverseQuery($query)); |
|
88 | } catch (Exception $exception) { |
||
89 | return new GeocoderRejectedPromise($exception); |
||
90 | } |
||
91 | 2 | }); |
|
92 | |||
93 | 2 | return $pluginChain($query)->wait(); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function getName(): string |
||
103 | |||
104 | /** |
||
105 | * Configure the plugin provider. |
||
106 | * |
||
107 | * @param array $options |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | 6 | private function configure(array $options = []): array |
|
126 | |||
127 | /** |
||
128 | * Create the plugin chain. |
||
129 | * |
||
130 | * @param Plugin[] $pluginList A list of plugins |
||
131 | * @param callable $clientCallable Callable making the HTTP call |
||
132 | * |
||
133 | * @return callable |
||
134 | */ |
||
135 | 6 | private function createPluginChain(array $pluginList, callable $clientCallable) |
|
160 | } |
||
161 |
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.