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