Complex classes like Meta 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 Meta, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Meta extends CApplicationComponent |
||
| 16 | {
|
||
| 17 | const VARIABLE_PATTERN = "/{([\w:]+)}/";
|
||
| 18 | |||
| 19 | const COMMAND_PATTERN = "/([a-z]+)\(([^()]+)\)/"; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | public $exceptedModels = array('ProductAssignment', 'ProductParameterAssignment', 'ProductTreeAssignment');
|
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | public $replaces = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | public $maxSearchDepth = 1; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var FController |
||
| 38 | */ |
||
| 39 | private $controller; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $route; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $requestUri; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var FActiveRecord[] |
||
| 53 | */ |
||
| 54 | private $renderedModels = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $renderedClips = array(); |
||
| 60 | |||
| 61 | private $header; |
||
| 62 | |||
| 63 | private $title; |
||
| 64 | |||
| 65 | private $description; |
||
| 66 | |||
| 67 | private $keywords; |
||
| 68 | |||
| 69 | private $custom; |
||
| 70 | |||
| 71 | private $noindex; |
||
| 72 | |||
| 73 | 2 | public function init() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | 18 | public function getTitle() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 3 | public function getDescription() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | 3 | public function getKeywords() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | 7 | public function getCustom() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param $header |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | 6 | public function setHeader($header) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @param FController $controller |
||
| 144 | */ |
||
| 145 | 7 | public function setController(FController $controller) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $uri |
||
| 156 | */ |
||
| 157 | 10 | public function setRequestUri($uri) |
|
| 161 | |||
| 162 | 14 | public function setMeta() |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Собираем все модели, переданные в render() |
||
| 188 | * |
||
| 189 | * @param CEvent $event |
||
| 190 | */ |
||
| 191 | 2 | public function setRenderedModels(CEvent $event) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param FActiveRecord[] $models |
||
| 201 | */ |
||
| 202 | 3 | public function addModels(array $models) |
|
| 206 | |||
| 207 | 10 | public function registerClip($id, $value) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Запись в базу моделей, найденных во время рендеринга |
||
| 217 | */ |
||
| 218 | 1 | public function updateRenderedModels() |
|
| 231 | |||
| 232 | 2 | public function registerMeta() |
|
| 245 | |||
| 246 | 7 | private function setRoute() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Удаление переменных моделей в строке метатегов |
||
| 254 | * |
||
| 255 | * @param string $string |
||
| 256 | * |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | 19 | private function clear($string) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param FActiveRecord[] $data |
||
| 271 | */ |
||
| 272 | 5 | private function processModels(array $data) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param FActiveRecord $model |
||
| 303 | */ |
||
| 304 | 2 | private function processRelations(FActiveRecord $model) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Замена встречающиеся переменных на значения свойств $model |
||
| 320 | * |
||
| 321 | * @param $string |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | 19 | private function replaceVariables($string) |
|
| 344 | |||
| 345 | 19 | private function replaceCommands($string) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param $value |
||
| 365 | * @param $replace |
||
| 366 | */ |
||
| 367 | 17 | private function processValue($value, $replace) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $command |
||
| 383 | * @param string $args |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | 5 | private function processCommand($command, $args) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @param FActiveRecord $model |
||
| 422 | * @param string $property |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | 2 | private function getPropertyValue($model, $property) |
|
| 430 | } |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.