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 |
||
18 | class PulseColumnNumericValue extends PulseColumnValue |
||
19 | { |
||
20 | /** |
||
21 | * Get a numeric column's content |
||
22 | * |
||
23 | * @api |
||
24 | * |
||
25 | * @since 0.4.0 ColumnNotFoundException is now thrown |
||
26 | * @since 0.3.0 Docs correctly specify a numeric return type |
||
27 | * @since 0.2.0 |
||
28 | * |
||
29 | * @throws ColumnNotFoundException The specified column ID does not exist for the parent Pulse |
||
30 | * |
||
31 | * @return int|double|null Null is returned if there is no value set for this column |
||
32 | */ |
||
33 | 3 | public function getValue () |
|
34 | { |
||
35 | 3 | return parent::getValue(); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Update the value of a numeric column |
||
40 | * |
||
41 | * @api |
||
42 | * |
||
43 | * @param int|double $number |
||
44 | * |
||
45 | * @since 0.3.0 \InvalidArgumentException is now thrown |
||
46 | * @since 0.2.0 |
||
47 | * |
||
48 | * @throws \InvalidArgumentException if $number is not a numeric value |
||
49 | */ |
||
50 | 5 | View Code Duplication | public function updateValue ($number) |
|
|||
51 | { |
||
52 | 5 | if (!is_numeric($number)) |
|
53 | { |
||
54 | 4 | throw new \InvalidArgumentException('$number is expected to be a numeric type'); |
|
55 | } |
||
56 | |||
57 | 1 | $url = sprintf("%s/%d/columns/%s/numeric.json", self::apiEndpoint(), $this->board_id, $this->column_id); |
|
58 | $postParams = [ |
||
59 | 1 | "pulse_id" => $this->pulse_id, |
|
60 | 1 | "value" => $number |
|
61 | ]; |
||
62 | |||
63 | 1 | $result = self::sendPut($url, $postParams); |
|
64 | 1 | $this->setValue($result); |
|
65 | 1 | } |
|
66 | |||
67 | 2 | protected function setValue ($response) |
|
71 | } |
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.