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 Criteria 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 Criteria, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Criteria extends CriteriaElement |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $prefix; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $function; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $column; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | public $operator; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var mixed |
||
| 53 | */ |
||
| 54 | public $value; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor |
||
| 58 | * |
||
| 59 | * @param string $column column criteria applies to |
||
| 60 | * @param string $value value to compare to column |
||
| 61 | * @param string $operator operator to apply to column |
||
| 62 | * @param string $prefix prefix to append to column |
||
| 63 | * @param string $function sprintf string taking one string argument applied to column |
||
| 64 | */ |
||
| 65 | 58 | public function __construct($column, $value = '', $operator = '=', $prefix = '', $function = '') |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Make a sql condition string |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 11 | public function render() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Generate an LDAP filter from criteria |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | * @author Nathan Dial [email protected], improved by Pierre-Eric MENUET [email protected] |
||
| 111 | */ |
||
| 112 | 2 | public function renderLdap() |
|
| 113 | { |
||
| 114 | 2 | $clause = ''; |
|
| 115 | 2 | if ($this->operator === '>') { |
|
| 116 | $this->operator = '>='; |
||
| 117 | } |
||
| 118 | 2 | if ($this->operator === '<') { |
|
| 119 | $this->operator = '<='; |
||
| 120 | } |
||
| 121 | |||
| 122 | 2 | if ($this->operator === '!=' || $this->operator === '<>') { |
|
| 123 | $operator = '='; |
||
| 124 | $clause = "(!(" . $this->column . $operator . $this->value . "))"; |
||
| 125 | } else { |
||
| 126 | 2 | if ($this->operator === 'IN') { |
|
| 127 | $newvalue = str_replace(array('(', ')'), '', $this->value); |
||
| 128 | $tab = explode(',', $newvalue); |
||
| 129 | foreach ($tab as $uid) { |
||
| 130 | $clause .= "({$this->column}={$uid})"; |
||
| 131 | } |
||
| 132 | $clause = '(|' . $clause . ')'; |
||
| 133 | } else { |
||
| 134 | 2 | $clause = "(" . $this->column . ' ' . $this->operator . ' ' . $this->value . ")"; |
|
| 135 | } |
||
| 136 | } |
||
| 137 | 2 | return $clause; |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Make a SQL "WHERE" clause |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | 3 | public function renderWhere() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Render criteria as Doctrine QueryBuilder instructions |
||
| 153 | * |
||
| 154 | * @param QueryBuilder $qb query builder instance |
||
| 155 | * @param string $whereMode how does this fit in the passed in QueryBuilder? |
||
| 156 | * '' = as where,'and'= as andWhere, 'or' = as orWhere |
||
| 157 | * |
||
| 158 | * @return QueryBuilder query builder instance |
||
| 159 | */ |
||
| 160 | 26 | public function renderQb(QueryBuilder $qb = null, $whereMode = '') |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Build an expression to be included in a Doctrine QueryBuilder instance. |
||
| 198 | * |
||
| 199 | * This method will build an expression, adding any parameters to the query, |
||
| 200 | * but the caller is responsible for adding the expression to the query, for |
||
| 201 | * example as where() parameter. This allows the caller to handle all context, |
||
| 202 | * such as parenthetical groupings. |
||
| 203 | * |
||
| 204 | * @param QueryBuilder $qb query builder instance |
||
| 205 | * |
||
| 206 | * @return string expression |
||
| 207 | */ |
||
| 208 | 41 | public function buildExpressionQb(QueryBuilder $qb) |
|
| 290 | } |
||
| 291 |