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 |
||
11 | class PulseColumnTextValue extends PulseColumnValue |
||
12 | { |
||
13 | /** |
||
14 | * Get a text column's content |
||
15 | * |
||
16 | * @api |
||
17 | * |
||
18 | * @since 0.1.0 |
||
19 | * |
||
20 | * @return string|null The column's content |
||
21 | */ |
||
22 | 2 | View Code Duplication | public function getValue () |
|
|||
23 | { |
||
24 | 2 | if ($this->isNullValue()) |
|
25 | { |
||
26 | 1 | return null; |
|
27 | } |
||
28 | |||
29 | 2 | if (!isset($this->column_value)) |
|
30 | { |
||
31 | 1 | $this->setValue($this->jsonResponse); |
|
32 | } |
||
33 | |||
34 | 2 | return $this->column_value; |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * Update the text of a text column |
||
39 | * |
||
40 | * @api |
||
41 | * |
||
42 | * @param string $text |
||
43 | * |
||
44 | * @since 0.3.0 \InvalidArgumentException is now thrown |
||
45 | * @since 0.1.0 |
||
46 | * |
||
47 | * @throws \InvalidArgumentException if $text does not have a string representation |
||
48 | */ |
||
49 | 1 | public function updateValue ($text) |
|
50 | { |
||
51 | 1 | if (!is_scalar($text) || (is_object($text) && method_exists($text, '__toString'))) |
|
52 | { |
||
53 | throw new \InvalidArgumentException('$text is expected to have a string representation'); |
||
54 | } |
||
55 | |||
56 | 1 | $url = sprintf("%s/%d/columns/%s/text.json", self::apiEndpoint(), $this->board_id, $this->column_id); |
|
57 | $postParams = array( |
||
58 | 1 | "pulse_id" => $this->pulse_id, |
|
59 | 1 | "text" => (string)$text |
|
60 | ); |
||
61 | |||
62 | 1 | $result = self::sendPut($url, $postParams); |
|
63 | 1 | $this->setValue($result); |
|
64 | 1 | } |
|
65 | |||
66 | 2 | protected function setValue ($response) |
|
70 | } |
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.