Passed
Pull Request — master (#1463)
by Marcin
11:42
created

DoctrineTypeDriver::setPropertyType()   C

Complexity

Conditions 12
Paths 7

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 12.2812

Importance

Changes 0
Metric Value
cc 12
eloc 18
nc 7
nop 2
dl 0
loc 36
ccs 7
cts 8
cp 0.875
crap 12.2812
rs 6.9666
c 0
b 0
f 0

How to fix   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 JMS\Serializer\Metadata\Driver;
6
7
use Doctrine\ORM\Mapping\ClassMetadataInfo;
8
use Doctrine\Persistence\Mapping\ClassMetadata as DoctrineClassMetadata;
9
use JMS\Serializer\Metadata\ClassMetadata;
10
use JMS\Serializer\Metadata\PropertyMetadata;
11
12
/**
13
 * This class decorates any other driver. If the inner driver does not provide a
14
 * a property type, the decorator will guess based on Doctrine 2 metadata.
15
 */
16
class DoctrineTypeDriver extends AbstractDoctrineTypeDriver
17 10
{
18
    protected function setDiscriminator(DoctrineClassMetadata $doctrineMetadata, ClassMetadata $classMetadata): void
19 10
    {
20 10
        if (
21
            empty($classMetadata->discriminatorMap) && !$classMetadata->discriminatorDisabled
22 3
            && !empty($doctrineMetadata->discriminatorMap) && $doctrineMetadata->isRootEntity()
0 ignored issues
show
Bug introduced by
Accessing discriminatorMap on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
The method isRootEntity() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. It seems like you code against a sub-type of Doctrine\Persistence\Mapping\ClassMetadata such as Doctrine\ORM\Mapping\ClassMetadataInfo. ( Ignorable by Annotation )

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

22
            && !empty($doctrineMetadata->discriminatorMap) && $doctrineMetadata->/** @scrutinizer ignore-call */ isRootEntity()
Loading history...
23 3
        ) {
24 3
            $classMetadata->setDiscriminator(
25
                $doctrineMetadata->discriminatorColumn['name'],
0 ignored issues
show
Bug introduced by
Accessing discriminatorColumn on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
26
                $doctrineMetadata->discriminatorMap
27 10
            );
28
        }
29 10
    }
30
31 10
    /**
32 10
     * @param ClassMetadataInfo $doctrineMetadata
33 10
     * @param PropertyMetadata $propertyMetadata
34 8
     *
35 8
     * @return void
36
     */
37 8
    protected function setPropertyType(DoctrineClassMetadata $doctrineMetadata, PropertyMetadata $propertyMetadata): void
38
    {
39
        $propertyName = $propertyMetadata->name;
40
        if (
41
            $doctrineMetadata->hasField($propertyName)
42
            && ($typeOfFiled = $doctrineMetadata->getTypeOfField($propertyName))
43
            && ($fieldType = $this->normalizeFieldType($typeOfFiled))
44 8
        ) {
45 1
            if (PHP_VERSION_ID >= 80100 && ($fieldMapping = $doctrineMetadata->getFieldMapping($propertyName)) && isset($fieldMapping['enumType'])) {
0 ignored issues
show
Bug introduced by
The method getFieldMapping() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. It seems like you code against a sub-type of Doctrine\Persistence\Mapping\ClassMetadata such as Doctrine\ORM\Mapping\ClassMetadataInfo or Doctrine\ODM\PHPCR\Mapping\ClassMetadata. ( Ignorable by Annotation )

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

45
            if (PHP_VERSION_ID >= 80100 && ($fieldMapping = $doctrineMetadata->/** @scrutinizer ignore-call */ getFieldMapping($propertyName)) && isset($fieldMapping['enumType'])) {
Loading history...
46
                $fieldType = $fieldMapping['enumType'];
47
            }
48 7
49 7
            $propertyMetadata->setType($this->typeParser->parse($fieldType));
50
51
            return;
52 7
        }
53
54 10
        if ($doctrineMetadata->hasAssociation($propertyName)) {
55
            $targetEntity = $doctrineMetadata->getAssociationTargetClass($propertyName);
56
57
            if (null === $targetMetadata = $this->tryLoadingDoctrineMetadata($targetEntity)) {
0 ignored issues
show
Bug introduced by
It seems like $targetEntity can also be of type null; however, parameter $className of JMS\Serializer\Metadata\...adingDoctrineMetadata() 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

57
            if (null === $targetMetadata = $this->tryLoadingDoctrineMetadata(/** @scrutinizer ignore-type */ $targetEntity)) {
Loading history...
58
                return;
59
            }
60
61
            // For inheritance schemes, we cannot add any type as we would only add the super-type of the hierarchy.
62
            // On serialization, this would lead to only the supertype being serialized, and properties of subtypes
63
            // being ignored.
64
            if ($targetMetadata instanceof DoctrineClassMetadata && !$targetMetadata->isInheritanceTypeNone()) {
0 ignored issues
show
Bug introduced by
The method isInheritanceTypeNone() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. It seems like you code against a sub-type of Doctrine\Persistence\Mapping\ClassMetadata such as Doctrine\ORM\Mapping\ClassMetadataInfo. ( Ignorable by Annotation )

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

64
            if ($targetMetadata instanceof DoctrineClassMetadata && !$targetMetadata->/** @scrutinizer ignore-call */ isInheritanceTypeNone()) {
Loading history...
65
                return;
66
            }
67
68
            if (!$doctrineMetadata->isSingleValuedAssociation($propertyName)) {
69
                $targetEntity = sprintf('ArrayCollection<%s>', $targetEntity);
70
            }
71
72
            $propertyMetadata->setType($this->typeParser->parse($targetEntity));
0 ignored issues
show
Bug introduced by
It seems like $targetEntity can also be of type null; however, parameter $type of JMS\Serializer\Type\ParserInterface::parse() 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

72
            $propertyMetadata->setType($this->typeParser->parse(/** @scrutinizer ignore-type */ $targetEntity));
Loading history...
73
        }
74
    }
75
}
76