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 |
||
33 | class Legacy extends SetupFactory |
||
34 | { |
||
35 | /** |
||
36 | * Data source name. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected static $dsn; |
||
41 | |||
42 | /** |
||
43 | * Root dir for IO operations. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected static $ioRootDir; |
||
48 | |||
49 | /** |
||
50 | * Database type (sqlite, mysql, ...). |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected static $db; |
||
55 | |||
56 | /** |
||
57 | * Service container. |
||
58 | * |
||
59 | * @var \eZ\Publish\Core\Base\ServiceContainer |
||
60 | */ |
||
61 | protected static $serviceContainer; |
||
62 | |||
63 | /** |
||
64 | * If the DB schema has already been initialized. |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | protected static $schemaInitialized = false; |
||
69 | |||
70 | /** |
||
71 | * Initial database data. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected static $initialData; |
||
76 | |||
77 | protected $repositoryReference = 'ezpublish.api.repository'; |
||
78 | |||
79 | /** |
||
80 | * Creates a new setup factory. |
||
81 | */ |
||
82 | public function __construct() |
||
83 | { |
||
84 | self::$dsn = getenv('DATABASE'); |
||
85 | if (!self::$dsn) { |
||
86 | // use sqlite in-memory by default (does not need special handling for paratest as it's per process) |
||
87 | self::$dsn = 'sqlite://:memory:'; |
||
88 | } elseif (getenv('TEST_TOKEN') !== false) { |
||
89 | // Using paratest, assuming dsn ends with db name here... |
||
90 | self::$dsn .= '_' . getenv('TEST_TOKEN'); |
||
91 | } |
||
92 | |||
93 | if ($repositoryReference = getenv('REPOSITORY_SERVICE_ID')) { |
||
94 | $this->repositoryReference = $repositoryReference; |
||
95 | } |
||
96 | |||
97 | self::$db = preg_replace('(^([a-z]+).*)', '\\1', self::$dsn); |
||
98 | |||
99 | if (!isset(self::$ioRootDir)) { |
||
100 | self::$ioRootDir = $this->createTemporaryDirectory(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Creates a temporary directory and returns it. |
||
106 | * |
||
107 | * @return string |
||
108 | * @throw \RuntimeException If the root directory can't be created |
||
109 | */ |
||
110 | private function createTemporaryDirectory() |
||
111 | { |
||
112 | $tmpFile = tempnam( |
||
113 | sys_get_temp_dir(), |
||
114 | 'ez_legacy_tests_' . time() |
||
115 | ); |
||
116 | unlink($tmpFile); |
||
117 | |||
118 | $fs = new Filesystem(); |
||
119 | $fs->mkdir($tmpFile); |
||
120 | |||
121 | $varDir = $tmpFile . '/var'; |
||
122 | if ($fs->exists($varDir)) { |
||
123 | $fs->remove($varDir); |
||
124 | } |
||
125 | $fs->mkdir($varDir); |
||
126 | |||
127 | return $tmpFile; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Returns a configured repository for testing. |
||
132 | * |
||
133 | * @param bool $initializeFromScratch if the back end should be initialized |
||
134 | * from scratch or re-used |
||
135 | * |
||
136 | * @return \eZ\Publish\API\Repository\Repository |
||
137 | */ |
||
138 | public function getRepository($initializeFromScratch = true) |
||
139 | { |
||
140 | if ($initializeFromScratch || !self::$schemaInitialized) { |
||
141 | $this->initializeSchema(); |
||
142 | $this->insertData(); |
||
143 | } |
||
144 | |||
145 | $this->clearInternalCaches(); |
||
146 | $repository = $this->getServiceContainer()->get($this->repositoryReference); |
||
147 | |||
148 | // Set admin user as current user by default |
||
149 | $repository->setCurrentUser(new UserReference(14)); |
||
150 | |||
151 | return $repository; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns a config value for $configKey. |
||
156 | * |
||
157 | * @param string $configKey |
||
158 | * |
||
159 | * @throws Exception if $configKey could not be found. |
||
160 | * |
||
161 | * @return mixed |
||
162 | */ |
||
163 | public function getConfigValue($configKey) |
||
164 | { |
||
165 | return $this->getServiceContainer()->getParameter($configKey); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Returns a repository specific ID manager. |
||
170 | * |
||
171 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
172 | */ |
||
173 | public function getIdManager() |
||
174 | { |
||
175 | return new IdManager\Php(); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Insert the database data. |
||
180 | */ |
||
181 | public function insertData() |
||
182 | { |
||
183 | $data = $this->getInitialData(); |
||
184 | $handler = $this->getDatabaseHandler(); |
||
185 | $connection = $handler->getConnection(); |
||
186 | $dbPlatform = $connection->getDatabasePlatform(); |
||
187 | $this->cleanupVarDir($this->getInitialVarDir()); |
||
188 | |||
189 | foreach (array_reverse(array_keys($data)) as $table) { |
||
190 | try { |
||
191 | // Cleanup before inserting (using TRUNCATE for speed, however not possible to rollback) |
||
192 | $connection->executeUpdate($dbPlatform->getTruncateTableSql($handler->quoteIdentifier($table))); |
||
193 | } catch (DBALException | PDOException $e) { |
||
194 | // Fallback to DELETE if TRUNCATE failed (because of FKs for instance) |
||
195 | $connection->createQueryBuilder()->delete($table)->execute(); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | foreach ($data as $table => $rows) { |
||
200 | // Check that at least one row exists |
||
201 | if (!isset($rows[0])) { |
||
202 | continue; |
||
203 | } |
||
204 | |||
205 | $q = $handler->createInsertQuery(); |
||
206 | $q->insertInto($handler->quoteIdentifier($table)); |
||
207 | |||
208 | // Contains the bound parameters |
||
209 | $values = array(); |
||
210 | |||
211 | // Binding the parameters |
||
212 | foreach ($rows[0] as $col => $val) { |
||
213 | $q->set( |
||
214 | $handler->quoteIdentifier($col), |
||
215 | $q->bindParam($values[$col]) |
||
216 | ); |
||
217 | } |
||
218 | |||
219 | $stmt = $q->prepare(); |
||
220 | |||
221 | foreach ($rows as $row) { |
||
222 | try { |
||
223 | // This CANNOT be replaced by: |
||
224 | // $values = $row |
||
225 | // each $values[$col] is a PHP reference which should be |
||
226 | // kept for parameters binding to work |
||
227 | foreach ($row as $col => $val) { |
||
228 | $values[$col] = $val; |
||
229 | } |
||
230 | |||
231 | $stmt->execute(); |
||
232 | } catch (Exception $e) { |
||
233 | echo "$table ( ", implode(', ', $row), " )\n"; |
||
234 | throw $e; |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | |||
239 | $this->applyStatements($this->getPostInsertStatements()); |
||
240 | } |
||
241 | |||
242 | protected function getInitialVarDir() |
||
246 | |||
247 | protected function cleanupVarDir($sourceDir) |
||
257 | |||
258 | /** |
||
259 | * CLears internal in memory caches after inserting data circumventing the |
||
260 | * API. |
||
261 | */ |
||
262 | protected function clearInternalCaches() |
||
282 | |||
283 | /** |
||
284 | * Returns statements to be executed after data insert. |
||
285 | * |
||
286 | * @return string[] |
||
287 | */ |
||
288 | protected function getPostInsertStatements() |
||
298 | |||
299 | /** |
||
300 | * Returns the initial database data. |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | protected function getInitialData() |
||
312 | |||
313 | /** |
||
314 | * Initializes the database schema. |
||
315 | */ |
||
316 | protected function initializeSchema() |
||
327 | |||
328 | /** |
||
329 | * Import database schema from Doctrine Schema Yaml configuration file. |
||
330 | * |
||
331 | * @param string $schemaFilePath Yaml schema configuration file path |
||
332 | * |
||
333 | * @throws \Doctrine\DBAL\ConnectionException |
||
334 | */ |
||
335 | private function createSchema(string $schemaFilePath) |
||
367 | |||
368 | /** |
||
369 | * @param \Doctrine\DBAL\Schema\Schema $newSchema |
||
370 | * @param \Doctrine\DBAL\Platforms\AbstractPlatform $databasePlatform |
||
371 | * @param \Doctrine\DBAL\Connection $connection |
||
372 | * |
||
373 | * @return string[] |
||
374 | */ |
||
375 | protected function getDropSqlStatementsForExistingSchema( |
||
393 | |||
394 | /** |
||
395 | * Applies the given SQL $statements to the database in use. |
||
396 | * |
||
397 | * @param array $statements |
||
398 | */ |
||
399 | protected function applyStatements(array $statements) |
||
405 | |||
406 | // ************* Setup copied and refactored from common.php ************ |
||
407 | |||
408 | /** |
||
409 | * Returns the database schema as an array of SQL statements. |
||
410 | * |
||
411 | * @return string[] |
||
412 | */ |
||
413 | protected function getSchemaStatements() |
||
419 | |||
420 | /** |
||
421 | * Returns the database handler from the service container. |
||
422 | * |
||
423 | * @return \eZ\Publish\Core\Persistence\Doctrine\ConnectionHandler |
||
424 | */ |
||
425 | protected function getDatabaseHandler() |
||
429 | |||
430 | /** |
||
431 | * Returns the raw database connection from the service container. |
||
432 | * |
||
433 | * @return \Doctrine\DBAL\Connection |
||
434 | */ |
||
435 | private function getDatabaseConnection(): Connection |
||
439 | |||
440 | /** |
||
441 | * Returns the service container used for initialization of the repository. |
||
442 | * |
||
443 | * @return \eZ\Publish\Core\Base\ServiceContainer |
||
444 | */ |
||
445 | public function getServiceContainer() |
||
487 | |||
488 | /** |
||
489 | * This is intended to be used from external repository in order to |
||
490 | * enable container customization. |
||
491 | * |
||
492 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $containerBuilder |
||
493 | */ |
||
494 | protected function externalBuildContainer(ContainerBuilder $containerBuilder) |
||
498 | |||
499 | /** |
||
500 | * Get the Database name. |
||
501 | * |
||
502 | * @return string |
||
503 | */ |
||
504 | public function getDB() |
||
508 | } |
||
509 |
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.