Completed
Pull Request — master (#677)
by Amrouche
09:37 queued 05:14
created

QueryJoinParser::getClassMetadataFromJoinAlias()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 8.7274
c 0
b 0
f 0
cc 6
eloc 35
nc 18
nop 3

How to fix   Long Method   

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
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util;
13
14
use Doctrine\Common\Persistence\ManagerRegistry;
15
use Doctrine\ORM\Mapping\ClassMetadata;
16
use Doctrine\ORM\Query\Expr\Join;
17
use Doctrine\ORM\Query\Expr\OrderBy;
18
use Doctrine\ORM\QueryBuilder;
19
20
/**
21
 * Utility functions for working with Doctrine ORM query.
22
 *
23
 * @author Teoh Han Hui <[email protected]>
24
 * @author Vincent Chalamon <[email protected]>
25
 */
26
abstract class QueryJoinParser
27
{
28
    /**
29
     * Gets the class metadata from a given join alias.
30
     *
31
     * @param string          $alias
32
     * @param QueryBuilder    $queryBuilder
33
     * @param ManagerRegistry $managerRegistry
34
     *
35
     * @return ClassMetadata
36
     */
37
    public static function getClassMetadataFromJoinAlias(string $alias, QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry) : ClassMetadata
38
    {
39
        $rootEntities = $queryBuilder->getRootEntities();
40
        $rootAliases = $queryBuilder->getRootAliases();
41
42
        $joinParts = $queryBuilder->getDQLPart('join');
43
44
        $aliasMap = [];
45
        $targetAlias = $alias;
46
47
        foreach ($joinParts as $rootAlias => $joins) {
48
            $aliasMap[$rootAlias] = 'root';
49
50
            foreach ($joins as $join) {
51
                $alias = self::getJoinAlias($join);
52
                $relationship = self::getJoinRelationship($join);
53
54
                $pos = strpos($relationship, '.');
55
56
                $aliasMap[$alias] = [
57
                    'parentAlias' => substr($relationship, 0, $pos),
58
                    'association' => substr($relationship, $pos + 1),
59
                ];
60
            }
61
        }
62
63
        $associationStack = [];
64
        $rootAlias = null;
65
66
        while (null === $rootAlias) {
67
            $mapping = $aliasMap[$targetAlias];
68
69
            if ('root' === $mapping) {
70
                $rootAlias = $targetAlias;
71
            } else {
72
                $associationStack[] = $mapping['association'];
73
                $targetAlias = $mapping['parentAlias'];
74
            }
75
        }
76
77
        $rootEntity = $rootEntities[array_search($rootAlias, $rootAliases)];
78
79
        $rootMetadata = $managerRegistry
80
            ->getManagerForClass($rootEntity)
81
            ->getClassMetadata($rootEntity);
82
83
        $metadata = $rootMetadata;
84
85
        while (null !== ($association = array_pop($associationStack))) {
86
            $associationClass = $metadata->getAssociationTargetClass($association);
87
88
            $metadata = $managerRegistry
89
                ->getManagerForClass($associationClass)
90
                ->getClassMetadata($associationClass);
91
        }
92
93
        return $metadata;
94
    }
95
96
    /**
97
     * Gets the relationship from a Join expression.
98
     *
99
     * @param Join $join
100
     *
101
     * @return string
102
     */
103 View Code Duplication
    public static function getJoinRelationship(Join $join) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        static $relationshipProperty = null;
106
        static $initialized = false;
107
108
        if (!$initialized && !method_exists(Join::class, 'getJoin')) {
109
            $relationshipProperty = new \ReflectionProperty(Join::class, '_join');
110
            $relationshipProperty->setAccessible(true);
111
112
            $initialized = true;
113
        }
114
115
        return (null === $relationshipProperty) ? $join->getJoin() : $relationshipProperty->getValue($join);
116
    }
117
118
    /**
119
     * Gets the alias from a Join expression.
120
     *
121
     * @param Join $join
122
     *
123
     * @return string
124
     */
125 View Code Duplication
    public static function getJoinAlias(Join $join) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        static $aliasProperty = null;
128
        static $initialized = false;
129
130
        if (!$initialized && !method_exists(Join::class, 'getAlias')) {
131
            $aliasProperty = new \ReflectionProperty(Join::class, '_alias');
132
            $aliasProperty->setAccessible(true);
133
134
            $initialized = true;
135
        }
136
137
        return (null === $aliasProperty) ? $join->getAlias() : $aliasProperty->getValue($join);
138
    }
139
140
    /**
141
     * Gets the parts from an OrderBy expression.
142
     *
143
     * @param OrderBy $orderBy
144
     *
145
     * @return string[]
146
     */
147 View Code Duplication
    public static function getOrderByParts(OrderBy $orderBy) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        static $partsProperty = null;
150
        static $initialized = false;
151
152
        if (!$initialized && !method_exists(OrderBy::class, 'getParts')) {
153
            $partsProperty = new \ReflectionProperty(OrderBy::class, '_parts');
154
            $partsProperty->setAccessible(true);
155
156
            $initialized = true;
157
        }
158
159
        return (null === $partsProperty) ? $orderBy->getParts() : $partsProperty->getValue($orderBy);
160
    }
161
}
162