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:
1 | <?php |
||
36 | class DateAddFunction extends FunctionNode |
||
37 | { |
||
38 | public $firstDateExpression = null; |
||
39 | public $intervalExpression = null; |
||
40 | public $unit = null; |
||
41 | |||
42 | /** |
||
43 | * @override |
||
44 | * @inheritdoc |
||
45 | */ |
||
46 | 7 | View Code Duplication | public function getSql(SqlWalker $sqlWalker) |
47 | { |
||
48 | 7 | switch (strtolower($this->unit->value)) { |
|
49 | 7 | case 'second': |
|
50 | 1 | return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression( |
|
51 | 1 | $this->firstDateExpression->dispatch($sqlWalker), |
|
52 | 1 | $this->intervalExpression->dispatch($sqlWalker) |
|
53 | ); |
||
54 | 6 | case 'minute': |
|
55 | 1 | return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMinutesExpression( |
|
56 | 1 | $this->firstDateExpression->dispatch($sqlWalker), |
|
57 | 1 | $this->intervalExpression->dispatch($sqlWalker) |
|
58 | ); |
||
59 | 5 | case 'hour': |
|
60 | 1 | return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression( |
|
61 | 1 | $this->firstDateExpression->dispatch($sqlWalker), |
|
62 | 1 | $this->intervalExpression->dispatch($sqlWalker) |
|
63 | ); |
||
64 | 4 | case 'day': |
|
65 | 1 | return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression( |
|
66 | 1 | $this->firstDateExpression->dispatch($sqlWalker), |
|
67 | 1 | $this->intervalExpression->dispatch($sqlWalker) |
|
68 | ); |
||
69 | 3 | case 'week': |
|
70 | 1 | return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddWeeksExpression( |
|
71 | 1 | $this->firstDateExpression->dispatch($sqlWalker), |
|
72 | 1 | $this->intervalExpression->dispatch($sqlWalker) |
|
73 | ); |
||
74 | 2 | case 'month': |
|
75 | 1 | return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression( |
|
76 | 1 | $this->firstDateExpression->dispatch($sqlWalker), |
|
77 | 1 | $this->intervalExpression->dispatch($sqlWalker) |
|
78 | ); |
||
79 | 1 | case 'year': |
|
80 | 1 | return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddYearsExpression( |
|
81 | 1 | $this->firstDateExpression->dispatch($sqlWalker), |
|
82 | 1 | $this->intervalExpression->dispatch($sqlWalker) |
|
83 | ); |
||
84 | |||
85 | default: |
||
86 | throw QueryException::semanticalError( |
||
87 | 'DATE_ADD() only supports units of type second, minute, hour, day, week, month and year.' |
||
88 | ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @override |
||
94 | * @inheritdoc |
||
95 | */ |
||
96 | 13 | public function parse(Parser $parser) |
|
109 | } |
||
110 |