Complex classes like Legacy 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 Legacy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Legacy extends SetupFactory |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Data source name. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected static $dsn; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Root dir for IO operations. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected static $ioRootDir; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Database type (sqlite, mysql, ...). |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected static $db; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Service container. |
||
| 54 | * |
||
| 55 | * @var \eZ\Publish\Core\Base\ServiceContainer |
||
| 56 | */ |
||
| 57 | protected static $serviceContainer; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * If the DB schema has already been initialized. |
||
| 61 | * |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected static $schemaInitialized = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Initial database data. |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected static $initialData; |
||
| 72 | |||
| 73 | protected $repositoryReference = 'ezpublish.api.repository'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates a new setup factory. |
||
| 77 | */ |
||
| 78 | public function __construct() |
||
| 79 | { |
||
| 80 | self::$dsn = getenv('DATABASE'); |
||
| 81 | if (!self::$dsn) { |
||
| 82 | // use sqlite in-memory by default (does not need special handling for paratest as it's per process) |
||
| 83 | self::$dsn = 'sqlite://:memory:'; |
||
| 84 | } elseif (getenv('TEST_TOKEN') !== false) { |
||
| 85 | // Using paratest, assuming dsn ends with db name here... |
||
| 86 | self::$dsn .= '_' . getenv('TEST_TOKEN'); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($repositoryReference = getenv('REPOSITORY_SERVICE_ID')) { |
||
| 90 | $this->repositoryReference = $repositoryReference; |
||
| 91 | } |
||
| 92 | |||
| 93 | self::$db = preg_replace('(^([a-z]+).*)', '\\1', self::$dsn); |
||
| 94 | |||
| 95 | if (!isset(self::$ioRootDir)) { |
||
| 96 | self::$ioRootDir = $this->createTemporaryDirectory(); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Creates a temporary directory and returns it. |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | * @throw \RuntimeException If the root directory can't be created |
||
| 105 | */ |
||
| 106 | private function createTemporaryDirectory() |
||
| 107 | { |
||
| 108 | $tmpFile = tempnam( |
||
| 109 | sys_get_temp_dir(), |
||
| 110 | 'ez_legacy_tests_' . time() |
||
| 111 | ); |
||
| 112 | unlink($tmpFile); |
||
| 113 | |||
| 114 | $fs = new Filesystem(); |
||
| 115 | $fs->mkdir($tmpFile); |
||
| 116 | |||
| 117 | $varDir = $tmpFile . '/var'; |
||
| 118 | if ($fs->exists($varDir)) { |
||
| 119 | $fs->remove($varDir); |
||
| 120 | } |
||
| 121 | $fs->mkdir($varDir); |
||
| 122 | |||
| 123 | return $tmpFile; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns a configured repository for testing. |
||
| 128 | * |
||
| 129 | * @param bool $initializeFromScratch if the back end should be initialized |
||
| 130 | * from scratch or re-used |
||
| 131 | * |
||
| 132 | * @return \eZ\Publish\API\Repository\Repository |
||
| 133 | */ |
||
| 134 | public function getRepository($initializeFromScratch = true) |
||
| 135 | { |
||
| 136 | if ($initializeFromScratch || !self::$schemaInitialized) { |
||
| 137 | $this->initializeSchema(); |
||
| 138 | $this->insertData(); |
||
| 139 | } |
||
| 140 | |||
| 141 | $this->clearInternalCaches(); |
||
| 142 | /** @var \eZ\Publish\API\Repository\Repository $repository */ |
||
| 143 | $repository = $this->getServiceContainer()->get($this->repositoryReference); |
||
| 144 | |||
| 145 | // Set admin user as current user by default |
||
| 146 | $repository->getPermissionResolver()->setCurrentUserReference( |
||
| 147 | new UserReference(14) |
||
| 148 | ); |
||
| 149 | |||
| 150 | return $repository; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns a config value for $configKey. |
||
| 155 | * |
||
| 156 | * @param string $configKey |
||
| 157 | * |
||
| 158 | * @throws Exception if $configKey could not be found. |
||
| 159 | * |
||
| 160 | * @return mixed |
||
| 161 | */ |
||
| 162 | public function getConfigValue($configKey) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns a repository specific ID manager. |
||
| 169 | * |
||
| 170 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
| 171 | */ |
||
| 172 | public function getIdManager() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Insert the database data. |
||
| 179 | */ |
||
| 180 | public function insertData() |
||
| 240 | |||
| 241 | protected function getInitialVarDir() |
||
| 245 | |||
| 246 | protected function cleanupVarDir($sourceDir) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * CLears internal in memory caches after inserting data circumventing the |
||
| 259 | * API. |
||
| 260 | */ |
||
| 261 | protected function clearInternalCaches() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns statements to be executed after data insert. |
||
| 284 | * |
||
| 285 | * @return string[] |
||
| 286 | */ |
||
| 287 | protected function getPostInsertStatements() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the initial database data. |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | protected function getInitialData() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Initializes the database schema. |
||
| 314 | * |
||
| 315 | * @throws \Doctrine\DBAL\ConnectionException |
||
| 316 | */ |
||
| 317 | protected function initializeSchema(): void |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Applies the given SQL $statements to the database in use. |
||
| 332 | * |
||
| 333 | * @param array $statements |
||
| 334 | */ |
||
| 335 | protected function applyStatements(array $statements) |
||
| 341 | |||
| 342 | // ************* Setup copied and refactored from common.php ************ |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns the database handler from the service container. |
||
| 346 | * |
||
| 347 | * @return \eZ\Publish\Core\Persistence\Doctrine\ConnectionHandler |
||
| 348 | */ |
||
| 349 | protected function getDatabaseHandler() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Returns the raw database connection from the service container. |
||
| 356 | * |
||
| 357 | * @return \Doctrine\DBAL\Connection |
||
| 358 | */ |
||
| 359 | private function getDatabaseConnection(): Connection |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns the service container used for initialization of the repository. |
||
| 366 | * |
||
| 367 | * @return \eZ\Publish\Core\Base\ServiceContainer |
||
| 368 | */ |
||
| 369 | public function getServiceContainer() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * This is intended to be used from external repository in order to |
||
| 413 | * enable container customization. |
||
| 414 | * |
||
| 415 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $containerBuilder |
||
| 416 | */ |
||
| 417 | protected function externalBuildContainer(ContainerBuilder $containerBuilder) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Get the Database name. |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function getDB() |
||
| 431 | } |
||
| 432 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.