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 |
||
17 | abstract class Resource implements Arrayable |
||
18 | { |
||
19 | use DateTimeFormatter; |
||
20 | |||
21 | const RESOURCE = ''; |
||
22 | const RESOURCE_COLLECTION = ''; |
||
23 | const RESOURCE_ID_REPLACE = '{id}'; |
||
24 | const RESOURCE_REPLACE = '{account_id}'; |
||
25 | |||
26 | private $properties = []; |
||
27 | |||
28 | /** @var TwitterAds $twitterAds */ |
||
29 | private $twitterAds; |
||
30 | |||
31 | abstract public function getId(); |
||
32 | |||
33 | /** |
||
34 | * Automatically set the account if this class is not an account |
||
35 | * Resource constructor. |
||
36 | * @param null $id |
||
37 | * @param TwitterAds $twitterAds |
||
38 | */ |
||
39 | public function __construct($id = null, TwitterAds $twitterAds = null) |
||
44 | |||
45 | protected static function assureApi(TwitterAds $instance = null) { |
||
54 | |||
55 | /** |
||
56 | * Returns a Cursor instance for a given resource. |
||
57 | * |
||
58 | * @param $params |
||
59 | * |
||
60 | * @return Cursor |
||
61 | */ |
||
62 | public function all($params = []) |
||
69 | |||
70 | /** |
||
71 | * @param $params |
||
72 | * @return Resource |
||
73 | */ |
||
74 | public function read($params = []) |
||
78 | |||
79 | /** |
||
80 | * Returns an object instance for a given resource. |
||
81 | * |
||
82 | * @param string $id |
||
83 | * @param $params |
||
84 | * |
||
85 | * @return Resource |
||
86 | */ |
||
87 | View Code Duplication | public function load($id, $params = []) |
|
95 | |||
96 | /** |
||
97 | * Reloads all attributes for the current object instance from the API. |
||
98 | * |
||
99 | * @param $params |
||
100 | * @return Resource |
||
101 | * @throws ServerError |
||
102 | */ |
||
103 | public function reload($params = []) |
||
115 | |||
116 | /** |
||
117 | * Populates a given objects attributes from a parsed JSON API response. |
||
118 | * This helper handles all necessary type coercions as it assigns attribute values. |
||
119 | * |
||
120 | * @param $response |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function fromResponse($response) |
||
136 | |||
137 | /** |
||
138 | * Generates a Hash of property values for the current object. This helper |
||
139 | * handles all necessary type coercions as it generates its output. |
||
140 | */ |
||
141 | public function toParams() |
||
161 | |||
162 | public function validateLoaded() |
||
168 | |||
169 | public function toArray() |
||
183 | |||
184 | public function loadResource($id = '', $params = []) |
||
192 | |||
193 | /** |
||
194 | * Saves or updates the current object instance depending on the |
||
195 | * presence of `object->getId()`. |
||
196 | */ |
||
197 | public function save() |
||
210 | |||
211 | /** |
||
212 | * Deletes the current object instance depending on the |
||
213 | * presence of `object->getId()`. |
||
214 | */ |
||
215 | View Code Duplication | public function delete() |
|
222 | |||
223 | /** |
||
224 | * @return array |
||
225 | */ |
||
226 | public function getProperties() |
||
230 | |||
231 | |||
232 | /** |
||
233 | * @return TwitterAds |
||
234 | */ |
||
235 | public function getTwitterAds() |
||
239 | } |
||
240 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: