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 |
||
42 | class D7Model extends BaseBitrixModel |
||
43 | { |
||
44 | const TABLE_CLASS = null; |
||
45 | |||
46 | /** |
||
47 | * @var null|string |
||
48 | */ |
||
49 | protected static $cachedTableClass = null; |
||
50 | |||
51 | /** |
||
52 | * Adapter to interact with Bitrix D7 API. |
||
53 | * |
||
54 | * @var D7Adapter |
||
55 | */ |
||
56 | protected static $adapter; |
||
57 | |||
58 | /** |
||
59 | * Constructor. |
||
60 | * |
||
61 | * @param $id |
||
62 | * @param $fields |
||
63 | */ |
||
64 | public function __construct($id = null, $fields = null) |
||
70 | |||
71 | /** |
||
72 | * Setter for adapter (for testing) |
||
73 | * @param $adapter |
||
74 | */ |
||
75 | public static function setAdapter($adapter) |
||
79 | |||
80 | /** |
||
81 | * Instantiate adapter if it's not instantiated. |
||
82 | * |
||
83 | * @return D7Adapter |
||
84 | */ |
||
85 | public static function instantiateAdapter() |
||
93 | |||
94 | /** |
||
95 | * Instantiate a query object for the model. |
||
96 | * |
||
97 | * @return D7Query |
||
98 | */ |
||
99 | public static function query() |
||
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | * @throws LogicException |
||
107 | */ |
||
108 | public static function tableClass() |
||
117 | |||
118 | /** |
||
119 | * Cached version of table class. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public static function cachedTableClass() |
||
131 | |||
132 | /** |
||
133 | * Internal part of create to avoid problems with static and inheritance |
||
134 | * |
||
135 | * @param $fields |
||
136 | * |
||
137 | * @throws ExceptionFromBitrix |
||
138 | * |
||
139 | * @return static|bool |
||
140 | */ |
||
141 | protected static function internalCreate($fields) |
||
162 | |||
163 | /** |
||
164 | * Delete model |
||
165 | * |
||
166 | * @return bool |
||
167 | * @throws ExceptionFromBitrix |
||
168 | */ |
||
169 | public function delete() |
||
185 | |||
186 | /** |
||
187 | * Save model to database. |
||
188 | * |
||
189 | * @param array $selectedFields save only these fields instead of all. |
||
190 | * @return bool |
||
191 | * @throws ExceptionFromBitrix |
||
192 | */ |
||
193 | public function save($selectedFields = []) |
||
215 | |||
216 | /** |
||
217 | * Determine whether the field should be stopped from passing to "update". |
||
218 | * |
||
219 | * @param string $field |
||
220 | * @param mixed $value |
||
221 | * @param array $selectedFields |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | protected function fieldShouldNotBeSaved($field, $value, $selectedFields) |
||
229 | |||
230 | /** |
||
231 | * Throw bitrix exception on fail |
||
232 | * |
||
233 | * @param \Bitrix\Main\Entity\Result $resultObject |
||
234 | * @throws ExceptionFromBitrix |
||
235 | */ |
||
236 | protected function throwExceptionOnFail($resultObject) |
||
242 | |||
243 | /** |
||
244 | * Set eventErrors field on error. |
||
245 | * |
||
246 | * @param \Bitrix\Main\Entity\Result $resultObject |
||
247 | */ |
||
248 | protected function setEventErrorsOnFail($resultObject) |
||
254 | } |
||
255 |
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.