Complex classes like Schema 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 Schema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class Schema extends AbstractAsset |
||
46 | { |
||
47 | /** |
||
48 | * The namespaces in this schema. |
||
49 | * |
||
50 | * @var array<string, string> |
||
51 | */ |
||
52 | private $namespaces = []; |
||
53 | |||
54 | /** @var array<string, Table> */ |
||
55 | protected $_tables = []; |
||
56 | |||
57 | /** @var array<string, Sequence> */ |
||
58 | protected $_sequences = []; |
||
59 | |||
60 | /** @var SchemaConfig */ |
||
61 | protected $_schemaConfig; |
||
62 | |||
63 | /** |
||
64 | * @param array<Table> $tables |
||
65 | * @param array<Sequence> $sequences |
||
66 | * @param array<string> $namespaces |
||
67 | */ |
||
68 | 1844 | public function __construct( |
|
69 | array $tables = [], |
||
70 | array $sequences = [], |
||
71 | ?SchemaConfig $schemaConfig = null, |
||
72 | array $namespaces = [] |
||
73 | ) { |
||
74 | 1844 | if ($schemaConfig === null) { |
|
75 | 1584 | $schemaConfig = new SchemaConfig(); |
|
76 | } |
||
77 | 1844 | $this->_schemaConfig = $schemaConfig; |
|
78 | 1844 | $this->_setName($schemaConfig->getName() ?: 'public'); |
|
79 | |||
80 | 1844 | foreach ($namespaces as $namespace) { |
|
81 | 7 | $this->createNamespace($namespace); |
|
82 | } |
||
83 | |||
84 | 1844 | foreach ($tables as $table) { |
|
85 | 547 | $this->_addTable($table); |
|
86 | } |
||
87 | |||
88 | 1817 | foreach ($sequences as $sequence) { |
|
89 | 117 | $this->_addSequence($sequence); |
|
90 | } |
||
91 | 1790 | } |
|
92 | |||
93 | public function hasExplicitForeignKeyIndexes() : bool |
||
94 | { |
||
95 | return $this->_schemaConfig->hasExplicitForeignKeyIndexes(); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @throws SchemaException |
||
100 | */ |
||
101 | 1439 | protected function _addTable(Table $table) : void |
|
102 | { |
||
103 | 1439 | $namespaceName = $table->getNamespaceName(); |
|
104 | 1439 | $tableName = $table->getFullQualifiedName($this->getName()); |
|
105 | |||
106 | 1439 | if (isset($this->_tables[$tableName])) { |
|
107 | 27 | throw TableAlreadyExists::new($tableName); |
|
108 | } |
||
109 | |||
110 | 1439 | if ($namespaceName !== null |
|
111 | 1439 | && ! $table->isInDefaultNamespace($this->getName()) |
|
112 | 1439 | && ! $this->hasNamespace($namespaceName)) { |
|
113 | 216 | $this->createNamespace($namespaceName); |
|
114 | } |
||
115 | |||
116 | 1439 | $this->_tables[$tableName] = $table; |
|
117 | 1439 | $table->setSchemaConfig($this->_schemaConfig); |
|
118 | 1439 | } |
|
119 | |||
120 | /** |
||
121 | * @throws SchemaException |
||
122 | */ |
||
123 | 468 | protected function _addSequence(Sequence $sequence) : void |
|
140 | |||
141 | /** |
||
142 | * Returns the namespaces of this schema. |
||
143 | * |
||
144 | * @return array<string, string> A list of namespace names. |
||
|
|||
145 | */ |
||
146 | 763 | public function getNamespaces() : array |
|
150 | |||
151 | /** |
||
152 | * Gets all tables of this schema. |
||
153 | * |
||
154 | * @return array<string, Table> |
||
155 | */ |
||
156 | 844 | public function getTables() : array |
|
160 | |||
161 | /** |
||
162 | * @throws SchemaException |
||
163 | */ |
||
164 | 925 | public function getTable(string $tableName) : Table |
|
165 | { |
||
166 | 925 | $tableName = $this->getFullQualifiedAssetName($tableName); |
|
167 | 925 | if (! isset($this->_tables[$tableName])) { |
|
168 | 27 | throw TableDoesNotExist::new($tableName); |
|
169 | } |
||
170 | |||
171 | 898 | return $this->_tables[$tableName]; |
|
172 | } |
||
173 | |||
174 | 1276 | private function getFullQualifiedAssetName(string $name) : string |
|
175 | { |
||
176 | 1276 | $name = $this->getUnquotedAssetName($name); |
|
177 | |||
178 | 1276 | if (strpos($name, '.') === false) { |
|
179 | 1168 | $name = $this->getName() . '.' . $name; |
|
180 | } |
||
181 | |||
182 | 1276 | return strtolower($name); |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Returns the unquoted representation of a given asset name. |
||
187 | */ |
||
188 | 1411 | private function getUnquotedAssetName(string $assetName) : string |
|
189 | { |
||
190 | 1411 | if ($this->isIdentifierQuoted($assetName)) { |
|
191 | 54 | return $this->trimQuotes($assetName); |
|
192 | } |
||
193 | |||
194 | 1384 | return $assetName; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Does this schema have a namespace with the given name? |
||
199 | */ |
||
200 | 275 | public function hasNamespace(string $namespaceName) : bool |
|
206 | |||
207 | /** |
||
208 | * Does this schema have a table with the given name? |
||
209 | */ |
||
210 | 898 | public function hasTable(string $tableName) : bool |
|
211 | { |
||
212 | 898 | $tableName = $this->getFullQualifiedAssetName($tableName); |
|
216 | |||
217 | /** |
||
218 | * Gets all table names, prefixed with a schema name, even the default one if present. |
||
219 | * |
||
220 | * @return array<int, string> |
||
221 | */ |
||
222 | public function getTableNames() : array |
||
226 | |||
227 | 351 | public function hasSequence(string $sequenceName) : bool |
|
233 | |||
234 | /** |
||
235 | * @throws SchemaException |
||
236 | */ |
||
237 | 243 | public function getSequence(string $sequenceName) : Sequence |
|
246 | |||
247 | /** |
||
248 | * @return array<string, Sequence> |
||
249 | */ |
||
250 | 817 | public function getSequences() : array |
|
254 | |||
255 | /** |
||
256 | * Creates a new namespace. |
||
257 | * |
||
258 | * @return $this |
||
259 | * |
||
260 | * @throws SchemaException |
||
261 | */ |
||
262 | 304 | public function createNamespace(string $namespaceName) : self |
|
274 | |||
275 | /** |
||
276 | * Creates a new table. |
||
277 | */ |
||
278 | 963 | public function createTable(string $tableName) : Table |
|
289 | |||
290 | /** |
||
291 | * Renames a table. |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | 27 | public function renameTable(string $oldTableName, string $newTableName) : self |
|
305 | |||
306 | /** |
||
307 | * Drops a table from the schema. |
||
308 | * |
||
309 | * @return $this |
||
310 | */ |
||
311 | 135 | public function dropTable(string $tableName) : self |
|
319 | |||
320 | /** |
||
321 | * Creates a new sequence. |
||
322 | */ |
||
323 | 351 | public function createSequence(string $sequenceName, int $allocationSize = 1, int $initialValue = 1) : Sequence |
|
330 | |||
331 | /** |
||
332 | * @return $this |
||
333 | */ |
||
334 | 27 | public function dropSequence(string $sequenceName) : self |
|
341 | |||
342 | /** |
||
343 | * Returns an array of necessary SQL queries to create the schema on the given platform. |
||
344 | * |
||
345 | * @return array<int, string> |
||
346 | */ |
||
347 | 321 | public function toSql(AbstractPlatform $platform) : array |
|
354 | |||
355 | /** |
||
356 | * Return an array of necessary SQL queries to drop the schema on the given platform. |
||
357 | * |
||
358 | * @return array<int, string> |
||
359 | */ |
||
360 | 27 | public function toDropSql(AbstractPlatform $platform) : array |
|
367 | |||
368 | /** |
||
369 | * @return array<int, string> |
||
370 | */ |
||
371 | 17 | public function getMigrateToSql(Schema $toSchema, AbstractPlatform $platform) : array |
|
378 | |||
379 | /** |
||
380 | * @return array<int, string> |
||
381 | */ |
||
382 | public function getMigrateFromSql(Schema $fromSchema, AbstractPlatform $platform) : array |
||
389 | |||
390 | 429 | public function visit(Visitor $visitor) : void |
|
408 | |||
409 | /** |
||
410 | * Cloning a Schema triggers a deep clone of all related assets. |
||
411 | */ |
||
412 | 71 | public function __clone() |
|
421 | } |
||
422 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.