Completed
Pull Request — master (#677)
by Amrouche
04:06
created

QueryJoinParser   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 136
Duplicated Lines 30.88 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 4
dl 42
loc 136
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getClassMetadataFromJoinAlias() 0 58 6
A getJoinRelationship() 14 14 4
A getJoinAlias() 14 14 4
A getOrderByParts() 14 14 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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