GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

EntityDebugDumper::dump()   C
last analyzed

Complexity

Conditions 14
Paths 38

Size

Total Lines 57
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 44.1315

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 42
nc 38
nop 3
dl 0
loc 57
rs 6.2666
c 1
b 0
f 0
ccs 26
cts 56
cp 0.4643
crap 44.1315

How to fix   Long Method    Complexity   

Long Method

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:

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
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
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
Bug introduced by
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 ignore-type  annotation

76
                        $dump[$getter][] = get_class($item) . ': ' . /** @scrutinizer ignore-type */ $item->getId();
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