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) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns a Cursor instance for a given resource. |
||
| 58 | * |
||
| 59 | * @param $params |
||
| 60 | * |
||
| 61 | * @return Cursor |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function all($params = []) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param $params |
||
| 73 | * @return Resource |
||
| 74 | */ |
||
| 75 | public function read($params = []) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns an object instance for a given resource. |
||
| 83 | * |
||
| 84 | * @param string $id |
||
| 85 | * @param $params |
||
| 86 | * |
||
| 87 | * @return Resource |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function load($id, $params = []) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Reloads all attributes for the current object instance from the API. |
||
| 100 | * |
||
| 101 | * @param $params |
||
| 102 | * @return Resource |
||
| 103 | * @throws ServerError |
||
| 104 | */ |
||
| 105 | public function reload($params = []) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Populates a given objects attributes from a parsed JSON API response. |
||
| 120 | * This helper handles all necessary type coercions as it assigns attribute values. |
||
| 121 | * |
||
| 122 | * @param $response |
||
| 123 | * |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | public function fromResponse($response) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Generates a Hash of property values for the current object. This helper |
||
| 141 | * handles all necessary type coercions as it generates its output. |
||
| 142 | */ |
||
| 143 | public function toParams() |
||
| 163 | |||
| 164 | public function validateLoaded() |
||
| 170 | |||
| 171 | public function toArray() |
||
| 184 | |||
| 185 | public function loadResource($id = '', $params = []) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Saves or updates the current object instance depending on the |
||
| 196 | * presence of `object->getId()`. |
||
| 197 | */ |
||
| 198 | public function save() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Deletes the current object instance depending on the |
||
| 214 | * presence of `object->getId()`. |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function delete() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function getProperties() |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * @return TwitterAds |
||
| 235 | */ |
||
| 236 | public function getTwitterAds() |
||
| 240 | } |
||
| 241 |
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: