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 ScopeInterpreter 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 ScopeInterpreter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class ScopeInterpreter extends ExpressionInterpreter implements IScopeInterpreter |
||
20 | { |
||
21 | /** |
||
22 | * @var IScopeInterpretation |
||
23 | */ |
||
24 | protected $interpretation; |
||
25 | |||
26 | /** |
||
27 | * Because the method expression are evaluated top-down, |
||
28 | * Query segments are interpreted in reverse order, so they are stored as |
||
29 | * callbacks and called in reverse order. |
||
30 | * |
||
31 | * @var callable[] |
||
32 | */ |
||
33 | protected $segmentCallbacks = []; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $segmentCounter = 0; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $segmentId = ''; |
||
44 | |||
45 | public function __construct( |
||
53 | |||
54 | public function getInterpretation() |
||
58 | |||
59 | public function interpretScope(O\Expression $expression) |
||
68 | |||
69 | final protected function visit(O\Expression $expression) |
||
70 | { |
||
71 | if ($expression instanceof O\ValueExpression) { |
||
72 | $queryable = $expression->getValue(); |
||
73 | if (!($queryable instanceof IQueryable)) { |
||
74 | throw new PinqException('Invalid scope expression: must originate from %s, %s given', |
||
75 | IQueryable::IQUERYABLE_TYPE, |
||
76 | Utilities::getTypeOrClass($queryable)); |
||
77 | } |
||
78 | |||
79 | if ($queryable->isSource()) { |
||
80 | $this->addSegment( |
||
81 | function () use ($queryable) { |
||
82 | $this->interpretation->interpretScopeSource($queryable); |
||
83 | } |
||
84 | ); |
||
85 | |||
86 | return; |
||
87 | } |
||
88 | |||
89 | $expression = $queryable->getExpression(); |
||
90 | } |
||
91 | |||
92 | $methodName = $this->getMethodName($expression); |
||
93 | $this->segmentCounter++; |
||
94 | $this->segmentId = "{$this->segmentCounter}-{$methodName}"; |
||
95 | if (!method_exists($this, "visit$methodName")) { |
||
96 | throw new PinqException('Cannot interpret query scope with method call \'%s\'', $methodName); |
||
97 | } |
||
98 | $this->{"visit$methodName"}($expression); |
||
99 | } |
||
100 | |||
101 | final protected function addSegment(callable $segmentCallback) |
||
108 | |||
109 | View Code Duplication | final protected function visitWhere(O\MethodCallExpression $expression) |
|
122 | |||
123 | final protected function getSegmentId($parameter = null) |
||
127 | |||
128 | final protected function visitOrderBy(O\MethodCallExpression $expression) |
||
132 | |||
133 | final protected function visitOrderings(O\MethodCallExpression $expression) |
||
161 | |||
162 | final protected function visitOrdering($count, O\MethodCallExpression $expression) |
||
181 | |||
182 | final protected function visitOrderByAscending(O\MethodCallExpression $expression) |
||
186 | |||
187 | final protected function visitOrderByDescending(O\MethodCallExpression $expression) |
||
191 | |||
192 | final protected function visitThenBy(O\MethodCallExpression $expression) |
||
196 | |||
197 | final protected function visitThenByAscending(O\MethodCallExpression $expression) |
||
201 | |||
202 | final protected function visitThenByDescending(O\MethodCallExpression $expression) |
||
206 | |||
207 | final protected function visitSlice(O\MethodCallExpression $expression) |
||
213 | |||
214 | final protected function addSlice($start, $amount) |
||
228 | |||
229 | final protected function visitSkip(O\MethodCallExpression $expression) |
||
235 | |||
236 | final protected function visitTake(O\MethodCallExpression $expression) |
||
242 | |||
243 | View Code Duplication | final protected function visitIndexBy(O\MethodCallExpression $expression) |
|
256 | |||
257 | final protected function visitKeys(O\MethodCallExpression $expression) |
||
267 | |||
268 | final protected function visitReindex(O\MethodCallExpression $expression) |
||
278 | |||
279 | View Code Duplication | final protected function visitGroupBy(O\MethodCallExpression $expression) |
|
292 | |||
293 | final protected function visitTo(O\MethodCallExpression $expression) |
||
315 | |||
316 | public function buildJoinOptionsInterpreter($segmentId) |
||
325 | |||
326 | public function buildSourceInterpreter($segmentId) |
||
338 | |||
339 | View Code Duplication | final protected function visitSelect(O\MethodCallExpression $expression) |
|
351 | |||
352 | View Code Duplication | final protected function visitSelectMany(O\MethodCallExpression $expression) |
|
364 | |||
365 | final protected function visitUnique(O\MethodCallExpression $expression) |
||
374 | |||
375 | final protected function visitAppend(O\MethodCallExpression $expression) |
||
379 | |||
380 | final protected function visitOperation($operationType, O\MethodCallExpression $expression) |
||
397 | |||
398 | final protected function visitWhereIn(O\MethodCallExpression $expression) |
||
402 | |||
403 | final protected function visitExcept(O\MethodCallExpression $expression) |
||
407 | |||
408 | final protected function visitUnion(O\MethodCallExpression $expression) |
||
412 | |||
413 | final protected function visitIntersect(O\MethodCallExpression $expression) |
||
417 | |||
418 | final protected function visitDifference(O\MethodCallExpression $expression) |
||
422 | } |
||
423 |
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.