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 |
||
24 | class PulseBoard extends ApiObject |
||
25 | { |
||
26 | /** |
||
27 | * The suffix that is appended to the URL to access functionality for certain objects |
||
28 | * |
||
29 | * @internal |
||
30 | */ |
||
31 | const API_PREFIX = "boards"; |
||
32 | |||
33 | // ================================================================================================================ |
||
34 | // Instance Variables |
||
35 | // ================================================================================================================ |
||
36 | |||
37 | /** |
||
38 | * The resource's URL. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $url; |
||
43 | |||
44 | /** |
||
45 | * The board's unique identifier. |
||
46 | * |
||
47 | * @var int |
||
48 | */ |
||
49 | protected $id; |
||
50 | |||
51 | /** |
||
52 | * The board's name. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $name; |
||
57 | |||
58 | /** |
||
59 | * The board's description. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $description; |
||
64 | |||
65 | /** |
||
66 | * The board's visible columns. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $columns; |
||
71 | |||
72 | /** |
||
73 | * The board's visible groups. |
||
74 | * |
||
75 | * @var array |
||
76 | */ |
||
77 | protected $groups; |
||
78 | |||
79 | /** |
||
80 | * Creation time. |
||
81 | * |
||
82 | * @var \DateTime |
||
83 | */ |
||
84 | protected $created_at; |
||
85 | |||
86 | /** |
||
87 | * Last update time. |
||
88 | * |
||
89 | * @var \DateTime |
||
90 | */ |
||
91 | protected $updated_at; |
||
92 | |||
93 | /** |
||
94 | * Whether or not groups have been fetched. Group data comes from both a unique API call and from the initial call |
||
95 | * of getting the board data, so this data is merged; this boolean is to avoid fetching this data twice. |
||
96 | * |
||
97 | * @var bool |
||
98 | */ |
||
99 | private $groupsFetched = false; |
||
100 | |||
101 | // ================================================================================================================ |
||
102 | // Getter functions |
||
103 | // ================================================================================================================ |
||
104 | |||
105 | /** |
||
106 | * The resource's URL. |
||
107 | * |
||
108 | * @api |
||
109 | * @since 0.1.0 |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getUrl() |
||
113 | { |
||
114 | return $this->url; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * The board's unique identifier. |
||
119 | * |
||
120 | * @api |
||
121 | * @since 0.1.0 |
||
122 | * @return int |
||
123 | */ |
||
124 | public function getId() |
||
125 | { |
||
126 | return $this->id; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * The board's name. |
||
131 | * |
||
132 | * @api |
||
133 | * @since 0.1.0 |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getName() |
||
137 | { |
||
138 | return $this->name; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * The board's description. |
||
143 | * |
||
144 | * @api |
||
145 | * @since 0.1.0 |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getDescription() |
||
149 | { |
||
150 | return $this->description; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * The board's visible columns. |
||
155 | * |
||
156 | * @api |
||
157 | * @since 0.1.0 |
||
158 | * @return PulseColumn[] |
||
159 | */ |
||
160 | public function getColumns() |
||
161 | { |
||
162 | self::lazyInject($this->columns, array( |
||
163 | "board_id" => $this->getId() |
||
164 | )); |
||
165 | self::lazyArray($this->columns, "PulseColumn"); |
||
166 | |||
167 | return $this->columns; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Creation time. |
||
172 | * |
||
173 | * @api |
||
174 | * @since 0.1.0 |
||
175 | * @return \DateTime |
||
176 | */ |
||
177 | public function getCreatedAt() |
||
178 | { |
||
179 | self::lazyLoad($this->created_at, "DateTime"); |
||
180 | |||
181 | return $this->created_at; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Last update time. |
||
186 | * |
||
187 | * @api |
||
188 | * @since 0.1.0 |
||
189 | * @return \DateTime |
||
190 | */ |
||
191 | public function getUpdatedAt() |
||
192 | { |
||
193 | self::lazyLoad($this->updated_at, "DateTime"); |
||
194 | |||
195 | return $this->updated_at; |
||
196 | } |
||
197 | |||
198 | // ================================================================================================================ |
||
199 | // Columns functions |
||
200 | // ================================================================================================================ |
||
201 | |||
202 | /** |
||
203 | * Create a new column for the current board. |
||
204 | * |
||
205 | * If you are creating a status column, use the constants available in the **PulseColumnColorValue** class to match the |
||
206 | * colors. Keep in mind this array cannot have a key higher than 11 nor can it be an associative array. Here's an |
||
207 | * example of how to match statuses with specific colors. |
||
208 | * |
||
209 | * ```php |
||
210 | * $labels = array( |
||
211 | * PulseColumnColorValue::Orange => "Working on it", |
||
212 | * PulseColumnColorValue::L_Green => "Done", |
||
213 | * PulseColumnColorValue::Red => "Delayed" |
||
214 | * ); |
||
215 | * ``` |
||
216 | * |
||
217 | * @api |
||
218 | * |
||
219 | * @param string $title The title of the column. This title will automatically be "slugified" and become the ID |
||
220 | * of the column. |
||
221 | * @param string $type The type of value that this column will use. Either use the available constants in the |
||
222 | * PulseColumn class or use the following strings: "date", "person", "status", "text". |
||
223 | * @param array $labels If the column type will be "status," then this array will be the values for each of the |
||
224 | * colors. |
||
225 | * |
||
226 | * @see PulseColumn::Date PulseColumn::Date |
||
227 | * @see PulseColumn::Person PulseColumn::Person |
||
228 | * @see PulseColumn::Status PulseColumn::Status |
||
229 | * @see PulseColumn::Text PulseColumn::Text |
||
230 | * @see PulseColumnColorValue::Orange PulseColumnColorValue::Orange |
||
231 | * @see PulseColumnColorValue::L_Green PulseColumnColorValue::L_Green |
||
232 | * @see PulseColumnColorValue::Red PulseColumnColorValue::Red |
||
233 | * @see PulseColumnColorValue::Blue PulseColumnColorValue::Blue |
||
234 | * @see PulseColumnColorValue::Purple PulseColumnColorValue::Purple |
||
235 | * @see PulseColumnColorValue::Grey PulseColumnColorValue::Grey |
||
236 | * @see PulseColumnColorValue::Green PulseColumnColorValue::Green |
||
237 | * @see PulseColumnColorValue::L_Blue PulseColumnColorValue::L_Blue |
||
238 | * @see PulseColumnColorValue::Gold PulseColumnColorValue::Gold |
||
239 | * @see PulseColumnColorValue::Yellow PulseColumnColorValue::Yellow |
||
240 | * @see PulseColumnColorValue::Black PulseColumnColorValue::Black |
||
241 | * |
||
242 | * @since 0.1.0 |
||
243 | * |
||
244 | * @throws ArgumentMismatchException Status definitions were defined yet the type of the column was not a status |
||
245 | * type column |
||
246 | * @throws InvalidArraySizeException The array containing the value of statuses has a key larger than the |
||
247 | * supported 10 indices |
||
248 | * |
||
249 | * @return $this This instance will be updated to have updated information to reflect the new column that was |
||
250 | * created |
||
251 | */ |
||
252 | public function createColumn ($title, $type, $labels = array()) |
||
253 | { |
||
254 | if ($type !== PulseColumn::Status && !empty($labels)) |
||
255 | { |
||
256 | throw new ArgumentMismatchException("No color definitions are required for a non-color column."); |
||
257 | } |
||
258 | |||
259 | if ($type === PulseColumn::Status && count($labels) > 0 && max(array_keys($labels)) > 10) |
||
260 | { |
||
261 | throw new InvalidArraySizeException("The range of status can only be from 0-10."); |
||
262 | } |
||
263 | |||
264 | $url = sprintf("%s/%d/columns.json", parent::apiEndpoint(), $this->getId()); |
||
265 | $postParams = array( |
||
266 | "title" => $title, |
||
267 | "type" => $type |
||
268 | ); |
||
269 | |||
270 | self::setIfNotNullOrEmpty($postParams, "labels", $labels); |
||
271 | |||
272 | $this->jsonResponse = self::sendPost($url, $postParams); |
||
273 | $this->assignResults(); |
||
274 | |||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | // ================================================================================================================ |
||
279 | // Group functions |
||
280 | // ================================================================================================================ |
||
281 | |||
282 | /** |
||
283 | * Get all of the groups belonging to a board. |
||
284 | * |
||
285 | * A group is defined as the colorful headers that split up pulses into categories. |
||
286 | * |
||
287 | * @api |
||
288 | * |
||
289 | * @param bool $showArchived Set to true if you would like to get archived groups in a board as well |
||
290 | * |
||
291 | * @since 0.1.0 |
||
292 | * |
||
293 | * @return PulseGroup[] |
||
294 | */ |
||
295 | public function getGroups ($showArchived = false) |
||
309 | |||
310 | public function createGroup ($title) |
||
311 | { |
||
312 | $url = sprintf("%s/%s/groups.json", parent::apiEndpoint(), $this->getId()); |
||
313 | $postParams = array("title" => $title); |
||
314 | |||
315 | // The API doesn't return the board ID, so since we have access to it here: set it manually |
||
316 | $groupResult = self::sendPost($url, $postParams); |
||
317 | $groupResult["board_id"] = $this->getId(); |
||
318 | |||
319 | return (new PulseGroup($groupResult)); |
||
320 | } |
||
321 | |||
322 | private function fetchGroups ($showArchived) |
||
323 | { |
||
324 | $url = sprintf("%s/%s/groups.json", parent::apiEndpoint(), $this->getId()); |
||
325 | |||
326 | $this->groupsFetched = true; |
||
327 | |||
328 | return self::sendGet($url, array("show_archived" => $showArchived)); |
||
329 | } |
||
330 | |||
331 | // ================================================================================================================ |
||
332 | // Pulse functions |
||
333 | // ================================================================================================================ |
||
334 | |||
335 | /** |
||
336 | * @return Pulse[] |
||
337 | */ |
||
338 | public function getPulses () |
||
339 | { |
||
340 | $url = sprintf("%s/%d/pulses.json", parent::apiEndpoint(), $this->getId()); |
||
341 | $data = self::sendGet($url); |
||
342 | $pulses = array(); |
||
343 | |||
344 | foreach ($data as $entry) |
||
345 | { |
||
346 | $this->pulseInjection($entry); |
||
347 | |||
348 | $pulses[] = new Pulse($entry["pulse"]); |
||
349 | } |
||
350 | |||
351 | return $pulses; |
||
352 | } |
||
353 | |||
354 | public function createPulse ($name, $owner, $group_id = null) |
||
355 | { |
||
356 | $url = sprintf("%s/%d/pulses.json", parent::apiEndpoint(), $this->getId()); |
||
357 | $postParams = array( |
||
358 | "user_id" => $owner, |
||
359 | "pulse" => array( |
||
360 | "name" => $name |
||
361 | ) |
||
362 | ); |
||
363 | |||
364 | self::setIfNotNullOrEmpty($postParams, "group_id", $group_id); |
||
365 | |||
366 | $result = self::sendPost($url, $postParams); |
||
367 | $this->pulseInjection($result); |
||
368 | |||
369 | return (new Pulse($result["pulse"])); |
||
370 | } |
||
371 | |||
372 | View Code Duplication | private function pulseInjection (&$result) |
|
373 | { |
||
374 | // Inject some information so a Pulse object can survive on its own |
||
375 | $result["pulse"]["group_id"] = $result["board_meta"]["group_id"]; |
||
376 | $result["pulse"]["column_structure"] = $this->getColumns(); |
||
377 | $result["pulse"]["raw_column_values"] = $result["column_values"]; |
||
378 | } |
||
379 | |||
380 | // ================================================================================================================ |
||
381 | // Board functions |
||
382 | // ================================================================================================================ |
||
383 | |||
384 | View Code Duplication | public function archiveBoard () |
|
385 | { |
||
386 | $this->checkInvalid(); |
||
387 | |||
388 | $url = sprintf("%s/%s.json", parent::apiEndpoint(), $this->getId()); |
||
389 | self::sendDelete($url); |
||
390 | |||
391 | $this->deletedObject = true; |
||
392 | } |
||
393 | |||
394 | public static function createBoard ($name, $user_id, $description = NULL) |
||
395 | { |
||
396 | $url = sprintf("%s.json", parent::apiEndpoint()); |
||
397 | $postParams = array( |
||
398 | "user_id" => $user_id, |
||
399 | "name" => $name |
||
400 | ); |
||
401 | |||
402 | self::setIfNotNullOrEmpty($postParams, "description", $description); |
||
403 | |||
404 | $boardResult = self::sendPost($url, $postParams); |
||
405 | |||
406 | return (new PulseBoard($boardResult)); |
||
407 | } |
||
408 | |||
409 | public static function getBoards ($params = array()) |
||
415 | } |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.