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 Migration 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 Migration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Migration |
||
16 | { |
||
17 | protected $migrationsDir; |
||
18 | protected $migrationsNamespace; |
||
19 | protected $adapter; |
||
20 | /** |
||
21 | * @var \Zend\Db\Adapter\Driver\ConnectionInterface |
||
22 | */ |
||
23 | protected $connection; |
||
24 | protected $metadata; |
||
25 | protected $migrationVersionTable; |
||
26 | protected $outputWriter; |
||
27 | protected $serviceLocator; |
||
28 | |||
29 | /** |
||
30 | * @return OutputWriter |
||
31 | */ |
||
32 | public function getOutputWriter() |
||
36 | |||
37 | /** |
||
38 | * @param \Zend\Db\Adapter\Adapter $adapter |
||
39 | * @param $metadata |
||
40 | * @param array $config |
||
41 | * @param $migrationVersionTable |
||
42 | * @param VersionResolver $versionResolver |
||
43 | * @param OutputWriter $writer |
||
44 | * @param null $serviceLocator |
||
45 | * @throws \Exception |
||
46 | */ |
||
47 | public function __construct( |
||
80 | |||
81 | /** |
||
82 | * @return int |
||
83 | */ |
||
84 | public function getCurrentVersion() |
||
88 | |||
89 | /** |
||
90 | * @param int $version target migration version, if not set all not applied available migrations will be applied |
||
91 | * @param bool $force force apply migration |
||
92 | * @param bool $down rollback migration |
||
93 | * @param bool $fake |
||
94 | * @throws \Exception |
||
95 | */ |
||
96 | public function migrate($version = null, $force = false, $down = false, $fake = false) |
||
130 | |||
131 | /** |
||
132 | * @param \ArrayIterator $migrations |
||
133 | * @return \ArrayIterator |
||
134 | */ |
||
135 | public function sortMigrationsByVersionDesc(\ArrayIterator $migrations) |
||
151 | |||
152 | /** |
||
153 | * Check migrations classes existence |
||
154 | * |
||
155 | * @param \ArrayIterator $migrations |
||
156 | * @param int $version |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function hasMigrationVersions(\ArrayIterator $migrations, $version) |
||
169 | |||
170 | /** |
||
171 | * @param \ArrayIterator $migrations |
||
172 | * @return int |
||
173 | */ |
||
174 | public function getMaxMigrationVersion(\ArrayIterator $migrations) |
||
186 | |||
187 | protected function applyMigration(array $migration, $down = false, $fake = false) |
||
261 | } |
||
262 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: