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 |
||
14 | class JsonApiView extends View |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | private $encodingOptions = 0; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | private $contentType = 'application/json'; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | private $dataWraper; |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $metaWrapper; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $dataOnly = false; |
||
43 | |||
44 | /** |
||
45 | * Construct JsonApiView instance |
||
46 | * |
||
47 | * @param string $dataWrapper (optional) Wrapper for data in response |
||
48 | * @param string $metaWrapper (optional) Wrapper for metadata in response |
||
49 | */ |
||
50 | 1 | public function __construct($dataWrapper = null, $metaWrapper = null) |
|
57 | |||
58 | /** |
||
59 | * Set whether to return only the data. |
||
60 | * |
||
61 | * @param bool $dataOnly |
||
62 | */ |
||
63 | public function setDataOnly($dataOnly = true) |
||
67 | |||
68 | /** |
||
69 | * @return int |
||
70 | */ |
||
71 | public function getEncodingOptions(): int |
||
75 | |||
76 | /** |
||
77 | * Bitmask consisting of<br /><br /> |
||
78 | * <b>JSON_HEX_QUOT</b>,<br /> |
||
79 | * <b>JSON_HEX_TAG</b>,<br /> |
||
80 | * <b>JSON_HEX_AMP</b>,<br /> |
||
81 | * <b>JSON_HEX_APOS</b>,<br /> |
||
82 | * <b>JSON_NUMERIC_CHECK</b>,<br /> |
||
83 | * <b>JSON_PRETTY_PRINT</b>,<br /> |
||
84 | * <b>JSON_UNESCAPED_SLASHES</b>,<br /> |
||
85 | * <b>JSON_FORCE_OBJECT</b>,<br /> |
||
86 | * <b>JSON_UNESCAPED_UNICODE</b>.<br /> |
||
87 | * |
||
88 | * <p>The behaviour of these constants is described on the JSON constants page.</p> |
||
89 | * |
||
90 | * @param int $encodingOptions |
||
91 | */ |
||
92 | public function setEncodingOptions($encodingOptions) |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getContentType(): string |
||
104 | |||
105 | /** |
||
106 | * Content-Type sent through the HTTP header. |
||
107 | * |
||
108 | * <p>Default is set to "application/json", append ";charset=UTF-8" to force the charset</p> |
||
109 | * |
||
110 | * @param string $contentType |
||
111 | */ |
||
112 | public function setContentType($contentType) |
||
116 | |||
117 | /** |
||
118 | * @param int $status |
||
119 | * @param mixed $data |
||
120 | * @param string $slimNameInstance |
||
121 | */ |
||
122 | public function render($status = 200, $data = null, $slimNameInstance = 'default') |
||
190 | } |
||
191 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.