edmondscommerce /
doctrine-static-meta
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing; |
||||
| 6 | |||||
| 7 | use Doctrine\Common\Collections\Collection; |
||||
| 8 | use Doctrine\Common\Util\Debug; |
||||
| 9 | use Doctrine\ORM\EntityManagerInterface; |
||||
| 10 | use Doctrine\ORM\Proxy\Proxy; |
||||
| 11 | use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
||||
| 12 | use ReflectionException; |
||||
| 13 | use RuntimeException; |
||||
| 14 | use TypeError; |
||||
| 15 | |||||
| 16 | use function get_class; |
||||
| 17 | use function is_numeric; |
||||
| 18 | use function lcfirst; |
||||
| 19 | use function preg_replace; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||||
| 23 | */ |
||||
| 24 | class EntityDebugDumper |
||||
| 25 | { |
||||
| 26 | /** |
||||
| 27 | * @param EntityInterface $entity |
||||
| 28 | * @param EntityManagerInterface $entityManager |
||||
| 29 | * @param int $level |
||||
| 30 | * |
||||
| 31 | * @return string |
||||
| 32 | * @throws ReflectionException |
||||
| 33 | * @SuppressWarnings(PHPMD.StaticAccess) |
||||
| 34 | * @SuppressWarnings(PHPMD.ElseExpression) |
||||
| 35 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||||
| 36 | */ |
||||
| 37 | 1 | public function dump(EntityInterface $entity, ?EntityManagerInterface $entityManager = null, int $level = 0): string |
|||
| 38 | { |
||||
| 39 | 1 | $dump = []; |
|||
| 40 | 1 | $fieldMappings = []; |
|||
| 41 | 1 | if (null !== $entityManager) { |
|||
| 42 | $metaData = $entityManager->getClassMetadata(get_class($entity)); |
||||
| 43 | $fieldMappings = $metaData->fieldMappings; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 44 | } |
||||
| 45 | 1 | foreach ($entity::getDoctrineStaticMeta()->getGetters() as $getter) { |
|||
| 46 | try { |
||||
| 47 | 1 | $got = $entity->$getter(); |
|||
| 48 | } catch (TypeError $e) { |
||||
| 49 | $got = '( *TypeError*: ' . $e->getMessage() . ' )'; |
||||
| 50 | } |
||||
| 51 | 1 | $fieldName = lcfirst(preg_replace('%^(get|is)%', '', $getter)); |
|||
| 52 | if ( |
||||
| 53 | 1 | is_numeric($got) |
|||
| 54 | 1 | || (isset($fieldMappings[$fieldName]) |
|||
| 55 | 1 | && 'decimal' === $fieldMappings[$fieldName]['type']) |
|||
| 56 | ) { |
||||
| 57 | 1 | $dump[$getter] = (float)$got; |
|||
| 58 | 1 | continue; |
|||
| 59 | } |
||||
| 60 | 1 | if ($got instanceof Proxy) { |
|||
| 61 | $dump[$getter] = 'Proxy class '; |
||||
| 62 | continue; |
||||
| 63 | } |
||||
| 64 | 1 | if ($got instanceof EntityInterface) { |
|||
| 65 | if ($level === 2) { |
||||
| 66 | $dump[$getter] = '(max depth of 2 reached)'; |
||||
| 67 | continue; |
||||
| 68 | } |
||||
| 69 | $dump[$getter] = $this->dump($got, $entityManager, ++$level); |
||||
| 70 | continue; |
||||
| 71 | } |
||||
| 72 | 1 | if ($got instanceof Collection) { |
|||
| 73 | 1 | $dump[$getter] = []; |
|||
| 74 | 1 | foreach ($got as $item) { |
|||
| 75 | 1 | if ($item instanceof EntityInterface) { |
|||
| 76 | 1 | $dump[$getter][] = get_class($item) . ': ' . $item->getId(); |
|||
|
0 ignored issues
–
show
Are you sure
$item->getId() of type Ramsey\Uuid\UuidInterface can be used in concatenation? Consider adding a __toString()-method.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 77 | 1 | continue; |
|||
| 78 | } |
||||
| 79 | throw new RuntimeException('Got unexpected object ' . |
||||
| 80 | get_class($got) . |
||||
| 81 | ' in collection from ' . |
||||
| 82 | $getter); |
||||
| 83 | } |
||||
| 84 | 1 | continue; |
|||
| 85 | } |
||||
| 86 | 1 | if (method_exists($got, '__toString')) { |
|||
| 87 | 1 | $dump[$getter] = (string)$got; |
|||
| 88 | 1 | continue; |
|||
| 89 | } |
||||
| 90 | 1 | $dump[$getter] = Debug::export($got, 2); |
|||
| 91 | } |
||||
| 92 | |||||
| 93 | 1 | return (string)print_r($dump, true); |
|||
| 94 | } |
||||
| 95 | } |
||||
| 96 |