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:
Complex classes like BController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class BController extends CController |
||
| 14 | {
|
||
| 15 | public $enabled = true; |
||
| 16 | |||
| 17 | public $position = 0; |
||
| 18 | |||
| 19 | public $name = '[Не задано]'; |
||
| 20 | |||
| 21 | public $layout = '//layouts/column1'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | public $breadcrumbs = array(); |
||
| 27 | |||
| 28 | public $modelClass = 'BActiveRecord'; |
||
| 29 | |||
| 30 | public $popup = false; |
||
| 31 | |||
| 32 | public $moduleMenu; |
||
| 33 | |||
| 34 | public $showInMenu = true; |
||
| 35 | |||
| 36 | 40 | public function behaviors() |
|
| 43 | |||
| 44 | public function getViewPath() |
||
| 56 | |||
| 57 | public function filters() |
||
| 63 | |||
| 64 | 1 | public function beforeAction($action) |
|
| 96 | |||
| 97 | 1 | public function getBackUrl() |
|
| 105 | |||
| 106 | 6 | public function actions() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Делаем редирект. |
||
| 122 | * Если приложение запущено с тестовым конфигом, то бросаем эксепшн |
||
| 123 | * |
||
| 124 | * @param mixed $url |
||
| 125 | * @param bool $terminate |
||
| 126 | * @param integer $statusCode |
||
| 127 | * |
||
| 128 | * @throws BTestRedirectException |
||
| 129 | */ |
||
| 130 | 4 | public function redirect($url, $terminate = true, $statusCode = 302) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $view |
||
| 144 | * @param null $data |
||
| 145 | * @param bool $return |
||
| 146 | * |
||
| 147 | * @return string|void |
||
| 148 | */ |
||
| 149 | 1 | public function render($view, $data = null, $return = false) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param $id |
||
| 163 | * @param string $modelClass |
||
| 164 | * |
||
| 165 | * @return mixed |
||
| 166 | * @throws CHttpException |
||
| 167 | */ |
||
| 168 | 8 | public function loadModel($id, $modelClass = null) |
|
| 178 | |||
| 179 | 1 | public function actionIndex() |
|
| 188 | |||
| 189 | public function actionCreate() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param $id |
||
| 198 | */ |
||
| 199 | public function actionUpdate($id) |
||
| 203 | |||
| 204 | public function isUpdate() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Стандартный автокомплит для моделей |
||
| 211 | * |
||
| 212 | * $_GET['model'] - название модели для поиска |
||
| 213 | * $_GET['field'] - поле для поиска |
||
| 214 | * $_GET['q'] - значение |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | public function actionAutocomplete() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @return BActiveRecord |
||
| 246 | */ |
||
| 247 | 1 | protected function createFilterModel() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param BActiveRecord $model |
||
| 261 | */ |
||
| 262 | 1 | protected function saveModel($model) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Проводим валидацию и сохраняем несколько связанных моделей |
||
| 280 | * Все модели должны быть связаны по первичному ключу |
||
| 281 | * |
||
| 282 | * @param BActiveRecord[] $models |
||
| 283 | * @param bool $extendedSave пытаемся сохранить все данные post, вызывая соответствующие методы контроллера |
||
| 284 | * @param bool $redirectToUpdate перенаправление на action update |
||
| 285 | * |
||
| 286 | * @throws CDbException |
||
| 287 | * @throws CHttpException |
||
| 288 | */ |
||
| 289 | 1 | protected function saveModels($models, $extendedSave = true, $redirectToUpdate = true) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param BActiveRecord[] $models |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | 2 | protected function validateModels($models) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param BActiveRecord $model |
||
| 352 | * |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | 2 | protected function validateRelatedModels($model) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param BActiveRecord $model |
||
| 376 | */ |
||
| 377 | 2 | protected function redirectAfterSave($model) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @param BActiveRecord $model |
||
| 394 | */ |
||
| 395 | protected function actionSave($model) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Performs the AJAX validation. |
||
| 403 | * |
||
| 404 | * @param BActiveRecord $model the model to be validated |
||
| 405 | */ |
||
| 406 | 3 | protected function performAjaxValidation($model) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @param BActiveRecord[] $models |
||
| 417 | */ |
||
| 418 | 3 | protected function performAjaxValidationForSeveralModels($models) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Проверям наличие в контроллере методов, чтобы сохранить данные из post |
||
| 437 | * |
||
| 438 | * @param array $unsavedKeys |
||
| 439 | * @param BActiveRecord $primaryModel |
||
| 440 | * |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | 1 | protected function saveMaximumPostData(array $unsavedKeys, BActiveRecord $primaryModel) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Возвращает массив разрешенных для сохнанеия релейшенов в формате 'relationName' => 'postPrefix' |
||
| 467 | * пример: array('variants' => 'BProductParamVariant').
|
||
| 468 | * Если в котроллере есть метод с именем save{postPrefix}($data, BActiveRecord $parentModel),
|
||
| 469 | * то для сохнания данных $_POST[{postPrefix}} будет вызван он.
|
||
| 470 | * |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | 2 | protected function getModelsAllowedForSave() |
|
| 477 | } |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: