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 |
||
| 43 | class D7Model extends BaseBitrixModel |
||
| 44 | { |
||
| 45 | const TABLE_CLASS = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var null|string |
||
| 49 | */ |
||
| 50 | protected static $cachedTableClasses = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Array of adapters for each model to interact with Bitrix D7 API. |
||
| 54 | * |
||
| 55 | * @var D7Adapter[] |
||
| 56 | */ |
||
| 57 | protected static $adapters = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructor. |
||
| 61 | * |
||
| 62 | * @param $id |
||
| 63 | * @param $fields |
||
| 64 | */ |
||
| 65 | public function __construct($id = null, $fields = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Setter for adapter (for testing) |
||
| 74 | * @param $adapter |
||
| 75 | */ |
||
| 76 | public static function setAdapter($adapter) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Instantiate adapter if it's not instantiated. |
||
| 83 | * |
||
| 84 | * @return D7Adapter |
||
| 85 | */ |
||
| 86 | public static function instantiateAdapter() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Instantiate a query object for the model. |
||
| 98 | * |
||
| 99 | * @return D7Query |
||
| 100 | */ |
||
| 101 | public static function query() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return string |
||
| 108 | * @throws LogicException |
||
| 109 | */ |
||
| 110 | public static function tableClass() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Cached version of table class. |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public static function cachedTableClass() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Internal part of create to avoid problems with static and inheritance |
||
| 137 | * |
||
| 138 | * @param $fields |
||
| 139 | * |
||
| 140 | * @throws ExceptionFromBitrix |
||
| 141 | * |
||
| 142 | * @return static|bool |
||
| 143 | */ |
||
| 144 | protected static function internalCreate($fields) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Delete model |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | * @throws ExceptionFromBitrix |
||
| 171 | */ |
||
| 172 | public function delete() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Save model to database. |
||
| 191 | * |
||
| 192 | * @param array $selectedFields save only these fields instead of all. |
||
| 193 | * @return bool |
||
| 194 | * @throws ExceptionFromBitrix |
||
| 195 | */ |
||
| 196 | public function save($selectedFields = []) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Determine whether the field should be stopped from passing to "update". |
||
| 223 | * |
||
| 224 | * @param string $field |
||
| 225 | * @param mixed $value |
||
| 226 | * @param array $selectedFields |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Throw bitrix exception on fail |
||
| 237 | * |
||
| 238 | * @param \Bitrix\Main\Entity\Result $resultObject |
||
| 239 | * @throws ExceptionFromBitrix |
||
| 240 | */ |
||
| 241 | protected function throwExceptionOnFail($resultObject) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Set eventErrors field on error. |
||
| 250 | * |
||
| 251 | * @param \Bitrix\Main\Entity\Result $resultObject |
||
| 252 | */ |
||
| 253 | protected function setEventErrorsOnFail($resultObject) |
||
| 259 | } |
||
| 260 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.