Complex classes like ImportVisitor 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 ImportVisitor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class ImportVisitor extends Visitor |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * The importer. |
||
| 33 | * |
||
| 34 | * @var Importer |
||
| 35 | */ |
||
| 36 | protected $importer; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Finished flag. |
||
| 40 | * |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $isFinished = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The context. |
||
| 47 | * |
||
| 48 | * @var Context |
||
| 49 | */ |
||
| 50 | protected $context; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Exception |
||
| 54 | */ |
||
| 55 | protected $error; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int |
||
| 59 | */ |
||
| 60 | private $importCount = 0; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $type = VisitorInterface::TYPE_PRE_COMPILE; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Constructor. |
||
| 69 | * |
||
| 70 | * @param Context $context The context |
||
| 71 | * @param Importer $importer The importer |
||
| 72 | */ |
||
| 73 | public function __construct(Context $context, Importer $importer) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public function run($root) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Visits a import node. |
||
| 104 | * |
||
| 105 | * @param ImportNode $node The node |
||
| 106 | * @param VisitorArguments $arguments The arguments |
||
| 107 | * |
||
| 108 | * @return ImportNode |
||
| 109 | */ |
||
| 110 | public function visitImport(ImportNode $node, VisitorArguments $arguments) |
||
| 136 | |||
| 137 | private function processImportNode(ImportNode $node, Context $context, $importParent) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Visits a rule node. |
||
| 225 | * |
||
| 226 | * @param RuleNode $node The node |
||
| 227 | * @param VisitorArguments $arguments The arguments |
||
| 228 | * |
||
| 229 | * @return RuleNode |
||
| 230 | */ |
||
| 231 | public function visitRule(RuleNode $node, VisitorArguments $arguments) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Visits a rule node (!again). |
||
| 244 | * |
||
| 245 | * @param RuleNode $node The node |
||
| 246 | * @param VisitorArguments $arguments The arguments |
||
| 247 | * |
||
| 248 | * @return RuleNode |
||
| 249 | */ |
||
| 250 | public function visitRuleOut(RuleNode $node, VisitorArguments $arguments) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Visits a directive node. |
||
| 261 | * |
||
| 262 | * @param DirectiveNode $node The node |
||
| 263 | * @param VisitorArguments $arguments The arguments |
||
| 264 | * |
||
| 265 | * @return DirectiveNode |
||
| 266 | */ |
||
| 267 | public function visitDirective(DirectiveNode $node, VisitorArguments $arguments) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Visits a directive node (!again). |
||
| 276 | * |
||
| 277 | * @param DirectiveNode $node The node |
||
| 278 | * @param VisitorArguments $arguments The arguments |
||
| 279 | * |
||
| 280 | * @return DirectiveNode |
||
| 281 | */ |
||
| 282 | public function visitDirectiveOut(DirectiveNode $node, VisitorArguments $arguments) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Visits a mixin definition node. |
||
| 291 | * |
||
| 292 | * @param MixinDefinitionNode $node The node |
||
| 293 | * @param VisitorArguments $arguments The arguments |
||
| 294 | * |
||
| 295 | * @return MixinDefinitionNode |
||
| 296 | */ |
||
| 297 | public function visitMixinDefinition(MixinDefinitionNode $node, VisitorArguments $arguments) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Visits a mixin definition node (!again). |
||
| 306 | * |
||
| 307 | * @param MixinDefinitionNode $node The node |
||
| 308 | * @param VisitorArguments $arguments The arguments |
||
| 309 | * |
||
| 310 | * @return MixinDefinitionNode |
||
| 311 | */ |
||
| 312 | public function visitMixinDefinitionOut(MixinDefinitionNode $node, VisitorArguments $arguments) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Visits a ruleset node. |
||
| 321 | * |
||
| 322 | * @param RulesetNode $node The node |
||
| 323 | * @param VisitorArguments $arguments The arguments |
||
| 324 | * |
||
| 325 | * @return RulesetNode |
||
| 326 | */ |
||
| 327 | public function visitRuleset(RulesetNode $node, VisitorArguments $arguments) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Visits a ruleset node (!again). |
||
| 336 | * |
||
| 337 | * @param RulesetNode $node The node |
||
| 338 | * @param VisitorArguments $arguments The arguments |
||
| 339 | * |
||
| 340 | * @return RulesetNode |
||
| 341 | */ |
||
| 342 | public function visitRulesetOut(RulesetNode $node, VisitorArguments $arguments) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Visits a media node. |
||
| 351 | * |
||
| 352 | * @param MediaNode $node The node |
||
| 353 | * @param VisitorArguments $arguments The arguments |
||
| 354 | * |
||
| 355 | * @return MediaNode |
||
| 356 | */ |
||
| 357 | public function visitMedia(MediaNode $node, VisitorArguments $arguments) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Visits a media node (!again). |
||
| 366 | * |
||
| 367 | * @param MediaNode $node The node |
||
| 368 | * @param VisitorArguments $arguments The arguments |
||
| 369 | * |
||
| 370 | * @return MediaNode |
||
| 371 | */ |
||
| 372 | public function visitMediaOut(MediaNode $node, VisitorArguments $arguments) |
||
| 378 | } |
||
| 379 |
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: