Total Complexity | 47 |
Total Lines | 260 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 2 | Features | 0 |
Complex classes like AuditTrait 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 AuditTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | trait AuditTrait |
||
18 | { |
||
19 | /** |
||
20 | * Returns the primary key value of an entity. |
||
21 | * |
||
22 | * @throws \DH\Auditor\Exception\MappingException |
||
23 | * @throws \Doctrine\DBAL\Exception |
||
24 | * @throws \Doctrine\ORM\Mapping\MappingException |
||
25 | */ |
||
26 | private function id(EntityManagerInterface $entityManager, object $entity): mixed |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Type converts the input value and returns it. |
||
64 | * |
||
65 | * @throws \Doctrine\DBAL\Exception |
||
66 | * @throws \Doctrine\DBAL\Types\ConversionException |
||
67 | */ |
||
68 | private function value(EntityManagerInterface $entityManager, Type $type, mixed $value): mixed |
||
69 | { |
||
70 | if (null === $value) { |
||
71 | return null; |
||
72 | } |
||
73 | |||
74 | if (interface_exists(UnitEnum::class) && $value instanceof UnitEnum && property_exists($value, 'value')) { /** @phpstan-ignore-line */ |
||
75 | $value = $value->value; |
||
76 | } |
||
77 | |||
78 | $platform = $entityManager->getConnection()->getDatabasePlatform(); |
||
79 | |||
80 | switch (array_search($type::class, Type::getTypesMap(), true)) { |
||
81 | case DoctrineHelper::getDoctrineType('BIGINT'): |
||
82 | $convertedValue = (string) $value; // @phpstan-ignore-line |
||
83 | |||
84 | break; |
||
85 | |||
86 | case DoctrineHelper::getDoctrineType('INTEGER'): |
||
87 | case DoctrineHelper::getDoctrineType('SMALLINT'): |
||
88 | $convertedValue = (int) $value; // @phpstan-ignore-line |
||
89 | |||
90 | break; |
||
91 | |||
92 | case DoctrineHelper::getDoctrineType('DECIMAL'): |
||
93 | case DoctrineHelper::getDoctrineType('FLOAT'): |
||
94 | case DoctrineHelper::getDoctrineType('BOOLEAN'): |
||
95 | $convertedValue = $type->convertToPHPValue($value, $platform); |
||
96 | |||
97 | break; |
||
98 | |||
99 | case 'uuid_binary': |
||
100 | case 'uuid_binary_ordered_time': |
||
101 | case 'uuid': |
||
102 | case 'ulid': |
||
103 | // Ramsey UUID / Symfony UID (UUID/ULID) |
||
104 | $convertedValue = (string) $value; // @phpstan-ignore-line |
||
105 | |||
106 | break; |
||
107 | |||
108 | case DoctrineHelper::getDoctrineType('BLOB'): |
||
109 | case DoctrineHelper::getDoctrineType('BINARY'): |
||
110 | if (\is_resource($value)) { |
||
111 | // let's replace resources with a "simple" representation: resourceType#resourceId |
||
112 | $convertedValue = get_resource_type($value).'#'.get_resource_id($value); |
||
113 | } else { |
||
114 | $convertedValue = $type->convertToDatabaseValue($value, $platform); |
||
115 | } |
||
116 | |||
117 | break; |
||
118 | |||
119 | case DoctrineHelper::getDoctrineType('JSON'): |
||
120 | return $value; |
||
121 | |||
122 | default: |
||
123 | $convertedValue = $type->convertToDatabaseValue($value, $platform); |
||
124 | } |
||
125 | |||
126 | return $convertedValue; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Returns the extra fields if set. |
||
131 | */ |
||
132 | private function extraFields(object $entity): array |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Computes a usable diff. |
||
153 | * |
||
154 | * @throws \DH\Auditor\Exception\MappingException |
||
155 | * @throws \Doctrine\DBAL\Exception |
||
156 | * @throws \Doctrine\DBAL\Types\ConversionException |
||
157 | * @throws \Doctrine\ORM\Mapping\MappingException |
||
158 | */ |
||
159 | private function diff(EntityManagerInterface $entityManager, object $entity, array $changeset): array |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Returns an array describing an entity. |
||
206 | * |
||
207 | * @throws \DH\Auditor\Exception\MappingException |
||
208 | * @throws \Doctrine\DBAL\Exception |
||
209 | * @throws \Doctrine\ORM\Mapping\MappingException |
||
210 | */ |
||
211 | private function summarize(EntityManagerInterface $entityManager, ?object $entity = null, array $extra = []): ?array |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * Blames an audit operation. |
||
247 | * |
||
248 | * @return array{client_ip: null|string, user_firewall: null|string, user_fqdn: null|string, user_id: null|string, username: null|string} |
||
249 | */ |
||
250 | private function blame(): array |
||
280 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths