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 | class ElementQuery extends BaseQuery |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * CIblock object or test double. |
||
| 21 | * |
||
| 22 | * @var object. |
||
| 23 | */ |
||
| 24 | public static $cIblockObject; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Query sort. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | public $sort = ['SORT' => 'ASC']; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Query group by. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | public $groupBy = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Iblock id. |
||
| 42 | * |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $iblockId; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * List of standard entity fields. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $standardFields = [ |
||
| 53 | 'ID', |
||
| 54 | 'TIMESTAMP_X', |
||
| 55 | 'TIMESTAMP_X_UNIX', |
||
| 56 | 'MODIFIED_BY', |
||
| 57 | 'DATE_CREATE', |
||
| 58 | 'DATE_CREATE_UNIX', |
||
| 59 | 'CREATED_BY', |
||
| 60 | 'IBLOCK_ID', |
||
| 61 | 'IBLOCK_SECTION_ID', |
||
| 62 | 'ACTIVE', |
||
| 63 | 'ACTIVE_FROM', |
||
| 64 | 'ACTIVE_TO', |
||
| 65 | 'SORT', |
||
| 66 | 'NAME', |
||
| 67 | 'PREVIEW_PICTURE', |
||
| 68 | 'PREVIEW_TEXT', |
||
| 69 | 'PREVIEW_TEXT_TYPE', |
||
| 70 | 'DETAIL_PICTURE', |
||
| 71 | 'DETAIL_TEXT', |
||
| 72 | 'DETAIL_TEXT_TYPE', |
||
| 73 | 'SEARCHABLE_CONTENT', |
||
| 74 | 'IN_SECTIONS', |
||
| 75 | 'SHOW_COUNTER', |
||
| 76 | 'SHOW_COUNTER_START', |
||
| 77 | 'CODE', |
||
| 78 | 'TAGS', |
||
| 79 | 'XML_ID', |
||
| 80 | 'EXTERNAL_ID', |
||
| 81 | 'TMP_ID', |
||
| 82 | 'CREATED_USER_NAME', |
||
| 83 | 'DETAIL_PAGE_URL', |
||
| 84 | 'LIST_PAGE_URL', |
||
| 85 | 'CREATED_DATE', |
||
| 86 | ]; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Constructor. |
||
| 90 | * |
||
| 91 | * @param object $bxObject |
||
| 92 | * @param string $modelName |
||
| 93 | */ |
||
| 94 | public function __construct($bxObject, $modelName) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Instantiate bitrix entity object. |
||
| 104 | * |
||
| 105 | * @throws Exception |
||
| 106 | * |
||
| 107 | * @return object |
||
| 108 | */ |
||
| 109 | public static function instantiateCIblockObject() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Setter for groupBy. |
||
| 124 | * |
||
| 125 | * @param $value |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function groupBy($value) |
||
| 135 | |||
| 136 | public function cache($ttl) |
||
| 137 | { |
||
| 138 | $this->ttl = $ttl; |
||
|
|
|||
| 139 | |||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get list of items. |
||
| 145 | * |
||
| 146 | * @return Collection |
||
| 147 | */ |
||
| 148 | View Code Duplication | private function getListSimple() |
|
| 170 | |||
| 171 | public function getList() |
||
| 172 | { |
||
| 173 | if (!$this->ttl) return $this->getListSimple(); |
||
| 174 | |||
| 175 | $cache = Cache::createInstance(); |
||
| 176 | if ($cache->initCache($this->ttl, $this->getKey(), '/models/')) { |
||
| 177 | $result = $cache->getVars(); |
||
| 178 | } elseif ($cache->startDataCache()) { |
||
| 179 | $result = $this->getListSimple(); |
||
| 180 | $cache->endDataCache($result); |
||
| 181 | } |
||
| 182 | |||
| 183 | return $result; |
||
| 184 | } |
||
| 185 | |||
| 186 | private function getKey() |
||
| 187 | { |
||
| 188 | $data = [ |
||
| 189 | 'sort' => $this->sort, |
||
| 190 | 'filter' => $this->normalizeFilter(), |
||
| 191 | 'groupBy' => $this->groupBy, |
||
| 192 | 'navigation' => $this->navigation, |
||
| 193 | 'select' => $this->normalizeSelect(), |
||
| 194 | ]; |
||
| 195 | |||
| 196 | return md5( json_encode($data) ); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the first element with a given code. |
||
| 201 | * |
||
| 202 | * @param string $code |
||
| 203 | * |
||
| 204 | * @return ElementModel |
||
| 205 | */ |
||
| 206 | public function getByCode($code) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get the first element with a given external id. |
||
| 215 | * |
||
| 216 | * @param string $id |
||
| 217 | * |
||
| 218 | * @return ElementModel |
||
| 219 | */ |
||
| 220 | public function getByExternalId($id) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get count of elements that match $filter. |
||
| 229 | * |
||
| 230 | * @return int |
||
| 231 | */ |
||
| 232 | public function count() |
||
| 240 | |||
| 241 | // /** |
||
| 242 | // * Normalize properties's format converting it to 'PROPERTY_"CODE"_VALUE'. |
||
| 243 | // * |
||
| 244 | // * @param array $fields |
||
| 245 | // * |
||
| 246 | // * @return null |
||
| 247 | // */ |
||
| 248 | // protected function normalizePropertyResultFormat(&$fields) |
||
| 249 | // { |
||
| 250 | // if (empty($fields['PROPERTIES'])) { |
||
| 251 | // return; |
||
| 252 | // } |
||
| 253 | // |
||
| 254 | // foreach ($fields['PROPERTIES'] as $code => $prop) { |
||
| 255 | // $fields['PROPERTY_'.$code.'_VALUE'] = $prop['VALUE']; |
||
| 256 | // $fields['~PROPERTY_'.$code.'_VALUE'] = $prop['~VALUE']; |
||
| 257 | // $fields['PROPERTY_'.$code.'_DESCRIPTION'] = $prop['DESCRIPTION']; |
||
| 258 | // $fields['~PROPERTY_'.$code.'_DESCRIPTION'] = $prop['~DESCRIPTION']; |
||
| 259 | // $fields['PROPERTY_'.$code.'_VALUE_ID'] = $prop['PROPERTY_VALUE_ID']; |
||
| 260 | // if (isset($prop['VALUE_ENUM_ID'])) { |
||
| 261 | // $fields['PROPERTY_'.$code.'_ENUM_ID'] = $prop['VALUE_ENUM_ID']; |
||
| 262 | // } |
||
| 263 | // } |
||
| 264 | // } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Normalize filter before sending it to getList. |
||
| 268 | * This prevents some inconsistency. |
||
| 269 | * |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | protected function normalizeFilter() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Normalize select before sending it to getList. |
||
| 281 | * This prevents some inconsistency. |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | View Code Duplication | protected function normalizeSelect() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Add all iblock property codes to select. |
||
| 303 | * |
||
| 304 | * return null |
||
| 305 | */ |
||
| 306 | protected function addAllPropsToSelect() |
||
| 317 | } |
||
| 318 |
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: