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 |
||
| 23 | class ArrayExpression implements ExpressionInterface |
||
| 24 | { |
||
| 25 | const PARAM_PREFIX = ':axp'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var null|string the type of the array elements. Defaults to `null` which means the type is |
||
| 29 | * not explicitly specified. This may result in an error if the type can not be inferred from the context. |
||
| 30 | * @see https://www.postgresql.org/docs/9.6/static/arrays.html |
||
| 31 | */ |
||
| 32 | protected $type; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array|QueryInterface|mixed the array content. Either represented as an array of values or a Query that |
||
| 36 | * returns these values. A single value will be considered as an array containing one element. |
||
| 37 | */ |
||
| 38 | protected $values; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * ArrayExpression constructor. |
||
| 42 | * |
||
| 43 | * @param array|QueryInterface|mixed $values the array content. Either represented as an array of values or a Query that |
||
| 44 | * returns these values. A single value will be considered as an array containing one element. |
||
| 45 | * @param string|null $type the type of the array elements. Defaults to `null` which means the type is |
||
| 46 | * not explicitly specified. This may result in an error if the type can not be inferred from the context. |
||
| 47 | */ |
||
| 48 | public function __construct($values, $type = null) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return string the typecast expression based on [[type]]. |
||
| 56 | */ |
||
| 57 | protected function getTypecast() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @inheritdoc |
||
| 73 | */ |
||
| 74 | public function buildUsing(QueryBuilder $queryBuilder, &$params = []) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Build an array expression from a subquery SQL. |
||
| 120 | * @param string $sql the subquery SQL. |
||
| 121 | * @return string the subquery array expression. |
||
| 122 | */ |
||
| 123 | protected function buildSubqueryArray($sql) |
||
| 127 | } |
||
| 128 |
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.