| Conditions | 10 |
| Paths | 7 |
| Total Lines | 98 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 243 | private function migrateSingleAssetToResourceNode( |
||
| 244 | string $context, |
||
| 245 | string $tableName, |
||
| 246 | string $rowId, |
||
| 247 | Asset $asset, |
||
| 248 | AssetRepository $assetRepo, |
||
| 249 | ResourceType $resourceType |
||
| 250 | ): bool { |
||
| 251 | $content = $assetRepo->getAssetContent($asset); |
||
| 252 | |||
| 253 | if (!\is_string($content) || '' === $content) { |
||
| 254 | $filePath = ''; |
||
| 255 | $exists = null; |
||
| 256 | |||
| 257 | try { |
||
| 258 | $filePath = $assetRepo->getStorage()->resolveUri($asset); |
||
| 259 | $exists = $assetRepo->getFileSystem()->fileExists($filePath); |
||
| 260 | } catch (Throwable $e) { |
||
| 261 | error_log( |
||
| 262 | sprintf( |
||
| 263 | '[MIGRATION][%s] Error while checking filesystem for Asset id=%s: %s', |
||
| 264 | $context, |
||
| 265 | (string) $asset->getId(), |
||
| 266 | $e->getMessage() |
||
| 267 | ) |
||
| 268 | ); |
||
| 269 | } |
||
| 270 | |||
| 271 | error_log( |
||
| 272 | sprintf( |
||
| 273 | '[MIGRATION][%s] Empty content for Asset id=%s, title=%s, mime=%s, size=%d, path=%s, exists=%s', |
||
| 274 | $context, |
||
| 275 | (string) $asset->getId(), |
||
| 276 | (string) $asset->getTitle(), |
||
| 277 | (string) $asset->getMimeType(), |
||
| 278 | (int) $asset->getSize(), |
||
| 279 | $filePath, |
||
| 280 | true === $exists ? 'yes' : 'no' |
||
| 281 | ) |
||
| 282 | ); |
||
| 283 | |||
| 284 | return false; |
||
| 285 | } |
||
| 286 | |||
| 287 | $originalName = $asset->getTitle(); |
||
| 288 | $mimeType = 'application/octet-stream'; |
||
| 289 | |||
| 290 | if (method_exists($asset, 'getMimeType') && null !== $asset->getMimeType()) { |
||
| 291 | $mimeType = (string) $asset->getMimeType(); |
||
| 292 | } |
||
| 293 | |||
| 294 | if (self::DRY_RUN) { |
||
| 295 | error_log( |
||
| 296 | sprintf( |
||
| 297 | '[MIGRATION][DRY_RUN][%s] Would create ResourceNode/ResourceFile for table=%s rowId=%s', |
||
| 298 | $context, |
||
| 299 | $tableName, |
||
| 300 | bin2hex($rowId) |
||
| 301 | ) |
||
| 302 | ); |
||
| 303 | |||
| 304 | return true; |
||
| 305 | } |
||
| 306 | |||
| 307 | $node = new ResourceNode(); |
||
| 308 | $node->setTitle($originalName ?: 'attempt_' . $context); |
||
| 309 | $node->setResourceType($resourceType); |
||
| 310 | |||
| 311 | $this->entityManager->persist($node); |
||
| 312 | |||
| 313 | $uploadedFile = CreateUploadedFileHelper::fromString( |
||
| 314 | $originalName ?: 'file_' . $context, |
||
| 315 | $mimeType, |
||
| 316 | $content |
||
| 317 | ); |
||
| 318 | |||
| 319 | $resourceFile = new ResourceFile(); |
||
| 320 | $resourceFile->setResourceNode($node); |
||
| 321 | $resourceFile->setFile($uploadedFile); |
||
| 322 | $this->entityManager->persist($resourceFile); |
||
| 323 | $this->entityManager->flush(); |
||
| 324 | |||
| 325 | $this->connection->update( |
||
| 326 | $tableName, |
||
| 327 | ['resource_node_id' => $node->getId()], |
||
| 328 | ['id' => $rowId] |
||
| 329 | ); |
||
| 330 | |||
| 331 | error_log( |
||
| 332 | sprintf( |
||
| 333 | '[MIGRATION][%s] Migrated rowId=%s to ResourceNode id=%d', |
||
| 334 | $context, |
||
| 335 | bin2hex($rowId), |
||
| 336 | (int) $node->getId() |
||
| 337 | ) |
||
| 338 | ); |
||
| 339 | |||
| 340 | return true; |
||
| 341 | } |
||
| 343 |
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.