Total Complexity | 51 |
Total Lines | 283 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ResetAutoIncrementORMPurger 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.
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 ResetAutoIncrementORMPurger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class ResetAutoIncrementORMPurger implements PurgerInterface, ORMPurgerInterface |
||
51 | { |
||
52 | public const PURGE_MODE_DELETE = 1; |
||
53 | public const PURGE_MODE_TRUNCATE = 2; |
||
54 | |||
55 | /** @var EntityManagerInterface|null */ |
||
56 | private $em; |
||
57 | |||
58 | /** |
||
59 | * If the purge should be done through DELETE or TRUNCATE statements |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | private $purgeMode = self::PURGE_MODE_DELETE; |
||
64 | |||
65 | /** |
||
66 | * Table/view names to be excluded from purge |
||
67 | * |
||
68 | * @var string[] |
||
69 | */ |
||
70 | private $excluded; |
||
71 | |||
72 | /** |
||
73 | * Construct new purger instance. |
||
74 | * |
||
75 | * @param EntityManagerInterface|null $em EntityManagerInterface instance used for persistence. |
||
76 | * @param string[] $excluded array of table/view names to be excluded from purge |
||
77 | */ |
||
78 | public function __construct(?EntityManagerInterface $em = null, array $excluded = []) |
||
79 | { |
||
80 | $this->em = $em; |
||
81 | $this->excluded = $excluded; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Set the purge mode |
||
86 | * |
||
87 | * @param int $mode |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | public function setPurgeMode(int $mode): void |
||
92 | { |
||
93 | $this->purgeMode = $mode; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get the purge mode |
||
98 | * |
||
99 | * @return int |
||
100 | */ |
||
101 | public function getPurgeMode(): int |
||
102 | { |
||
103 | return $this->purgeMode; |
||
104 | } |
||
105 | |||
106 | /** @inheritDoc */ |
||
107 | public function setEntityManager(EntityManagerInterface $em): void |
||
108 | { |
||
109 | $this->em = $em; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Retrieve the EntityManagerInterface instance this purger instance is using. |
||
114 | * |
||
115 | * @return EntityManagerInterface |
||
116 | */ |
||
117 | public function getObjectManager(): ?EntityManagerInterface |
||
118 | { |
||
119 | return $this->em; |
||
120 | } |
||
121 | |||
122 | /** @inheritDoc */ |
||
123 | public function purge(): void |
||
124 | { |
||
125 | $classes = []; |
||
126 | |||
127 | foreach ($this->em->getMetadataFactory()->getAllMetadata() as $metadata) { |
||
|
|||
128 | if ($metadata->isMappedSuperclass || (isset($metadata->isEmbeddedClass) && $metadata->isEmbeddedClass)) { |
||
129 | continue; |
||
130 | } |
||
131 | |||
132 | $classes[] = $metadata; |
||
133 | } |
||
134 | |||
135 | $commitOrder = $this->getCommitOrder($this->em, $classes); |
||
136 | |||
137 | // Get platform parameters |
||
138 | $platform = $this->em->getConnection()->getDatabasePlatform(); |
||
139 | |||
140 | // Drop association tables first |
||
141 | $orderedTables = $this->getAssociationTables($commitOrder, $platform); |
||
142 | |||
143 | // Drop tables in reverse commit order |
||
144 | for ($i = count($commitOrder) - 1; $i >= 0; --$i) { |
||
145 | $class = $commitOrder[$i]; |
||
146 | |||
147 | if ( |
||
148 | (isset($class->isEmbeddedClass) && $class->isEmbeddedClass) || |
||
149 | $class->isMappedSuperclass || |
||
150 | ($class->isInheritanceTypeSingleTable() && $class->name !== $class->rootEntityName) |
||
151 | ) { |
||
152 | continue; |
||
153 | } |
||
154 | |||
155 | $orderedTables[] = $this->getTableName($class, $platform); |
||
156 | } |
||
157 | |||
158 | $connection = $this->em->getConnection(); |
||
159 | $filterExpr = method_exists( |
||
160 | $connection->getConfiguration(), |
||
161 | 'getFilterSchemaAssetsExpression' |
||
162 | ) ? $connection->getConfiguration()->getFilterSchemaAssetsExpression() : null; |
||
163 | $emptyFilterExpression = empty($filterExpr); |
||
164 | |||
165 | $schemaAssetsFilter = method_exists( |
||
166 | $connection->getConfiguration(), |
||
167 | 'getSchemaAssetsFilter' |
||
168 | ) ? $connection->getConfiguration()->getSchemaAssetsFilter() : null; |
||
169 | |||
170 | //Disable foreign key checks |
||
171 | if($platform instanceof AbstractMySQLPlatform) { |
||
172 | $connection->executeQuery('SET foreign_key_checks = 0;'); |
||
173 | } |
||
174 | |||
175 | foreach ($orderedTables as $tbl) { |
||
176 | // If we have a filter expression, check it and skip if necessary |
||
177 | if (! $emptyFilterExpression && ! preg_match($filterExpr, $tbl)) { |
||
178 | continue; |
||
179 | } |
||
180 | |||
181 | // If the table is excluded, skip it as well |
||
182 | if (array_search($tbl, $this->excluded) !== false) { |
||
183 | continue; |
||
184 | } |
||
185 | |||
186 | // Support schema asset filters as presented in |
||
187 | if (is_callable($schemaAssetsFilter) && ! $schemaAssetsFilter($tbl)) { |
||
188 | continue; |
||
189 | } |
||
190 | |||
191 | if ($this->purgeMode === self::PURGE_MODE_DELETE) { |
||
192 | $connection->executeStatement($this->getDeleteFromTableSQL($tbl, $platform)); |
||
193 | } else { |
||
194 | $connection->executeStatement($platform->getTruncateTableSQL($tbl, true)); |
||
195 | } |
||
196 | |||
197 | //Reseting autoincrement is only supported on MySQL platforms |
||
198 | if ($platform instanceof AbstractMySQLPlatform) { |
||
199 | $connection->beginTransaction(); |
||
200 | $connection->executeQuery($this->getResetAutoIncrementSQL($tbl, $platform)); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | //Reenable foreign key checks |
||
205 | if($platform instanceof AbstractMySQLPlatform) { |
||
206 | $connection->executeQuery('SET foreign_key_checks = 1;'); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | private function getResetAutoIncrementSQL(string $tableName, AbstractPlatform $platform): string |
||
211 | { |
||
212 | $tableIdentifier = new Identifier($tableName); |
||
213 | |||
214 | return 'ALTER TABLE '. $tableIdentifier->getQuotedName($platform) .' AUTO_INCREMENT = 1;'; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * @param ClassMetadata[] $classes |
||
219 | * |
||
220 | * @return ClassMetadata[] |
||
221 | */ |
||
222 | private function getCommitOrder(EntityManagerInterface $em, array $classes): array |
||
223 | { |
||
224 | $sorter = new TopologicalSorter(); |
||
225 | |||
226 | foreach ($classes as $class) { |
||
227 | if (! $sorter->hasNode($class->name)) { |
||
228 | $sorter->addNode($class->name, $class); |
||
229 | } |
||
230 | |||
231 | // $class before its parents |
||
232 | foreach ($class->parentClasses as $parentClass) { |
||
233 | $parentClass = $em->getClassMetadata($parentClass); |
||
234 | $parentClassName = $parentClass->getName(); |
||
235 | |||
236 | if (! $sorter->hasNode($parentClassName)) { |
||
237 | $sorter->addNode($parentClassName, $parentClass); |
||
238 | } |
||
239 | |||
240 | $sorter->addDependency($class->name, $parentClassName); |
||
241 | } |
||
242 | |||
243 | foreach ($class->associationMappings as $assoc) { |
||
244 | if (! $assoc['isOwningSide']) { |
||
245 | continue; |
||
246 | } |
||
247 | |||
248 | $targetClass = $em->getClassMetadata($assoc['targetEntity']); |
||
249 | assert($targetClass instanceof ClassMetadata); |
||
250 | $targetClassName = $targetClass->getName(); |
||
251 | |||
252 | if (! $sorter->hasNode($targetClassName)) { |
||
253 | $sorter->addNode($targetClassName, $targetClass); |
||
254 | } |
||
255 | |||
256 | // add dependency ($targetClass before $class) |
||
257 | $sorter->addDependency($targetClassName, $class->name); |
||
258 | |||
259 | // parents of $targetClass before $class, too |
||
260 | foreach ($targetClass->parentClasses as $parentClass) { |
||
261 | $parentClass = $em->getClassMetadata($parentClass); |
||
262 | $parentClassName = $parentClass->getName(); |
||
263 | |||
264 | if (! $sorter->hasNode($parentClassName)) { |
||
265 | $sorter->addNode($parentClassName, $parentClass); |
||
266 | } |
||
267 | |||
268 | $sorter->addDependency($parentClassName, $class->name); |
||
269 | } |
||
270 | } |
||
271 | } |
||
272 | |||
273 | return array_reverse($sorter->sort()); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param array $classes |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | private function getAssociationTables(array $classes, AbstractPlatform $platform): array |
||
296 | } |
||
297 | |||
298 | private function getTableName(ClassMetadata $class, AbstractPlatform $platform): string |
||
299 | { |
||
300 | if (isset($class->table['schema']) && ! method_exists($class, 'getSchemaName')) { |
||
301 | return $class->table['schema'] . '.' . |
||
302 | $this->em->getConfiguration() |
||
303 | ->getQuoteStrategy() |
||
304 | ->getTableName($class, $platform); |
||
305 | } |
||
306 | |||
307 | return $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param array $assoc |
||
312 | */ |
||
313 | private function getJoinTableName( |
||
326 | } |
||
327 | |||
328 | private function getDeleteFromTableSQL(string $tableName, AbstractPlatform $platform): string |
||
329 | { |
||
330 | $tableIdentifier = new Identifier($tableName); |
||
331 | |||
332 | return 'DELETE FROM ' . $tableIdentifier->getQuotedName($platform); |
||
333 | } |
||
334 | } |
||
335 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.