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:
Complex classes like DoctrineDatabase 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 DoctrineDatabase, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class DoctrineDatabase extends Gateway |
||
31 | { |
||
32 | /** |
||
33 | * 2^30, since PHP_INT_MAX can cause overflows in DB systems, if PHP is run |
||
34 | * on 64 bit systems. |
||
35 | */ |
||
36 | const MAX_LIMIT = 1073741824; |
||
37 | |||
38 | /** |
||
39 | * Database handler. |
||
40 | * |
||
41 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
42 | */ |
||
43 | protected $handler; |
||
44 | |||
45 | /** @var \Doctrine\DBAL\Connection */ |
||
46 | protected $connection; |
||
47 | |||
48 | /** |
||
49 | * Language mask generator. |
||
50 | * |
||
51 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator |
||
52 | */ |
||
53 | protected $languageMaskGenerator; |
||
54 | |||
55 | /** |
||
56 | * Construct from database handler. |
||
57 | * |
||
58 | * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $handler |
||
59 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $languageMaskGenerator |
||
60 | */ |
||
61 | public function __construct(DatabaseHandler $handler, MaskGenerator $languageMaskGenerator) |
||
62 | { |
||
63 | $this->handler = $handler; |
||
64 | $this->connection = $handler->getConnection(); |
||
65 | $this->languageMaskGenerator = $languageMaskGenerator; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | View Code Duplication | public function getBasicNodeData($nodeId, array $translations = null, bool $useAlwaysAvailable = true) |
|
72 | { |
||
73 | $q = $this->createNodeQueryBuilder($translations, $useAlwaysAvailable); |
||
74 | $q->andWhere( |
||
75 | $q->expr()->eq('t.node_id', $q->createNamedParameter($nodeId, PDO::PARAM_INT)) |
||
76 | ); |
||
77 | |||
78 | if ($row = $q->execute()->fetch(FetchMode::ASSOCIATIVE)) { |
||
79 | return $row; |
||
80 | } |
||
81 | |||
82 | throw new NotFound('location', $nodeId); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function getNodeDataList(array $locationIds, array $translations = null, bool $useAlwaysAvailable = true): iterable |
||
89 | { |
||
90 | $q = $this->createNodeQueryBuilder($translations, $useAlwaysAvailable); |
||
91 | $q->andWhere( |
||
92 | $q->expr()->in( |
||
93 | 't.node_id', |
||
94 | $q->createNamedParameter($locationIds, Connection::PARAM_INT_ARRAY) |
||
95 | ) |
||
96 | ); |
||
97 | |||
98 | return $q->execute()->fetchAll(FetchMode::ASSOCIATIVE); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public function getBasicNodeDataByRemoteId($remoteId, array $translations = null, bool $useAlwaysAvailable = true) |
|
105 | { |
||
106 | $q = $this->createNodeQueryBuilder($translations, $useAlwaysAvailable); |
||
107 | $q->andWhere( |
||
108 | $q->expr()->eq('t.remote_id', $q->createNamedParameter($remoteId, PDO::PARAM_STR)) |
||
109 | ); |
||
110 | |||
111 | if ($row = $q->execute()->fetch(FetchMode::ASSOCIATIVE)) { |
||
112 | return $row; |
||
113 | } |
||
114 | |||
115 | throw new NotFound('location', $remoteId); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Loads data for all Locations for $contentId, optionally only in the |
||
120 | * subtree starting at $rootLocationId. |
||
121 | * |
||
122 | * @param int $contentId |
||
123 | * @param int $rootLocationId |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | public function loadLocationDataByContent($contentId, $rootLocationId = null) |
||
128 | { |
||
129 | $query = $this->handler->createSelectQuery(); |
||
130 | $query |
||
131 | ->select('*') |
||
132 | ->from($this->handler->quoteTable('ezcontentobject_tree')) |
||
133 | ->where( |
||
134 | $query->expr->eq( |
||
135 | $this->handler->quoteColumn('contentobject_id'), |
||
136 | $query->bindValue($contentId) |
||
137 | ) |
||
138 | ); |
||
139 | |||
140 | if ($rootLocationId !== null) { |
||
141 | $this->applySubtreeLimitation($query, $rootLocationId); |
||
142 | } |
||
143 | |||
144 | $statement = $query->prepare(); |
||
145 | $statement->execute(); |
||
146 | |||
147 | return $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway::loadParentLocationsDataForDraftContent |
||
152 | */ |
||
153 | public function loadParentLocationsDataForDraftContent($contentId, $drafts = null) |
||
154 | { |
||
155 | /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */ |
||
156 | $query = $this->handler->createSelectQuery(); |
||
157 | $query->selectDistinct( |
||
158 | 'ezcontentobject_tree.*' |
||
159 | )->from( |
||
160 | $this->handler->quoteTable('ezcontentobject_tree') |
||
161 | )->innerJoin( |
||
162 | $this->handler->quoteTable('eznode_assignment'), |
||
163 | $query->expr->lAnd( |
||
164 | $query->expr->eq( |
||
165 | $this->handler->quoteColumn('node_id', 'ezcontentobject_tree'), |
||
166 | $this->handler->quoteColumn('parent_node', 'eznode_assignment') |
||
167 | ), |
||
168 | $query->expr->eq( |
||
169 | $this->handler->quoteColumn('contentobject_id', 'eznode_assignment'), |
||
170 | $query->bindValue($contentId, null, \PDO::PARAM_INT) |
||
171 | ), |
||
172 | $query->expr->eq( |
||
173 | $this->handler->quoteColumn('op_code', 'eznode_assignment'), |
||
174 | $query->bindValue(self::NODE_ASSIGNMENT_OP_CODE_CREATE, null, \PDO::PARAM_INT) |
||
175 | ) |
||
176 | ) |
||
177 | )->innerJoin( |
||
178 | $this->handler->quoteTable('ezcontentobject'), |
||
179 | $query->expr->lAnd( |
||
180 | $query->expr->lOr( |
||
181 | $query->expr->eq( |
||
182 | $this->handler->quoteColumn('contentobject_id', 'eznode_assignment'), |
||
183 | $this->handler->quoteColumn('id', 'ezcontentobject') |
||
184 | ) |
||
185 | ), |
||
186 | $query->expr->eq( |
||
187 | $this->handler->quoteColumn('status', 'ezcontentobject'), |
||
188 | $query->bindValue(ContentInfo::STATUS_DRAFT, null, \PDO::PARAM_INT) |
||
189 | ) |
||
190 | ) |
||
191 | ); |
||
192 | |||
193 | $statement = $query->prepare(); |
||
194 | $statement->execute(); |
||
195 | |||
196 | return $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Find all content in the given subtree. |
||
201 | * |
||
202 | * @param mixed $sourceId |
||
203 | * @param bool $onlyIds |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getSubtreeContent($sourceId, $onlyIds = false) |
||
208 | { |
||
209 | $query = $this->handler->createSelectQuery(); |
||
210 | $query->select($onlyIds ? 'node_id, contentobject_id, depth' : '*')->from( |
||
211 | $this->handler->quoteTable('ezcontentobject_tree') |
||
212 | ); |
||
213 | $this->applySubtreeLimitation($query, $sourceId); |
||
214 | $query->orderBy( |
||
215 | $this->handler->quoteColumn('depth', 'ezcontentobject_tree') |
||
216 | )->orderBy( |
||
217 | $this->handler->quoteColumn('node_id', 'ezcontentobject_tree') |
||
218 | ); |
||
219 | $statement = $query->prepare(); |
||
220 | $statement->execute(); |
||
221 | |||
222 | $results = $statement->fetchAll($onlyIds ? (PDO::FETCH_COLUMN | PDO::FETCH_GROUP) : PDO::FETCH_ASSOC); |
||
223 | // array_map() is used to to map all elements stored as $results[$i][0] to $results[$i] |
||
224 | return $onlyIds ? array_map('reset', $results) : $results; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Limits the given $query to the subtree starting at $rootLocationId. |
||
229 | * |
||
230 | * @param \eZ\Publish\Core\Persistence\Database\Query $query |
||
231 | * @param string $rootLocationId |
||
232 | */ |
||
233 | protected function applySubtreeLimitation(DatabaseQuery $query, $rootLocationId) |
||
234 | { |
||
235 | $query->where( |
||
|
|||
236 | $query->expr->like( |
||
237 | $this->handler->quoteColumn('path_string', 'ezcontentobject_tree'), |
||
238 | $query->bindValue('%/' . $rootLocationId . '/%') |
||
239 | ) |
||
240 | ); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Returns data for the first level children of the location identified by given $locationId. |
||
245 | * |
||
246 | * @param mixed $locationId |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | public function getChildren($locationId) |
||
251 | { |
||
252 | $query = $this->handler->createSelectQuery(); |
||
253 | $query->select('*')->from( |
||
254 | $this->handler->quoteTable('ezcontentobject_tree') |
||
255 | )->where( |
||
256 | $query->expr->eq( |
||
257 | $this->handler->quoteColumn('parent_node_id', 'ezcontentobject_tree'), |
||
258 | $query->bindValue($locationId, null, \PDO::PARAM_INT) |
||
259 | ) |
||
260 | ); |
||
261 | $statement = $query->prepare(); |
||
262 | $statement->execute(); |
||
263 | |||
264 | return $statement->fetchAll(\PDO::FETCH_ASSOC); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Update path strings to move nodes in the ezcontentobject_tree table. |
||
269 | * |
||
270 | * This query can likely be optimized to use some more advanced string |
||
271 | * operations, which then depend on the respective database. |
||
272 | * |
||
273 | * @todo optimize |
||
274 | * |
||
275 | * @param array $sourceNodeData |
||
276 | * @param array $destinationNodeData |
||
277 | */ |
||
278 | public function moveSubtreeNodes(array $sourceNodeData, array $destinationNodeData) |
||
279 | { |
||
280 | $fromPathString = $sourceNodeData['path_string']; |
||
281 | |||
282 | /** @var $query \eZ\Publish\Core\Persistence\Database\SelectQuery */ |
||
283 | $query = $this->handler->createSelectQuery(); |
||
284 | $query |
||
285 | ->select( |
||
286 | $this->handler->quoteColumn('node_id'), |
||
287 | $this->handler->quoteColumn('parent_node_id'), |
||
288 | $this->handler->quoteColumn('path_string'), |
||
289 | $this->handler->quoteColumn('path_identification_string'), |
||
290 | $this->handler->quoteColumn('is_hidden') |
||
291 | ) |
||
292 | ->from($this->handler->quoteTable('ezcontentobject_tree')) |
||
293 | ->where( |
||
294 | $query->expr->like( |
||
295 | $this->handler->quoteColumn('path_string'), |
||
296 | $query->bindValue($fromPathString . '%') |
||
297 | ) |
||
298 | ); |
||
299 | $statement = $query->prepare(); |
||
300 | $statement->execute(); |
||
301 | |||
302 | $rows = $statement->fetchAll(); |
||
303 | $oldParentPathString = implode('/', array_slice(explode('/', $fromPathString), 0, -2)) . '/'; |
||
304 | $oldParentPathIdentificationString = implode( |
||
305 | '/', |
||
306 | array_slice(explode('/', $sourceNodeData['path_identification_string']), 0, -1) |
||
307 | ); |
||
308 | |||
309 | foreach ($rows as $row) { |
||
310 | // Prefixing ensures correct replacement when old parent is root node |
||
311 | $newPathString = str_replace( |
||
312 | 'prefix' . $oldParentPathString, |
||
313 | $destinationNodeData['path_string'], |
||
314 | 'prefix' . $row['path_string'] |
||
315 | ); |
||
316 | $replace = rtrim($destinationNodeData['path_identification_string'], '/'); |
||
317 | if (empty($oldParentPathIdentificationString)) { |
||
318 | $replace .= '/'; |
||
319 | } |
||
320 | $newPathIdentificationString = str_replace( |
||
321 | 'prefix' . $oldParentPathIdentificationString, |
||
322 | $replace, |
||
323 | 'prefix' . $row['path_identification_string'] |
||
324 | ); |
||
325 | $newParentId = $row['parent_node_id']; |
||
326 | if ($row['path_string'] === $fromPathString) { |
||
327 | $newParentId = (int)implode('', array_slice(explode('/', $newPathString), -3, 1)); |
||
328 | } |
||
329 | |||
330 | /** @var $query \eZ\Publish\Core\Persistence\Database\UpdateQuery */ |
||
331 | $query = $this->handler->createUpdateQuery(); |
||
332 | $query |
||
333 | ->update($this->handler->quoteTable('ezcontentobject_tree')) |
||
334 | ->set( |
||
335 | $this->handler->quoteColumn('path_string'), |
||
336 | $query->bindValue($newPathString) |
||
337 | ) |
||
338 | ->set( |
||
339 | $this->handler->quoteColumn('path_identification_string'), |
||
340 | $query->bindValue($newPathIdentificationString) |
||
341 | ) |
||
342 | ->set( |
||
343 | $this->handler->quoteColumn('depth'), |
||
344 | $query->bindValue(substr_count($newPathString, '/') - 2) |
||
345 | ) |
||
346 | ->set( |
||
347 | $this->handler->quoteColumn('parent_node_id'), |
||
348 | $query->bindValue($newParentId) |
||
349 | ); |
||
350 | |||
351 | if ($destinationNodeData['is_hidden'] || $destinationNodeData['is_invisible']) { |
||
352 | // CASE 1: Mark whole tree as invisible if destination is invisible and/or hidden |
||
353 | $query->set( |
||
354 | $this->handler->quoteColumn('is_invisible'), |
||
355 | $query->bindValue(1) |
||
356 | ); |
||
357 | } elseif (!$sourceNodeData['is_hidden'] && $sourceNodeData['is_invisible']) { |
||
358 | // CASE 2: source is only invisible, we will need to re-calculate whole moved tree visibility |
||
359 | $query->set( |
||
360 | $this->handler->quoteColumn('is_invisible'), |
||
361 | $query->bindValue($this->isHiddenByParent($newPathString, $rows) ? 1 : 0) |
||
362 | ); |
||
363 | } else { |
||
364 | // CASE 3: keep invisible flags as is (source is either hidden or not hidden/invisible at all) |
||
365 | } |
||
366 | |||
367 | $query->where( |
||
368 | $query->expr->eq( |
||
369 | $this->handler->quoteColumn('node_id'), |
||
370 | $query->bindValue($row['node_id']) |
||
371 | ) |
||
372 | ); |
||
373 | $query->prepare()->execute(); |
||
374 | } |
||
375 | } |
||
376 | |||
377 | private function isHiddenByParent($pathString, array $rows) |
||
389 | |||
390 | /** |
||
391 | * Updated subtree modification time for all nodes on path. |
||
392 | * |
||
393 | * @param string $pathString |
||
394 | * @param int|null $timestamp |
||
395 | */ |
||
396 | public function updateSubtreeModificationTime($pathString, $timestamp = null) |
||
416 | |||
417 | /** |
||
418 | * Sets a location to be hidden, and it self + all children to invisible. |
||
419 | * |
||
420 | * @param string $pathString |
||
421 | */ |
||
422 | public function hideSubtree($pathString) |
||
427 | |||
428 | /** |
||
429 | * @param string $pathString |
||
430 | **/ |
||
431 | public function setNodeWithChildrenInvisible(string $pathString): void |
||
453 | |||
454 | /** |
||
455 | * @param string $pathString |
||
456 | **/ |
||
457 | public function setNodeHidden(string $pathString): void |
||
461 | |||
462 | /** |
||
463 | * @param string $pathString |
||
464 | * @param bool $isHidden |
||
465 | */ |
||
466 | private function setNodeHiddenStatus(string $pathString, bool $isHidden): void |
||
467 | { |
||
468 | $query = $this->handler->createUpdateQuery(); |
||
469 | $query |
||
470 | ->update($this->handler->quoteTable('ezcontentobject_tree')) |
||
471 | ->set( |
||
472 | $this->handler->quoteColumn('is_hidden'), |
||
473 | $query->bindValue((int) $isHidden) |
||
474 | ) |
||
475 | ->where( |
||
476 | $query->expr->eq( |
||
477 | $this->handler->quoteColumn('path_string'), |
||
478 | $query->bindValue($pathString) |
||
479 | ) |
||
480 | ); |
||
481 | |||
482 | $query->prepare()->execute(); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Sets a location to be unhidden, and self + children to visible unless a parent is hiding the tree. |
||
487 | * If not make sure only children down to first hidden node is marked visible. |
||
488 | * |
||
489 | * @param string $pathString |
||
490 | */ |
||
491 | public function unHideSubtree($pathString) |
||
496 | |||
497 | /** |
||
498 | * Sets a location + children to visible unless a parent is hiding the tree. |
||
499 | * |
||
500 | * @param string $pathString |
||
501 | **/ |
||
502 | public function setNodeWithChildrenVisible(string $pathString): void |
||
606 | |||
607 | /** |
||
608 | * Sets location to be unhidden. |
||
609 | * |
||
610 | * @param string $pathString |
||
611 | **/ |
||
612 | public function setNodeUnhidden(string $pathString): void |
||
616 | |||
617 | /** |
||
618 | * Swaps the content object being pointed to by a location object. |
||
619 | * |
||
620 | * Make the location identified by $locationId1 refer to the Content |
||
621 | * referred to by $locationId2 and vice versa. |
||
622 | * |
||
623 | * @param int $locationId1 |
||
624 | * @param int $locationId2 |
||
625 | * |
||
626 | * @return bool |
||
627 | */ |
||
628 | public function swap(int $locationId1, int $locationId2): bool |
||
705 | |||
706 | /** |
||
707 | * Creates a new location in given $parentNode. |
||
708 | * |
||
709 | * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct |
||
710 | * @param array $parentNode |
||
711 | * |
||
712 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
713 | */ |
||
714 | public function create(CreateStruct $createStruct, array $parentNode) |
||
795 | |||
796 | /** |
||
797 | * Create an entry in the node assignment table. |
||
798 | * |
||
799 | * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct |
||
800 | * @param mixed $parentNodeId |
||
801 | * @param int $type |
||
802 | */ |
||
803 | public function createNodeAssignment(CreateStruct $createStruct, $parentNodeId, $type = self::NODE_ASSIGNMENT_OP_CODE_CREATE_NOP) |
||
856 | |||
857 | /** |
||
858 | * Deletes node assignment for given $contentId and $versionNo. |
||
859 | * |
||
860 | * If $versionNo is not passed all node assignments for given $contentId are deleted |
||
861 | * |
||
862 | * @param int $contentId |
||
863 | * @param int|null $versionNo |
||
864 | */ |
||
865 | public function deleteNodeAssignment($contentId, $versionNo = null) |
||
886 | |||
887 | /** |
||
888 | * Update node assignment table. |
||
889 | * |
||
890 | * @param int $contentObjectId |
||
891 | * @param int $oldParent |
||
892 | * @param int $newParent |
||
893 | * @param int $opcode |
||
894 | */ |
||
895 | public function updateNodeAssignment($contentObjectId, $oldParent, $newParent, $opcode) |
||
922 | |||
923 | /** |
||
924 | * Create locations from node assignments. |
||
925 | * |
||
926 | * Convert existing node assignments into real locations. |
||
927 | * |
||
928 | * @param mixed $contentId |
||
929 | * @param mixed $versionNo |
||
930 | */ |
||
931 | public function createLocationsFromNodeAssignments($contentId, $versionNo) |
||
993 | |||
994 | /** |
||
995 | * Updates all Locations of content identified with $contentId with $versionNo. |
||
996 | * |
||
997 | * @param mixed $contentId |
||
998 | * @param mixed $versionNo |
||
999 | */ |
||
1000 | View Code Duplication | public function updateLocationsContentVersionNo($contentId, $versionNo) |
|
1016 | |||
1017 | /** |
||
1018 | * Searches for the main nodeId of $contentId in $versionId. |
||
1019 | * |
||
1020 | * @param int $contentId |
||
1021 | * |
||
1022 | * @return int|bool |
||
1023 | */ |
||
1024 | private function getMainNodeId($contentId) |
||
1052 | |||
1053 | /** |
||
1054 | * Updates an existing location. |
||
1055 | * |
||
1056 | * Will not throw anything if location id is invalid or no entries are affected. |
||
1057 | * |
||
1058 | * @param \eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct $location |
||
1059 | * @param int $locationId |
||
1060 | */ |
||
1061 | public function update(UpdateStruct $location, $locationId) |
||
1099 | |||
1100 | /** |
||
1101 | * Updates path identification string for given $locationId. |
||
1102 | * |
||
1103 | * @param mixed $locationId |
||
1104 | * @param mixed $parentLocationId |
||
1105 | * @param string $text |
||
1106 | */ |
||
1107 | public function updatePathIdentificationString($locationId, $parentLocationId, $text) |
||
1130 | |||
1131 | /** |
||
1132 | * Deletes ezcontentobject_tree row for given $locationId (node_id). |
||
1133 | * |
||
1134 | * @param mixed $locationId |
||
1135 | */ |
||
1136 | public function removeLocation($locationId) |
||
1149 | |||
1150 | /** |
||
1151 | * Returns id of the next in line node to be set as a new main node. |
||
1152 | * |
||
1153 | * This returns lowest node id for content identified by $contentId, and not of |
||
1154 | * the node identified by given $locationId (current main node). |
||
1155 | * Assumes that content has more than one location. |
||
1156 | * |
||
1157 | * @param mixed $contentId |
||
1158 | * @param mixed $locationId |
||
1159 | * |
||
1160 | * @return array |
||
1161 | */ |
||
1162 | public function getFallbackMainNodeData($contentId, $locationId) |
||
1188 | |||
1189 | /** |
||
1190 | * Sends a single location identified by given $locationId to the trash. |
||
1191 | * |
||
1192 | * The associated content object is left untouched. |
||
1193 | * |
||
1194 | * @param mixed $locationId |
||
1195 | * |
||
1196 | * @return bool |
||
1197 | */ |
||
1198 | public function trashLocation($locationId) |
||
1217 | |||
1218 | /** |
||
1219 | * Returns a trashed location to normal state. |
||
1220 | * |
||
1221 | * Recreates the originally trashed location in the new position. If no new |
||
1222 | * position has been specified, it will be tried to re-create the location |
||
1223 | * at the old position. If this is not possible ( because the old location |
||
1224 | * does not exist any more) and exception is thrown. |
||
1225 | * |
||
1226 | * @param mixed $locationId |
||
1227 | * @param mixed|null $newParentId |
||
1228 | * |
||
1229 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
1230 | */ |
||
1231 | public function untrashLocation($locationId, $newParentId = null) |
||
1257 | |||
1258 | /** |
||
1259 | * @param mixed $contentId |
||
1260 | * @param int $status |
||
1261 | */ |
||
1262 | protected function setContentStatus($contentId, $status) |
||
1279 | |||
1280 | /** |
||
1281 | * Loads trash data specified by location ID. |
||
1282 | * |
||
1283 | * @param mixed $locationId |
||
1284 | * |
||
1285 | * @return array |
||
1286 | */ |
||
1287 | public function loadTrashByLocation($locationId) |
||
1308 | |||
1309 | /** |
||
1310 | * List trashed items. |
||
1311 | * |
||
1312 | * @param int $offset |
||
1313 | * @param int $limit |
||
1314 | * @param array $sort |
||
1315 | * |
||
1316 | * @return array |
||
1317 | */ |
||
1318 | public function listTrashed($offset, $limit, array $sort = null) |
||
1368 | |||
1369 | View Code Duplication | public function countTrashed(): int |
|
1378 | |||
1379 | /** |
||
1380 | * Removes every entries in the trash. |
||
1381 | * Will NOT remove associated content objects nor attributes. |
||
1382 | * |
||
1383 | * Basically truncates ezcontentobject_trash table. |
||
1384 | */ |
||
1385 | public function cleanupTrash() |
||
1391 | |||
1392 | /** |
||
1393 | * Removes trashed element identified by $id from trash. |
||
1394 | * Will NOT remove associated content object nor attributes. |
||
1395 | * |
||
1396 | * @param int $id The trashed location Id |
||
1397 | */ |
||
1398 | public function removeElementFromTrash($id) |
||
1411 | |||
1412 | /** |
||
1413 | * Set section on all content objects in the subtree. |
||
1414 | * |
||
1415 | * @param string $pathString |
||
1416 | * @param int $sectionId |
||
1417 | * |
||
1418 | * @return bool |
||
1419 | */ |
||
1420 | public function setSectionForSubtree($pathString, $sectionId) |
||
1459 | |||
1460 | /** |
||
1461 | * Returns how many locations given content object identified by $contentId has. |
||
1462 | * |
||
1463 | * @param int $contentId |
||
1464 | * |
||
1465 | * @return int |
||
1466 | */ |
||
1467 | public function countLocationsByContentId($contentId) |
||
1487 | |||
1488 | /** |
||
1489 | * Changes main location of content identified by given $contentId to location identified by given $locationId. |
||
1490 | * |
||
1491 | * Updates ezcontentobject_tree table for the given $contentId and eznode_assignment table for the given |
||
1492 | * $contentId, $parentLocationId and $versionNo |
||
1493 | * |
||
1494 | * @param mixed $contentId |
||
1495 | * @param mixed $locationId |
||
1496 | * @param mixed $versionNo version number, needed to update eznode_assignment table |
||
1497 | * @param mixed $parentLocationId parent location of location identified by $locationId, needed to update |
||
1498 | * eznode_assignment table |
||
1499 | */ |
||
1500 | public function changeMainLocation($contentId, $locationId, $versionNo, $parentLocationId) |
||
1567 | |||
1568 | /** |
||
1569 | * Get the total number of all Locations, except the Root node. |
||
1570 | * |
||
1571 | * @see loadAllLocationsData |
||
1572 | * |
||
1573 | * @return int |
||
1574 | */ |
||
1575 | public function countAllLocations() |
||
1583 | |||
1584 | /** |
||
1585 | * Load data of every Location, except the Root node. |
||
1586 | * |
||
1587 | * @param int $offset Paginator offset |
||
1588 | * @param int $limit Paginator limit |
||
1589 | * |
||
1590 | * @return array |
||
1591 | */ |
||
1592 | public function loadAllLocationsData($offset, $limit) |
||
1621 | |||
1622 | /** |
||
1623 | * Get Query Builder for fetching data of all Locations except the Root node. |
||
1624 | * |
||
1625 | * @todo Align with createNodeQueryBuilder, removing the need for both(?) |
||
1626 | * |
||
1627 | * @param array $columns list of columns to fetch |
||
1628 | * |
||
1629 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
1630 | */ |
||
1631 | private function getAllLocationsQueryBuilder(array $columns) |
||
1642 | |||
1643 | /** |
||
1644 | * Create QueryBuilder for selecting node data. |
||
1645 | * |
||
1646 | * @param array|null $translations Filters on language mask of content if provided. |
||
1647 | * @param bool $useAlwaysAvailable Respect always available flag on content when filtering on $translations. |
||
1648 | * |
||
1649 | * @return \Doctrine\DBAL\Query\QueryBuilder |
||
1650 | */ |
||
1651 | private function createNodeQueryBuilder(array $translations = null, bool $useAlwaysAvailable = true): QueryBuilder |
||
1689 | } |
||
1690 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: