MetadataPropertyFactory::createProperties()   B
last analyzed

Complexity

Conditions 8
Paths 16

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 19
c 1
b 0
f 1
nc 16
nop 1
dl 0
loc 34
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Bridge\Doctrine\ORM\Metadata;
6
7
use LAG\AdminBundle\Metadata\Property\BooleanProperty;
8
use LAG\AdminBundle\Metadata\Property\CountProperty;
9
use LAG\AdminBundle\Metadata\Property\DateProperty;
10
use LAG\AdminBundle\Metadata\Property\StringProperty;
11
12
class MetadataPropertyFactory implements MetadataPropertyFactoryInterface
13
{
14
    public function __construct(
15
        private MetadataHelperInterface $metadataHelper,
16
    ) {
17
    }
18
19
    public function createProperties(string $resourceClass): array
20
    {
21
        $metadata = $this->metadataHelper->findMetadata($resourceClass);
22
        $properties = [];
23
24
        if ($metadata === null) {
25
            return $properties;
26
        }
27
28
        foreach ($metadata->getFieldNames() as $fieldName) {
29
            $fieldType = $metadata->getTypeOfField($fieldName);
30
31
            if (str_contains($fieldType, 'datetime')) {
0 ignored issues
show
Bug introduced by
It seems like $fieldType can also be of type null; however, parameter $haystack of str_contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
            if (str_contains(/** @scrutinizer ignore-type */ $fieldType, 'datetime')) {
Loading history...
32
                $properties[$fieldName] = new DateProperty($fieldName);
33
            } elseif ($fieldType === 'boolean') {
34
                $properties[$fieldName] = new BooleanProperty($fieldName);
35
            } else {
36
                $properties[$fieldName] = new StringProperty($fieldName);
37
            }
38
39
            if (\count($properties) > 10) {
40
                return $properties;
41
            }
42
        }
43
44
        foreach ($metadata->getAssociationNames() as $associationName) {
45
            $properties[$associationName] = new CountProperty($associationName);
46
47
            if (\count($properties) > 10) {
48
                return $properties;
49
            }
50
        }
51
52
        return $properties;
53
    }
54
}
55