Complex classes like ToCSSVisitor 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.
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 ToCSSVisitor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class ToCSSVisitor extends Visitor |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * The context. |
||
| 37 | * |
||
| 38 | * @var Context |
||
| 39 | */ |
||
| 40 | protected $context; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Is replacing flag. |
||
| 44 | * |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $isReplacing = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Charset flag. |
||
| 51 | * |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | public $charset = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor. |
||
| 58 | * |
||
| 59 | * @param Context $context |
||
| 60 | */ |
||
| 61 | public function __construct(Context $context) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the context. |
||
| 69 | * |
||
| 70 | * @return Context |
||
| 71 | */ |
||
| 72 | public function getContext() |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Visits a rule node. |
||
| 79 | * |
||
| 80 | * @param RuleNode $node The node |
||
| 81 | * @param VisitorArguments $arguments The arguments |
||
| 82 | * |
||
| 83 | * @return array|RuleNode |
||
| 84 | */ |
||
| 85 | public function visitRule(RuleNode $node, VisitorArguments $arguments) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Visits a mixin definition. |
||
| 96 | * |
||
| 97 | * @param MixinDefinitionNode $node The node |
||
| 98 | * @param VisitorArguments $arguments The arguments |
||
| 99 | * |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public function visitMixinDefinition(MixinDefinitionNode $node, VisitorArguments $arguments) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Visits a extend node. |
||
| 111 | * |
||
| 112 | * @param ExtendNode $node The node |
||
| 113 | * @param VisitorArguments $arguments The arguments |
||
| 114 | */ |
||
| 115 | public function visitExtend(ExtendNode $node, VisitorArguments $arguments) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Visits a comment node. |
||
| 121 | * |
||
| 122 | * @param CommentNode $node The node |
||
| 123 | * @param VisitorArguments $arguments The arguments |
||
| 124 | * |
||
| 125 | * @return CommentNode|null |
||
| 126 | */ |
||
| 127 | public function visitComment(CommentNode $node, VisitorArguments $arguments) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Visits a media node. |
||
| 138 | * |
||
| 139 | * @param MediaNode $node The node |
||
| 140 | * @param VisitorArguments $arguments The arguments |
||
| 141 | * |
||
| 142 | * @return MediaNode|null |
||
| 143 | */ |
||
| 144 | public function visitMedia(MediaNode $node, VisitorArguments $arguments) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Visits a import node. |
||
| 158 | * |
||
| 159 | * @param ImportNode $node The node |
||
| 160 | * @param VisitorArguments $arguments The arguments |
||
| 161 | * |
||
| 162 | * @return ImportNode|null |
||
| 163 | */ |
||
| 164 | public function visitImport(ImportNode $node, VisitorArguments $arguments) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Visits a directive node. |
||
| 175 | * |
||
| 176 | * @param DirectiveNode $node The node |
||
| 177 | * @param VisitorArguments $arguments The arguments |
||
| 178 | * |
||
| 179 | * @return DirectiveNode|null |
||
| 180 | */ |
||
| 181 | public function visitDirective(DirectiveNode $node, VisitorArguments $arguments) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param DirectiveNode $node |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | private function hasVisibleChild(DirectiveNode $node) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Visits a ruleset node. |
||
| 271 | * |
||
| 272 | * @param RulesetNode $node The node |
||
| 273 | * @param VisitorArguments $arguments The arguments |
||
| 274 | * |
||
| 275 | * @return array|RulesetNode |
||
| 276 | */ |
||
| 277 | public function visitRuleset(RulesetNode $node, VisitorArguments $arguments) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Visits anonymous node. |
||
| 360 | * |
||
| 361 | * @param AnonymousNode $node |
||
| 362 | * @param VisitorArguments $arguments |
||
| 363 | */ |
||
| 364 | public function visitAnonymous(AnonymousNode $node, VisitorArguments $arguments) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Checks properties for presence in selector blocks. |
||
| 377 | * |
||
| 378 | * @param array $rules |
||
| 379 | * |
||
| 380 | * @throws CompilerException |
||
| 381 | */ |
||
| 382 | private function checkPropertiesInRoot($rules) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Merges rules. |
||
| 398 | * |
||
| 399 | * @param array $rules |
||
| 400 | */ |
||
| 401 | private function mergeRules(array &$rules) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Removes duplicates. |
||
| 443 | * |
||
| 444 | * @param array $rules |
||
| 445 | */ |
||
| 446 | private function removeDuplicateRules(array &$rules) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param $values |
||
| 474 | * |
||
| 475 | * @return ExpressionNode |
||
| 476 | */ |
||
| 477 | private function toExpression($values) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @param $values |
||
| 489 | * |
||
| 490 | * @return ValueNode |
||
| 491 | */ |
||
| 492 | private function toValue($values) |
||
| 496 | } |
||
| 497 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: