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 Builder 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 Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Builder |
||
29 | { |
||
30 | /** |
||
31 | * The DocumentManager instance for this query |
||
32 | * |
||
33 | * @var DocumentManager |
||
34 | */ |
||
35 | private $dm; |
||
36 | |||
37 | /** |
||
38 | * The ClassMetadata instance. |
||
39 | * |
||
40 | * @var ClassMetadata |
||
41 | */ |
||
42 | private $class; |
||
43 | |||
44 | /** @var string */ |
||
45 | private $hydrationClass; |
||
46 | |||
47 | /** |
||
48 | * The Collection instance. |
||
49 | * |
||
50 | * @var Collection |
||
51 | */ |
||
52 | private $collection; |
||
53 | |||
54 | /** @var Stage[] */ |
||
55 | private $stages = []; |
||
56 | |||
57 | /** |
||
58 | * Create a new aggregation builder. |
||
59 | */ |
||
60 | 255 | public function __construct(DocumentManager $dm, string $documentName) |
|
66 | |||
67 | /** |
||
68 | * Adds new fields to documents. $addFields outputs documents that contain all |
||
69 | * existing fields from the input documents and newly added fields. |
||
70 | * |
||
71 | * The $addFields stage is equivalent to a $project stage that explicitly specifies |
||
72 | * all existing fields in the input documents and adds the new fields. |
||
73 | * |
||
74 | * If the name of the new field is the same as an existing field name (including _id), |
||
75 | * $addFields overwrites the existing value of that field with the value of the |
||
76 | * specified expression. |
||
77 | * |
||
78 | * @see http://docs.mongodb.com/manual/reference/operator/aggregation/addFields/ |
||
79 | */ |
||
80 | 1 | public function addFields() : Stage\AddFields |
|
87 | |||
88 | /** |
||
89 | * Categorizes incoming documents into groups, called buckets, based on a |
||
90 | * specified expression and bucket boundaries. |
||
91 | * |
||
92 | * Each bucket is represented as a document in the output. The document for |
||
93 | * each bucket contains an _id field, whose value specifies the inclusive |
||
94 | * lower bound of the bucket and a count field that contains the number of |
||
95 | * documents in the bucket. The count field is included by default when the |
||
96 | * output is not specified. |
||
97 | * |
||
98 | * @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/ |
||
99 | */ |
||
100 | 2 | public function bucket() : Stage\Bucket |
|
107 | |||
108 | /** |
||
109 | * Categorizes incoming documents into a specific number of groups, called |
||
110 | * buckets, based on a specified expression. |
||
111 | * |
||
112 | * Bucket boundaries are automatically determined in an attempt to evenly |
||
113 | * distribute the documents into the specified number of buckets. Each |
||
114 | * bucket is represented as a document in the output. The document for each |
||
115 | * bucket contains an _id field, whose value specifies the inclusive lower |
||
116 | * bound and the exclusive upper bound for the bucket, and a count field |
||
117 | * that contains the number of documents in the bucket. The count field is |
||
118 | * included by default when the output is not specified. |
||
119 | * |
||
120 | * @see https://docs.mongodb.com/manual/reference/operator/aggregation/bucketAuto/ |
||
121 | */ |
||
122 | 2 | public function bucketAuto() : Stage\BucketAuto |
|
129 | |||
130 | /** |
||
131 | * Returns statistics regarding a collection or view. |
||
132 | * |
||
133 | * $collStats must be the first stage in an aggregation pipeline, or else |
||
134 | * the pipeline returns an error. |
||
135 | * |
||
136 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/collStats/ |
||
137 | */ |
||
138 | 1 | public function collStats() : Stage\CollStats |
|
145 | |||
146 | /** |
||
147 | * Returns a document that contains a count of the number of documents input |
||
148 | * to the stage. |
||
149 | * |
||
150 | * @see https://docs.mongodb.com/manual/reference/operator/aggregation/count/ |
||
151 | */ |
||
152 | 1 | public function count(string $fieldName) : Stage\Count |
|
159 | |||
160 | /** |
||
161 | * Executes the aggregation pipeline |
||
162 | */ |
||
163 | 17 | public function execute(array $options = []) : Iterator |
|
173 | |||
174 | 147 | public function expr() : Expr |
|
178 | |||
179 | /** |
||
180 | * Processes multiple aggregation pipelines within a single stage on the |
||
181 | * same set of input documents. |
||
182 | * |
||
183 | * Each sub-pipeline has its own field in the output document where its |
||
184 | * results are stored as an array of documents. |
||
185 | */ |
||
186 | 1 | public function facet() : Stage\Facet |
|
193 | |||
194 | /** |
||
195 | * Outputs documents in order of nearest to farthest from a specified point. |
||
196 | * |
||
197 | * A GeoJSON point may be provided as the first and only argument for |
||
198 | * 2dsphere queries. This single parameter may be a GeoJSON point object or |
||
199 | * an array corresponding to the point's JSON representation. If GeoJSON is |
||
200 | * used, the "spherical" option will default to true. |
||
201 | * |
||
202 | * You can only use this as the first stage of a pipeline. |
||
203 | * |
||
204 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/ |
||
205 | * |
||
206 | * @param float|array|Point $x |
||
207 | * @param float $y |
||
208 | */ |
||
209 | 4 | public function geoNear($x, $y = null) : Stage\GeoNear |
|
216 | |||
217 | /** |
||
218 | * Returns the assembled aggregation pipeline |
||
219 | * |
||
220 | * For pipelines where the first stage is a $geoNear stage, it will apply |
||
221 | * the document filters and discriminator queries to the query portion of |
||
222 | * the geoNear operation. For all other pipelines, it prepends a $match stage |
||
223 | * containing the required query. |
||
224 | */ |
||
225 | 56 | public function getPipeline() : array |
|
251 | |||
252 | /** |
||
253 | * Returns a certain stage from the pipeline |
||
254 | */ |
||
255 | 56 | public function getStage(int $index) : Stage |
|
263 | |||
264 | /** |
||
265 | * Performs a recursive search on a collection, with options for restricting |
||
266 | * the search by recursion depth and query filter. |
||
267 | * |
||
268 | * @see https://docs.mongodb.org/manual/reference/operator/aggregation/graphLookup/ |
||
269 | * |
||
270 | * @param string $from Target collection for the $graphLookup operation to |
||
271 | * search, recursively matching the connectFromField to the connectToField. |
||
272 | */ |
||
273 | 10 | View Code Duplication | public function graphLookup(string $from) : Stage\GraphLookup |
280 | |||
281 | /** |
||
282 | * Groups documents by some specified expression and outputs to the next |
||
283 | * stage a document for each distinct grouping. |
||
284 | * |
||
285 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/group/ |
||
286 | */ |
||
287 | 4 | public function group() : Stage\Group |
|
294 | |||
295 | /** |
||
296 | * Set which class to use when hydrating results as document class instances. |
||
297 | */ |
||
298 | 4 | public function hydrate(string $className) : self |
|
304 | |||
305 | /** |
||
306 | * Returns statistics regarding the use of each index for the collection. |
||
307 | * |
||
308 | * @see https://docs.mongodb.org/manual/reference/operator/aggregation/indexStats/ |
||
309 | */ |
||
310 | 2 | public function indexStats() : Stage\IndexStats |
|
317 | |||
318 | /** |
||
319 | * Limits the number of documents passed to the next stage in the pipeline. |
||
320 | * |
||
321 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/limit/ |
||
322 | */ |
||
323 | 2 | public function limit(int $limit) : Stage\Limit |
|
330 | |||
331 | /** |
||
332 | * Performs a left outer join to an unsharded collection in the same |
||
333 | * database to filter in documents from the “joined” collection for |
||
334 | * processing. |
||
335 | * |
||
336 | * @see https://docs.mongodb.org/manual/reference/operator/aggregation/lookup/ |
||
337 | */ |
||
338 | 14 | View Code Duplication | public function lookup(string $from) : Stage\Lookup |
345 | |||
346 | /** |
||
347 | * Filters the documents to pass only the documents that match the specified |
||
348 | * condition(s) to the next pipeline stage. |
||
349 | * |
||
350 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/match/ |
||
351 | */ |
||
352 | 8 | public function match() : Stage\Match |
|
359 | |||
360 | /** |
||
361 | * Returns a query expression to be used in match stages |
||
362 | */ |
||
363 | 60 | public function matchExpr() : QueryExpr |
|
370 | |||
371 | /** |
||
372 | * Takes the documents returned by the aggregation pipeline and writes them |
||
373 | * to a specified collection. This must be the last stage in the pipeline. |
||
374 | * |
||
375 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/out/ |
||
376 | */ |
||
377 | 6 | public function out(string $from) : Stage\Out |
|
384 | |||
385 | /** |
||
386 | * Passes along the documents with only the specified fields to the next |
||
387 | * stage in the pipeline. The specified fields can be existing fields from |
||
388 | * the input documents or newly computed fields. |
||
389 | * |
||
390 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/project/ |
||
391 | */ |
||
392 | 4 | public function project() : Stage\Project |
|
399 | |||
400 | /** |
||
401 | * Restricts the contents of the documents based on information stored in |
||
402 | * the documents themselves. |
||
403 | * |
||
404 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/redact/ |
||
405 | */ |
||
406 | 2 | public function redact() : Stage\Redact |
|
413 | |||
414 | /** |
||
415 | * Promotes a specified document to the top level and replaces all other |
||
416 | * fields. |
||
417 | * |
||
418 | * The operation replaces all existing fields in the input document, |
||
419 | * including the _id field. You can promote an existing embedded document to |
||
420 | * the top level, or create a new document for promotion. |
||
421 | * |
||
422 | * @param string|array|null $expression Optional. A replacement expression that |
||
423 | * resolves to a document. |
||
424 | */ |
||
425 | 6 | View Code Duplication | public function replaceRoot($expression = null) : Stage\ReplaceRoot |
432 | |||
433 | /** |
||
434 | * Randomly selects the specified number of documents from its input. |
||
435 | * |
||
436 | * @see https://docs.mongodb.org/manual/reference/operator/aggregation/sample/ |
||
437 | */ |
||
438 | 2 | public function sample(int $size) : Stage\Sample |
|
445 | |||
446 | /** |
||
447 | * Skips over the specified number of documents that pass into the stage and |
||
448 | * passes the remaining documents to the next stage in the pipeline. |
||
449 | * |
||
450 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/skip/ |
||
451 | */ |
||
452 | 2 | public function skip(int $skip) : Stage\Skip |
|
459 | |||
460 | /** |
||
461 | * Sorts all input documents and returns them to the pipeline in sorted |
||
462 | * order. |
||
463 | * |
||
464 | * If sorting by multiple fields, the first argument should be an array of |
||
465 | * field name (key) and order (value) pairs. |
||
466 | * |
||
467 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/sort/ |
||
468 | * |
||
469 | * @param array|string $fieldName Field name or array of field/order pairs |
||
470 | * @param int|string $order Field order (if one field is specified) |
||
471 | */ |
||
472 | 7 | public function sort($fieldName, $order = null) : Stage\Sort |
|
481 | |||
482 | /** |
||
483 | * Groups incoming documents based on the value of a specified expression, |
||
484 | * then computes the count of documents in each distinct group. |
||
485 | * |
||
486 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/sortByCount/ |
||
487 | */ |
||
488 | 3 | View Code Duplication | public function sortByCount(string $expression) : Stage\SortByCount |
495 | |||
496 | /** |
||
497 | * Deconstructs an array field from the input documents to output a document |
||
498 | * for each element. Each output document is the input document with the |
||
499 | * value of the array field replaced by the element. |
||
500 | * |
||
501 | * @see http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/ |
||
502 | */ |
||
503 | 7 | public function unwind(string $fieldName) : Stage\Unwind |
|
511 | |||
512 | /** |
||
513 | * Allows adding an arbitrary stage to the pipeline |
||
514 | * |
||
515 | * @return Stage The method returns the stage given as an argument |
||
516 | */ |
||
517 | 62 | public function addStage(Stage $stage) : Stage |
|
523 | |||
524 | /** |
||
525 | * Applies filters and discriminator queries to the pipeline |
||
526 | */ |
||
527 | 54 | private function applyFilters(array $query) : array |
|
536 | |||
537 | 11 | private function getDocumentPersister() : DocumentPersister |
|
541 | |||
542 | 17 | private function prepareIterator(Cursor $cursor) : Iterator |
|
555 | } |
||
556 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.