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 |
||
| 28 | class Legacy extends SetupFactory |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Data source name. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected static $dsn; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Root dir for IO operations. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected static $ioRootDir; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Database type (sqlite, mysql, ...). |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected static $db; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Service container. |
||
| 53 | * |
||
| 54 | * @var \eZ\Publish\Core\Base\ServiceContainer |
||
| 55 | */ |
||
| 56 | protected static $serviceContainer; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * If the DB schema has already been initialized. |
||
| 60 | * |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected static $schemaInitialized = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Initial database data. |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected static $initialData; |
||
| 71 | |||
| 72 | protected $repositoryReference = 'ezpublish.api.repository'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Creates a new setup factory. |
||
| 76 | */ |
||
| 77 | public function __construct() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Creates a temporary directory and returns it. |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | * @throw \RuntimeException If the root directory can't be created |
||
| 100 | */ |
||
| 101 | private function createTemporaryDirectory() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns a configured repository for testing. |
||
| 123 | * |
||
| 124 | * @param bool $initializeFromScratch if the back end should be initialized |
||
| 125 | * from scratch or re-used |
||
| 126 | * |
||
| 127 | * @return \eZ\Publish\API\Repository\Repository |
||
| 128 | */ |
||
| 129 | public function getRepository($initializeFromScratch = true) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns a config value for $configKey. |
||
| 147 | * |
||
| 148 | * @param string $configKey |
||
| 149 | * |
||
| 150 | * @throws Exception if $configKey could not be found. |
||
| 151 | * |
||
| 152 | * @return mixed |
||
| 153 | */ |
||
| 154 | public function getConfigValue($configKey) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns a repository specific ID manager. |
||
| 161 | * |
||
| 162 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
| 163 | */ |
||
| 164 | public function getIdManager() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Insert the database data. |
||
| 171 | */ |
||
| 172 | public function insertData() |
||
| 238 | |||
| 239 | protected function getInitialVarDir() |
||
| 243 | |||
| 244 | protected function cleanupVarDir($sourceDir) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * CLears internal in memory caches after inserting data circumventing the |
||
| 257 | * API. |
||
| 258 | */ |
||
| 259 | protected function clearInternalCaches() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns statements to be executed after data insert. |
||
| 282 | * |
||
| 283 | * @return string[] |
||
| 284 | */ |
||
| 285 | protected function getPostInsertStatements() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns the initial database data. |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | protected function getInitialData() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Initializes the database schema. |
||
| 313 | */ |
||
| 314 | protected function initializeSchema() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Applies the given SQL $statements to the database in use. |
||
| 327 | * |
||
| 328 | * @param array $statements |
||
| 329 | */ |
||
| 330 | protected function applyStatements(array $statements) |
||
| 336 | |||
| 337 | // ************* Setup copied and refactored from common.php ************ |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns the database schema as an array of SQL statements. |
||
| 341 | * |
||
| 342 | * @return string[] |
||
| 343 | */ |
||
| 344 | protected function getSchemaStatements() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns the database handler from the service container. |
||
| 353 | * |
||
| 354 | * @return \eZ\Publish\Core\Persistence\Doctrine\ConnectionHandler |
||
| 355 | */ |
||
| 356 | protected function getDatabaseHandler() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Returns the service container used for initialization of the repository. |
||
| 363 | * |
||
| 364 | * @return \eZ\Publish\Core\Base\ServiceContainer |
||
| 365 | */ |
||
| 366 | public function getServiceContainer() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * This is intended to be used from external repository in order to |
||
| 408 | * enable container customization. |
||
| 409 | * |
||
| 410 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $containerBuilder |
||
| 411 | */ |
||
| 412 | protected function externalBuildContainer(ContainerBuilder $containerBuilder) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Get the Database name. |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | public function getDB() |
||
| 426 | } |
||
| 427 |
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.