Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Container |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Exit code used by commands to identify that they should abort the entire script |
||
| 29 | */ |
||
| 30 | const ABORT_EXIT_CODE = 42; |
||
| 31 | |||
| 32 | protected $commands = array(); |
||
| 33 | protected $values = array(); |
||
| 34 | |||
| 35 | private $resolutionStack = array(); |
||
| 36 | private $varStack = array(); |
||
| 37 | private $scope = array(); |
||
|
|
|||
| 38 | |||
| 39 | public $output; |
||
| 40 | public $executor; |
||
| 41 | public $plugins = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Construct the container with the specified values as services/values. |
||
| 45 | * |
||
| 46 | * @param Executor $executor |
||
| 47 | * @param OutputInterface $output |
||
| 48 | */ |
||
| 49 | 10 | public function __construct(Executor $executor = null, $output = null) |
|
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Return the raw context value at the specified path. |
||
| 185 | * |
||
| 186 | * @param array|string $path |
||
| 187 | * @return mixed |
||
| 188 | */ |
||
| 189 | 3 | public function get($path) |
|
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Looks up a path in the specified context |
||
| 197 | * |
||
| 198 | * @param array $context |
||
| 199 | * @param array $path |
||
| 200 | * @param bool $require |
||
| 201 | * @return string |
||
| 202 | * |
||
| 203 | * @throws \RuntimeException |
||
| 204 | * @throws \InvalidArgumentException |
||
| 205 | */ |
||
| 206 | 12 | public function lookup($context, $path, $require = false) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Resolve the specified path. If the resulting value is a Closure, it's assumed a declaration and therefore |
||
| 219 | * executed |
||
| 220 | * |
||
| 221 | * @param array|string $id |
||
| 222 | * @param bool $required |
||
| 223 | * @return string |
||
| 224 | * |
||
| 225 | * @throws \RuntimeException |
||
| 226 | * @throws CircularReferenceException |
||
| 227 | */ |
||
| 228 | 9 | public function resolve($id, $required = false) |
|
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * Set the value at the specified path |
||
| 265 | * |
||
| 266 | * @param array|string $path |
||
| 267 | * @param mixed $value |
||
| 268 | * @return void |
||
| 269 | * |
||
| 270 | * @throws \UnexpectedValueException |
||
| 271 | */ |
||
| 272 | 9 | public function set($path, $value) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * This is useful for commands that need the shell regardless of the 'explain' value setting. |
||
| 279 | * |
||
| 280 | * @param string $cmd |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | 1 | public function helperExec($cmd) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Wrapper for converting string paths to arrays. |
||
| 296 | * |
||
| 297 | * @param mixed $path |
||
| 298 | * @return array |
||
| 299 | * |
||
| 300 | * @throws \InvalidArgumentException |
||
| 301 | */ |
||
| 302 | 9 | private function path($path) |
|
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Set a function at the specified path. |
||
| 313 | * |
||
| 314 | * @param array|string $id |
||
| 315 | * @param callable $callable |
||
| 316 | * @param bool $needsContainer |
||
| 317 | * @return void |
||
| 318 | * |
||
| 319 | * @throws \InvalidArgumentException |
||
| 320 | */ |
||
| 321 | 7 | public function fn($id, $callable = null, $needsContainer = false) |
|
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * Creates a method-type function, i.e. the first parameter will always be the container. |
||
| 335 | * |
||
| 336 | * @param array $id |
||
| 337 | * @param callable $callable |
||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | public function method($id, $callable) |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * Does a declaration, i.e., the first time the declaration is called, it's resulting value overwrites the |
||
| 348 | * declaration. |
||
| 349 | * |
||
| 350 | * @param array|string $id |
||
| 351 | * @param callable $callable |
||
| 352 | * @return void |
||
| 353 | * |
||
| 354 | * @throws \InvalidArgumentException |
||
| 355 | */ |
||
| 356 | 9 | public function decl($id, $callable) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Output a notice |
||
| 373 | * |
||
| 374 | * @param string $message |
||
| 375 | * @return void |
||
| 376 | */ |
||
| 377 | public function notice($message) |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Does an on-the-fly evaluation of the specified expression. |
||
| 385 | * The compilation result will be stored in $code. |
||
| 386 | * |
||
| 387 | * @param string $expression |
||
| 388 | * @param string &$code |
||
| 389 | * |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | public function evaluate($expression, &$code = null) |
||
| 403 | |||
| 404 | |||
| 405 | /** |
||
| 406 | * Checks for existence of the specified path. |
||
| 407 | * |
||
| 408 | * @param string $id |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 1 | public function has($id) |
|
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * Checks if a value is empty. |
||
| 424 | * |
||
| 425 | * @param mixed $path |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public function isEmpty($path) |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Separate helper for calling a service as a function. |
||
| 441 | * |
||
| 442 | * @return mixed |
||
| 443 | * |
||
| 444 | * @throws \InvalidArgumentException |
||
| 445 | */ |
||
| 446 | public function call() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Helper to set prefix if the output if PrefixFormatter |
||
| 466 | * |
||
| 467 | * @param string $prefix |
||
| 468 | * @return void |
||
| 469 | */ |
||
| 470 | 2 | private function setOutputPrefix($prefix) |
|
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * Executes a script snippet using the 'executor' service. |
||
| 482 | * |
||
| 483 | * @param string $cmd |
||
| 484 | * @return void |
||
| 485 | */ |
||
| 486 | 2 | public function exec($cmd) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Execute a command. This is a wrapper for 'exec', so that a task prefixed with '@' can be passed as well. |
||
| 511 | * |
||
| 512 | * @param string $cmd |
||
| 513 | * @return string|null |
||
| 514 | */ |
||
| 515 | 3 | public function cmd($cmd) |
|
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Returns the value representation of the requested variable |
||
| 529 | * |
||
| 530 | * @param string $value |
||
| 531 | * @return string |
||
| 532 | * |
||
| 533 | * @throws \UnexpectedValueException |
||
| 534 | */ |
||
| 535 | 7 | public function value($value) |
|
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * Convert the value to a string. |
||
| 546 | * |
||
| 547 | * @param mixed $value |
||
| 548 | * @return string |
||
| 549 | * @throws \UnexpectedValueException |
||
| 550 | */ |
||
| 551 | 2 | public function str($value) |
|
| 564 | |||
| 565 | |||
| 566 | /** |
||
| 567 | * Register a plugin |
||
| 568 | * |
||
| 569 | * @param \Zicht\Tool\PluginInterface $plugin |
||
| 570 | * @return void |
||
| 571 | */ |
||
| 572 | public function addPlugin(PluginInterface $plugin) |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * Register a command |
||
| 581 | * |
||
| 582 | * @param \Symfony\Component\Console\Command\Command $command |
||
| 583 | * @return void |
||
| 584 | */ |
||
| 585 | public function addCommand(Command $command) |
||
| 589 | |||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns the registered commands |
||
| 593 | * |
||
| 594 | * @return Task[] |
||
| 595 | */ |
||
| 596 | public function getCommands() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Returns the values. |
||
| 603 | * |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | 1 | public function getValues() |
|
| 610 | |||
| 611 | |||
| 612 | /** |
||
| 613 | * Can not be cloned |
||
| 614 | * |
||
| 615 | * @return void |
||
| 616 | */ |
||
| 617 | private function __clone() |
||
| 620 | |||
| 621 | |||
| 622 | /** |
||
| 623 | * Check whether we're in debug mode or not. |
||
| 624 | * |
||
| 625 | * @return bool |
||
| 626 | */ |
||
| 627 | public function isDebug() |
||
| 631 | |||
| 632 | |||
| 633 | /** |
||
| 634 | * Push a var on a local stack by it's name. |
||
| 635 | * |
||
| 636 | * @param string $varName |
||
| 637 | * @param string $tail |
||
| 638 | * @return void |
||
| 639 | */ |
||
| 640 | public function push($varName, $tail) |
||
| 651 | |||
| 652 | |||
| 653 | /** |
||
| 654 | * Pop a var from a local var stack. |
||
| 655 | * |
||
| 656 | * @param string $varName |
||
| 657 | * @return void |
||
| 658 | */ |
||
| 659 | public function pop($varName) |
||
| 663 | } |
||
| 664 |
This check marks private properties in classes that are never used. Those properties can be removed.