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 Application 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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 74 | class Application extends \Thread implements ApplicationInterface, DirectoryAwareInterface, FilesystemAwareInterface, \AppserverIo\Psr\Context\ContextInterface |
||
| 75 | { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The time we wait after each loop. |
||
| 79 | * |
||
| 80 | * @var integer |
||
| 81 | */ |
||
| 82 | const TIME_TO_LIVE = 1; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Trait that provides threaded context functionality. |
||
| 86 | * |
||
| 87 | * @var \AppserverIo\Appserver\Core\Traits\ThreadedContextTrait |
||
| 88 | */ |
||
| 89 | 23 | use ThreadedContextTrait; |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Initialize the internal members. |
||
| 93 | 23 | */ |
|
| 94 | public function __construct() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Inject the environment name |
||
| 106 | * |
||
| 107 | * @param string $environmentName The environment name to inject |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public function injectEnvironmentName($environmentName) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Injects the naming directory. |
||
| 118 | 23 | * |
|
| 119 | * @param \AppserverIo\Psr\Naming\NamingDirectoryInterface $namingDirectory The naming directory instance |
||
| 120 | 23 | * |
|
| 121 | 23 | * @return void |
|
| 122 | */ |
||
| 123 | public function injectNamingDirectory($namingDirectory) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Injects the storage for the managers. |
||
| 130 | 23 | * |
|
| 131 | * @param \AppserverIo\Storage\GenericStackable $managers The storage for the managers |
||
| 132 | 23 | * |
|
| 133 | 23 | * @return void |
|
| 134 | */ |
||
| 135 | public function injectManagers(GenericStackable $managers) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Injects the storage for the class loaders. |
||
| 142 | 23 | * |
|
| 143 | * @param \AppserverIo\Storage\GenericStackable $classLoaders The storage for the class loaders |
||
| 144 | 23 | * |
|
| 145 | 23 | * @return void |
|
| 146 | */ |
||
| 147 | public function injectClassLoaders(GenericStackable $classLoaders) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Injects the storage for the provisioners. |
||
| 154 | 1 | * |
|
| 155 | * @param \AppserverIo\Storage\GenericStackable $provisioners The storage for the provisioners |
||
| 156 | * |
||
| 157 | 1 | * @return void |
|
| 158 | */ |
||
| 159 | public function injectProvisioners(GenericStackable $provisioners) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Injects the storage for the loggers. |
||
| 166 | * |
||
| 167 | * @param \AppserverIo\Storage\GenericStackable $loggers The storage for the loggers |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public function injectLoggers(GenericStackable $loggers) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * The initial context instance. |
||
| 178 | 4 | * |
|
| 179 | * @param \AppserverIo\Psr\ApplicationServer\ContextInterface $initialContext The initial context instance |
||
| 180 | 4 | * |
|
| 181 | 4 | * @return void |
|
| 182 | */ |
||
| 183 | public function injectInitialContext(ContextInterface $initialContext) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Injects the application name. |
||
| 190 | 23 | * |
|
| 191 | * @param string $name The application name |
||
| 192 | 23 | * |
|
| 193 | 23 | * @return void |
|
| 194 | */ |
||
| 195 | public function injectName($name) |
||
| 199 | |||
| 200 | 11 | /** |
|
| 201 | * Returns the application name (that has to be the class namespace, e.g. example) |
||
| 202 | 11 | * |
|
| 203 | * @return string The application name |
||
| 204 | */ |
||
| 205 | public function getName() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns the applications environment name |
||
| 212 | * |
||
| 213 | * @return string The applications environment name |
||
| 214 | */ |
||
| 215 | public function getEnvironmentName() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Injects the name of the container the application is bound to. |
||
| 222 | 23 | * |
|
| 223 | * @param string $containerName The container's name |
||
| 224 | 23 | * |
|
| 225 | 23 | * @return void |
|
| 226 | */ |
||
| 227 | public function injectContainerName($containerName) |
||
| 231 | |||
| 232 | 10 | /** |
|
| 233 | * Returns the name of the container the application is bound to. |
||
| 234 | 10 | * |
|
| 235 | * @return string The container's name |
||
| 236 | */ |
||
| 237 | public function getContainerName() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Injects the runlevel of the container the application is bound to. |
||
| 244 | * |
||
| 245 | * @param string $containerRunlevel The container's runlevel |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | public function injectContainerRunlevel($containerRunlevel) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns the runlevel of the container the application is bound to. |
||
| 256 | * |
||
| 257 | * @return string The container's runlevel |
||
| 258 | */ |
||
| 259 | public function getContainerRunlevel() |
||
| 263 | |||
| 264 | 15 | /** |
|
| 265 | * Returns the applications naming directory. |
||
| 266 | 15 | * |
|
| 267 | * @return \AppserverIo\Psr\Naming\NamingDirectoryInterface The applications naming directory interface |
||
| 268 | */ |
||
| 269 | public function getNamingDirectory() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns the absolute path to the servers document root directory |
||
| 276 | 2 | * |
|
| 277 | * @param string $directoryToAppend The directory to append to the base directory |
||
| 278 | 2 | * |
|
| 279 | 2 | * @return string The base directory with appended dir if given |
|
| 280 | 1 | */ |
|
| 281 | 1 | public function getBaseDirectory($directoryToAppend = null) |
|
| 289 | |||
| 290 | 1 | /** |
|
| 291 | * Returns the absolute path to the applications base directory. |
||
| 292 | 1 | * |
|
| 293 | * @return string The app base directory |
||
| 294 | */ |
||
| 295 | public function getAppBase() |
||
| 299 | |||
| 300 | 1 | /** |
|
| 301 | * Returns the absolute path to the web application base directory. |
||
| 302 | 1 | * |
|
| 303 | * @return string The path to the webapps folder |
||
| 304 | */ |
||
| 305 | public function getWebappPath() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the absolute path to the applications temporary directory. |
||
| 312 | * |
||
| 313 | * @return string The app temporary directory |
||
| 314 | */ |
||
| 315 | public function getTmpDir() |
||
| 319 | |||
| 320 | 1 | /** |
|
| 321 | * Returns the absolute path to the applications data directory. |
||
| 322 | 1 | * |
|
| 323 | * @return string The app data directory |
||
| 324 | */ |
||
| 325 | public function getDataDir() |
||
| 329 | |||
| 330 | 1 | /** |
|
| 331 | * Returns the absolute path to the applications session directory. |
||
| 332 | 1 | * |
|
| 333 | * @return string The app session directory |
||
| 334 | */ |
||
| 335 | public function getSessionDir() |
||
| 339 | |||
| 340 | 1 | /** |
|
| 341 | * Returns the absolute path to the applications cache directory. |
||
| 342 | 1 | * |
|
| 343 | * @return string The app cache directory |
||
| 344 | */ |
||
| 345 | public function getCacheDir() |
||
| 349 | |||
| 350 | 1 | /** |
|
| 351 | * Returns the username the application should be executed with. |
||
| 352 | 1 | * |
|
| 353 | * @return string The username |
||
| 354 | */ |
||
| 355 | public function getUser() |
||
| 359 | |||
| 360 | 1 | /** |
|
| 361 | * Returns the groupname the application should be executed with. |
||
| 362 | 1 | * |
|
| 363 | * @return string The groupname |
||
| 364 | */ |
||
| 365 | public function getGroup() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Returns the umask the application should create files/directories with. |
||
| 372 | * |
||
| 373 | * @return string The umask |
||
| 374 | */ |
||
| 375 | public function getUmask() |
||
| 379 | |||
| 380 | 1 | /** |
|
| 381 | * Return's the container instance the application is bound to. |
||
| 382 | 1 | * |
|
| 383 | * @return \AppserverIo\Psr\ApplicationServer\ContainerInterface The container instance |
||
| 384 | */ |
||
| 385 | public function getContainer() |
||
| 389 | |||
| 390 | /** |
||
| 391 | 10 | * Return's the system properties enriched with the application specific properties like webapp.dir etc. |
|
| 392 | * |
||
| 393 | 10 | * @return \AppserverIo\Properties\PropertiesInterface The sytem properties |
|
| 394 | */ |
||
| 395 | public function getSystemProperties() |
||
| 414 | 4 | ||
| 415 | /** |
||
| 416 | 4 | * Return's the application's UUID. |
|
| 417 | * |
||
| 418 | * @return string The application's UUID |
||
| 419 | */ |
||
| 420 | public function getSerial() |
||
| 424 | |||
| 425 | /** |
||
| 426 | 1 | * Return's the unique application name that is the container + application name |
|
| 427 | * separated with a slash, e. g. combined-appserver/example. |
||
| 428 | 1 | * |
|
| 429 | 1 | * @return string |
|
| 430 | */ |
||
| 431 | public function getUniqueName() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * (non-PHPdoc) |
||
| 438 | 3 | * |
|
| 439 | * @param string $className The API service class name to return the instance for |
||
| 440 | 3 | * |
|
| 441 | * @return object The service instance |
||
| 442 | * @see \AppserverIo\Psr\ApplicationServer\ContextInterface::newService() |
||
| 443 | */ |
||
| 444 | public function newService($className) |
||
| 448 | 3 | ||
| 449 | /** |
||
| 450 | 3 | * Returns the initial context instance. |
|
| 451 | * |
||
| 452 | * @return \AppserverIo\Psr\ApplicationServer\ContextInterface The initial Context |
||
| 453 | */ |
||
| 454 | public function getInitialContext() |
||
| 458 | |||
| 459 | /** |
||
| 460 | 2 | * Return the requested class loader instance |
|
| 461 | * |
||
| 462 | 2 | * @param string $identifier The unique identifier of the requested class loader |
|
| 463 | 1 | * |
|
| 464 | * @return \AppserverIo\Appserver\Core\Interfaces\ClassLoaderInterface The class loader instance |
||
| 465 | 1 | */ |
|
| 466 | public function getClassLoader($identifier) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Return the class loaders. |
||
| 475 | * |
||
| 476 | * @return \AppserverIo\Storage\GenericStackable The class loader instances |
||
| 477 | */ |
||
| 478 | public function getClassLoaders() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Returns the manager instances. |
||
| 485 | * |
||
| 486 | * @return \AppserverIo\Storage\GenericStackable The manager instances |
||
| 487 | */ |
||
| 488 | public function getManagers() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Return the requested manager instance. |
||
| 495 | * |
||
| 496 | * @param string $identifier The unique identifier of the requested manager |
||
| 497 | * |
||
| 498 | * @return \AppserverIo\Psr\Application\ManagerInterface The manager instance |
||
| 499 | */ |
||
| 500 | public function getManager($identifier) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns the provisioner instances. |
||
| 509 | * |
||
| 510 | * @return \AppserverIo\Storage\GenericStackable The provisioner instances |
||
| 511 | */ |
||
| 512 | public function getProvisioners() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Return the requested provisioner instance. |
||
| 519 | * |
||
| 520 | * @param string $identifier The unique identifier of the requested provisioner |
||
| 521 | * |
||
| 522 | * @return \AppserverIo\Psr\Application\ProvisionerInterface The provisioner instance |
||
| 523 | */ |
||
| 524 | 3 | public function getProvisioner($identifier) |
|
| 530 | |||
| 531 | 3 | /** |
|
| 532 | 3 | * Returns the logger instances. |
|
| 533 | * |
||
| 534 | * @return \AppserverIo\Storage\GenericStackable The logger instances |
||
| 535 | */ |
||
| 536 | public function getLoggers() |
||
| 540 | |||
| 541 | /** |
||
| 542 | 3 | * Return the requested logger instance, by default the application's system logger. |
|
| 543 | * |
||
| 544 | * @param string $name The name of the requested logger |
||
| 545 | * |
||
| 546 | 3 | * @return \Psr\Log\LoggerInterface|null The logger instance |
|
| 547 | */ |
||
| 548 | public function getLogger($name = LoggerUtils::SYSTEM_LOGGER) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Injects an additional class loader. |
||
| 558 | * |
||
| 559 | * @param \AppserverIo\Appserver\Core\Interfaces\ClassLoaderInterface $classLoader A class loader to put on the class loader stack |
||
| 560 | * @param \AppserverIo\Appserver\Core\Api\Node\ClassLoaderNodeInterface $configuration The class loader's configuration |
||
| 561 | * |
||
| 562 | * @return void |
||
| 563 | */ |
||
| 564 | public function addClassLoader(ClassLoaderInterface $classLoader, ClassLoaderNodeInterface $configuration) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Injects manager instance and the configuration. |
||
| 576 | * |
||
| 577 | * @param \AppserverIo\Psr\Application\ManagerInterface $manager A manager instance |
||
| 578 | * @param \AppserverIo\Appserver\Core\Api\Node\ManagerNodeInterface $configuration The managers configuration |
||
| 579 | * |
||
| 580 | * @return void |
||
| 581 | */ |
||
| 582 | public function addManager(ManagerInterface $manager, ManagerNodeInterface $configuration) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Injects the provisioner instance and the configuration. |
||
| 594 | * |
||
| 595 | * @param \AppserverIo\Psr\Application\ProvisionerInterface $provisioner A provisioner instance |
||
| 596 | * @param \AppserverIo\Appserver\Core\Api\Node\ProvisionerNodeInterface $configuration The provisioner configuration |
||
| 597 | * |
||
| 598 | * @return void |
||
| 599 | */ |
||
| 600 | public function addProvisioner(ProvisionerInterface $provisioner, ProvisionerNodeInterface $configuration) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Injects the logger instance and the configuration. |
||
| 612 | * |
||
| 613 | * @param \Psr\Log\LoggerInterface $logger A provisioner instance |
||
| 614 | * @param \AppserverIo\Appserver\Core\Api\Node\LoggerNodeInterface $configuration The provisioner configuration |
||
| 615 | * |
||
| 616 | * @return void |
||
| 617 | */ |
||
| 618 | public function addLogger(LoggerInterface $logger, LoggerNodeInterface $configuration) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Prepares the application with the specific data found in the |
||
| 633 | * passed context node. |
||
| 634 | * |
||
| 635 | * @param \AppserverIo\Psr\ApplicationServer\ContainerInterface $container The container instance bind the application to |
||
| 636 | * @param \AppserverIo\Appserver\Core\Api\Node\ContextNode $context The application configuration |
||
| 637 | * |
||
| 638 | * @return void |
||
| 639 | */ |
||
| 640 | public function prepare(ContainerInterface $container, ContextNode $context) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Cleanup the naming directory from the application entries. |
||
| 686 | * |
||
| 687 | * @return void |
||
| 688 | */ |
||
| 689 | public function unload() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Has been automatically invoked by the container after the application |
||
| 713 | * instance has been created. |
||
| 714 | * |
||
| 715 | 1 | * @return void |
|
| 716 | * @see \Thread::run() |
||
| 717 | * @codeCoverageIgnore |
||
| 718 | */ |
||
| 719 | public function connect() |
||
| 723 | 1 | ||
| 724 | 1 | /** |
|
| 725 | * TRUE if the application has been connected, else FALSE. |
||
| 726 | * |
||
| 727 | 1 | * @return boolean Returns TRUE if the application has been connected, else FALSE |
|
| 728 | */ |
||
| 729 | public function isConnected() |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Registers all class loaders injected to the applications in the opposite |
||
| 738 | * order as they have been injected. |
||
| 739 | * |
||
| 740 | * @return void |
||
| 741 | 1 | */ |
|
| 742 | public function registerClassLoaders() |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Register's additional annotation registries defined in the configuration. |
||
| 765 | * |
||
| 766 | * @return void |
||
| 767 | */ |
||
| 768 | public function registerAnnotationRegistries() |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Provisions the initialized application. |
||
| 785 | * |
||
| 786 | * @return void |
||
| 787 | */ |
||
| 788 | public function provision() |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Registers all managers in the application. |
||
| 807 | * |
||
| 808 | * @return void |
||
| 809 | */ |
||
| 810 | View Code Duplication | public function initializeManagers() |
|
| 826 | |||
| 827 | /** |
||
| 828 | * Invokes the postStartup() method lifecycle callback of the registered managers. |
||
| 829 | * |
||
| 830 | * @return void |
||
| 831 | */ |
||
| 832 | View Code Duplication | public function postStartupManagers() |
|
| 848 | |||
| 849 | /** |
||
| 850 | * Stops the application instance. |
||
| 851 | * |
||
| 852 | * @return void |
||
| 853 | */ |
||
| 854 | public function stop() |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Queries the naming directory for the requested name and returns the value |
||
| 879 | * or invokes the bound callback. |
||
| 880 | * |
||
| 881 | * @param string $name The name of the requested value |
||
| 882 | * @param array $args The arguments to pass to the callback |
||
| 883 | * |
||
| 884 | * @return mixed The requested value |
||
| 885 | * @see \AppserverIo\Appserver\Naming\NamingDirectory::search() |
||
| 886 | */ |
||
| 887 | public function search($name, array $args = array()) |
||
| 891 | |||
| 892 | /** |
||
| 893 | * This is the threads main() method that initializes the application with the autoloader and |
||
| 894 | * instantiates all the necessary manager instances. |
||
| 895 | * |
||
| 896 | * @return void |
||
| 897 | * @codeCoverageIgnore |
||
| 898 | */ |
||
| 899 | public function run() |
||
| 1001 | |||
| 1002 | /** |
||
| 1003 | * Shutdown function to log unexpected errors. |
||
| 1004 | * |
||
| 1005 | * @return void |
||
| 1006 | * @see http://php.net/register_shutdown_function |
||
| 1007 | */ |
||
| 1008 | public function shutdown() |
||
| 1024 | } |
||
| 1025 |