Passed
Pull Request — master (#7506)
by
unknown
09:45
created

SimpleObjectHydrator::hydrateRowData()   C

Complexity

Conditions 16
Paths 57

Size

Total Lines 79
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 16.032

Importance

Changes 0
Metric Value
cc 16
eloc 39
nc 57
nop 2
dl 0
loc 79
ccs 38
cts 40
cp 0.95
crap 16.032
rs 5.5666
c 0
b 0
f 0

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 Doctrine\ORM\Internal\Hydration;
6
7
use Doctrine\DBAL\FetchMode;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Doctrine\ORM\Mapping\InheritanceType;
10
use Doctrine\ORM\Query;
11
use Exception;
12
use RuntimeException;
13
use function array_keys;
14
use function array_search;
15
use function count;
16
use function key;
17
use function reset;
18
use function sprintf;
19
20
class SimpleObjectHydrator extends AbstractHydrator
21
{
22
    /** @var ClassMetadata */
23
    private $class;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 414
    protected function prepare()
29
    {
30 414
        if (count($this->rsm->aliasMap) !== 1) {
31
            throw new RuntimeException('Cannot use SimpleObjectHydrator with a ResultSetMapping that contains more than one object result.');
32
        }
33
34 414
        if ($this->rsm->scalarMappings) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->rsm->scalarMappings of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
35
            throw new RuntimeException('Cannot use SimpleObjectHydrator with a ResultSetMapping that contains scalar mappings.');
36
        }
37
38 414
        $this->class = $this->getClassMetadata(reset($this->rsm->aliasMap));
39 414
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 411
    protected function cleanup()
45
    {
46 411
        parent::cleanup();
47
48 411
        $this->uow->triggerEagerLoads();
49 411
        $this->uow->hydrationComplete();
50 411
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 413
    protected function hydrateAllData()
56
    {
57 413
        $result = [];
58
59 413
        while ($row = $this->stmt->fetch(FetchMode::ASSOCIATIVE)) {
60 360
            $this->hydrateRowData($row, $result);
61
        }
62
63 411
        $this->em->getUnitOfWork()->triggerEagerLoads();
64
65 411
        return $result;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 361
    protected function hydrateRowData(array $sqlResult, array &$result)
72
    {
73 361
        $entityName = $this->class->getClassName();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74 361
        $data       = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 7 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
75 361
        $discrColumnValue = null;
76
77
        // We need to find the correct entity class name if we have inheritance in resultset
78 361
        if ($this->class->inheritanceType !== InheritanceType::NONE) {
79 80
            $discrColumnName            = $this->platform->getSQLResultCasing(
80 80
                $this->class->discriminatorColumn->getColumnName()
81
            );
82 80
            $metaMappingDiscrColumnName = array_search($discrColumnName, $this->rsm->metaMappings);
83
84
            // Find mapped discriminator column from the result set.
85 80
            if ($metaMappingDiscrColumnName) {
86 80
                $discrColumnName = $metaMappingDiscrColumnName;
87
            }
88
89 80
            if (! isset($sqlResult[$discrColumnName])) {
90 1
                throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->rsm->aliasMap));
91
            }
92
93 79
            if ($sqlResult[$discrColumnName] === '') {
94
                throw HydrationException::emptyDiscriminatorValue(key($this->rsm->aliasMap));
95
            }
96
97 79
            $discrMap = $this->class->discriminatorMap;
98
99 79
            if (! isset($discrMap[$sqlResult[$discrColumnName]])) {
100 1
                throw HydrationException::invalidDiscriminatorValue($sqlResult[$discrColumnName], array_keys($discrMap));
101
            }
102
103 78
            $entityName = $discrMap[$sqlResult[$discrColumnName]];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
104 78
            $discrColumnValue = $sqlResult[$discrColumnName];
105
106 78
            unset($sqlResult[$discrColumnName]);
107
        }
108
109 359
        foreach ($sqlResult as $column => $value) {
110
            // An ObjectHydrator should be used instead of SimpleObjectHydrator
111 359
            if (isset($this->rsm->relationMap[$column])) {
112
                throw new Exception(sprintf('Unable to retrieve association information for column "%s"', $column));
113
            }
114
115 359
            $cacheKeyInfo = $this->hydrateColumnInfo($column);
116
117 359
            if (! $cacheKeyInfo) {
118 1
                continue;
119
            }
120
121
            // Check if value is null before conversion (because some types convert null to something else)
122 359
            $valueIsNull = $value === null;
123
124
            // Convert field to a valid PHP value
125 359
            if (isset($cacheKeyInfo['type'])) {
126 359
                $type  = $cacheKeyInfo['type'];
127 359
                $value = $type->convertToPHPValue($value, $this->platform);
128
            }
129
130 359
            $fieldName = $cacheKeyInfo['fieldName'];
131
132
            // Prevent overwrite in case of inherit classes using same property name (See AbstractHydrator)
133 359
            if (! isset($data[$fieldName]) || ! $valueIsNull) {
134
                // If we have inheritance in resultset, make sure the field belongs to the correct class
135 359
                if (isset($cacheKeyInfo['discriminatorValues']) && !in_array($discrColumnValue, $cacheKeyInfo['discriminatorValues'])) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
introduced by
Function in_array() should not be referenced via a fallback global name, but via a use statement.
Loading history...
136 31
                    continue;
137
                }
138
139 359
                $data[$fieldName] = $value;
140
            }
141
        }
142
143 359
        $uow    = $this->em->getUnitOfWork();
144 359
        $entity = $uow->createEntity($entityName, $data, $this->hints);
145
146 359
        $result[] = $entity;
147
148 359
        if (isset($this->hints[Query::HINT_INTERNAL_ITERATION]) && $this->hints[Query::HINT_INTERNAL_ITERATION]) {
149 1
            $this->uow->hydrationComplete();
150
        }
151 359
    }
152
}
153