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 |
||
12 | class Lifx |
||
13 | { |
||
14 | const API_URL = 'https://api.lifx.com/'; |
||
15 | const API_VERSION = 'v1beta1'; |
||
16 | |||
17 | /** |
||
18 | * @var Client |
||
19 | */ |
||
20 | private $client; |
||
21 | |||
22 | /** |
||
23 | * Instantiates the Guzzle client using the authorization token. |
||
24 | * A token can be obtained at: https://cloud.lifx.com/settings |
||
25 | * |
||
26 | * @param string $token Authorization token for the LIFX HTTP API. |
||
27 | */ |
||
28 | public function __construct($token) |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Sends a request to the LIFX HTTP API. |
||
42 | * |
||
43 | * @param $request |
||
44 | * @param array $options |
||
45 | * @return \Psr\Http\Message\ResponseInterface |
||
46 | */ |
||
47 | public function sendRequest($request, $options = []) |
||
54 | |||
55 | /** |
||
56 | * Gets lights belonging to the authenticated account. |
||
57 | * |
||
58 | * @param string $selector Selector used to filter lights. Defaults to `all`. |
||
59 | * @return \Psr\Http\Message\StreamInterface |
||
60 | */ |
||
61 | View Code Duplication | public function getLights($selector = 'all') |
|
68 | |||
69 | /** |
||
70 | * Toggle off lights if they are on, or turn them on if they are off. |
||
71 | * Physically powered off lights are ignored. |
||
72 | * |
||
73 | * @param string $selector Selector used to filter lights. Defaults to `all`. |
||
74 | * @return \Psr\Http\Message\StreamInterface |
||
75 | */ |
||
76 | View Code Duplication | public function toggleLights($selector = 'all') |
|
83 | |||
84 | /** |
||
85 | * Turn lights on or off. |
||
86 | * |
||
87 | * @param string $selector Selector used to filter lights. Defaults to `all`. |
||
88 | * @param string $state State of 'on' or 'off'. |
||
89 | * @param float $duration (Optional) Fade to the given `state` over a duration of seconds. Defaults to `1.0`. |
||
90 | * @return \Psr\Http\Message\StreamInterface |
||
91 | */ |
||
92 | View Code Duplication | public function setLights($selector = 'all', $state = 'on', $duration = 1.0) |
|
104 | |||
105 | /** |
||
106 | * Set lights to any color. |
||
107 | * |
||
108 | * @param string $selector Selector used to filter lights. Defaults to `all`. |
||
109 | * @param string $color Color the lights should be set to. Defaults to `white`. |
||
110 | * @param float $duration (Optional) Fade to the given `state` over a duration of seconds. Defaults to `1.0`. |
||
111 | * @param bool $power_on (Optional) Whether to turn light on first. Defaults to `true`. |
||
112 | * @return \Psr\Http\Message\StreamInterface |
||
113 | */ |
||
114 | View Code Duplication | public function setColor($selector = 'all', $color = 'white', $duration = 1.0, $power_on = true) |
|
127 | |||
128 | /** |
||
129 | * Performs a breathe effect by slowly fading between the given colors. |
||
130 | * If `from_color` is omitted then the current color is used in its place. |
||
131 | * |
||
132 | * @param string $selector Selector used to filter lights. Defaults to `all`. |
||
133 | * @param string $color Color the lights should be set to. Defaults to `purple`. |
||
134 | * @param string|null $from_color (Optional) From color of the waveform. Defaults to `null`. |
||
135 | * @param float $period (Optional) Period of the waveform in seconds. Defaults to `1.0`. |
||
136 | * @param float $cycles (Optional) Number of times to repeat, cycle counts. Defaults to `1.0`. |
||
137 | * @param bool $persist (Optional) Whether to keep state at the end of the effect. Defaults to `false`. |
||
138 | * @param bool $power_on (Optional) Whether to turn light on first. Defaults to `true`. |
||
139 | * @param float $peak (Optional) Defines where in a period the target color is at its maximum. Defaults to `0.5`. Minimum `0.0`, maximum `1.0`. |
||
140 | * @return \Psr\Http\Message\StreamInterface |
||
141 | */ |
||
142 | View Code Duplication | public function breatheLights( |
|
167 | |||
168 | /** |
||
169 | * Performs a pulse effect by quickly flashing between the given colors. |
||
170 | * If `from_color` is omitted then the current color is used in its place. |
||
171 | * |
||
172 | * @param string $selector Selector used to filter lights. Defaults to `all`. |
||
173 | * @param string $color Color the lights should be set to. Defaults to `purple`. |
||
174 | * @param string|null $from_color (Optional) From color of the waveform. Defaults to `null`. |
||
175 | * @param float $period (Optional) Period of the waveform in seconds. Defaults to `1.0`. |
||
176 | * @param float $cycles (Optional) Number of times to repeat, cycle counts. Defaults to `1.0`. |
||
177 | * @param bool $persist (Optional) Whether to keep state at the end of the effect. Defaults to `false`. |
||
178 | * @param bool $power_on (Optional) Whether to turn light on first. Defaults to `true`. |
||
179 | * @param float $duty_cycle (Optional) Ratio of the period where color is active. Defaults to `0.5`. Minimum `0.0`, maximum `1.0`. |
||
180 | * @return \Psr\Http\Message\StreamInterface |
||
181 | */ |
||
182 | View Code Duplication | public function pulseLights( |
|
208 | } |