Total Complexity | 41 |
Total Lines | 190 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like QueryChecker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryChecker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | final class QueryChecker |
||
30 | { |
||
31 | private function __construct() |
||
32 | { |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Determines whether the query builder uses a HAVING clause. |
||
37 | */ |
||
38 | public static function hasHavingClause(QueryBuilder $queryBuilder): bool |
||
39 | { |
||
40 | return null !== $queryBuilder->getDQLPart('having'); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Determines whether the query builder has any root entity with foreign key identifier. |
||
45 | */ |
||
46 | public static function hasRootEntityWithForeignKeyIdentifier(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Determines whether the query builder has any composite identifier. |
||
64 | */ |
||
65 | public static function hasRootEntityWithCompositeIdentifier(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Determines whether the query builder has a limit on the maximum number of results. |
||
83 | */ |
||
84 | public static function hasMaxResults(QueryBuilder $queryBuilder): bool |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Determines whether the query builder has ORDER BY on a column from a fetch joined to-many association. |
||
91 | */ |
||
92 | public static function hasOrderByOnFetchJoinedToManyAssociation(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool |
||
93 | { |
||
94 | if ( |
||
95 | 0 === \count($selectParts = $queryBuilder->getDQLPart('select')) || |
||
96 | 0 === \count($queryBuilder->getDQLPart('join')) || |
||
97 | 0 === \count($orderByParts = $queryBuilder->getDQLPart('orderBy')) |
||
98 | ) { |
||
99 | return false; |
||
100 | } |
||
101 | |||
102 | $rootAliases = $queryBuilder->getRootAliases(); |
||
103 | |||
104 | $selectAliases = []; |
||
105 | |||
106 | foreach ($selectParts as $select) { |
||
107 | foreach ($select->getParts() as $part) { |
||
108 | [$alias] = explode('.', $part); |
||
109 | |||
110 | $selectAliases[] = $alias; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | $selectAliases = array_diff($selectAliases, $rootAliases); |
||
115 | if (0 === \count($selectAliases)) { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | $orderByAliases = []; |
||
120 | |||
121 | foreach ($orderByParts as $orderBy) { |
||
122 | foreach ($orderBy->getParts() as $part) { |
||
123 | if (false !== strpos($part, '.')) { |
||
124 | [$alias] = explode('.', $part); |
||
125 | |||
126 | $orderByAliases[] = $alias; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | $orderByAliases = array_diff($orderByAliases, $rootAliases); |
||
132 | if (0 === \count($orderByAliases)) { |
||
133 | return false; |
||
134 | } |
||
135 | |||
136 | foreach ($orderByAliases as $orderByAlias) { |
||
137 | $inToManyContext = false; |
||
138 | |||
139 | foreach (QueryBuilderHelper::traverseJoins($orderByAlias, $queryBuilder, $managerRegistry) as $alias => [$metadata, $association]) { |
||
140 | if ($inToManyContext && \in_array($alias, $selectAliases, true)) { |
||
141 | return true; |
||
142 | } |
||
143 | |||
144 | if (null !== $association && $metadata->isCollectionValuedAssociation($association)) { |
||
145 | $inToManyContext = true; |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return false; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Determines whether the query builder has ORDER BY on a column from a fetch joined to-many association. |
||
155 | * |
||
156 | * @deprecated |
||
157 | */ |
||
158 | public static function hasOrderByOnToManyJoin(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool |
||
159 | { |
||
160 | @trigger_error(sprintf('The use of "%s::hasOrderByOnToManyJoin()" is deprecated since 2.4 and will be removed in 3.0. Use "%1$s::hasOrderByOnFetchJoinedToManyAssociation()" instead.', __CLASS__), E_USER_DEPRECATED); |
||
161 | |||
162 | return self::hasOrderByOnFetchJoinedToManyAssociation($queryBuilder, $managerRegistry); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Determines whether the query builder already has a left join. |
||
167 | */ |
||
168 | public static function hasLeftJoin(QueryBuilder $queryBuilder): bool |
||
169 | { |
||
170 | foreach ($queryBuilder->getDQLPart('join') as $joins) { |
||
171 | foreach ($joins as $join) { |
||
172 | if (Join::LEFT_JOIN === $join->getJoinType()) { |
||
173 | return true; |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | return false; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Determines whether the query builder has a fetch joined to-many association. |
||
183 | */ |
||
184 | public static function hasFetchJoinedToManyAssociation(QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): bool |
||
219 | } |
||
220 | } |
||
221 |