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 |
||
7 | class Escaper |
||
8 | { |
||
9 | /** |
||
10 | * @var ConnectionInterface |
||
11 | */ |
||
12 | protected $conn; |
||
13 | |||
14 | 3 | public function __construct(ConnectionInterface $conn) |
|
18 | |||
19 | /** |
||
20 | * Wraps the input with identifiers when necessary. |
||
21 | * |
||
22 | * @param Expr|string $value The string to be quoted, or an Expression to leave it untouched |
||
23 | * |
||
24 | * @return string The untouched Expression or the quoted string |
||
25 | */ |
||
26 | public function quoteIdentifier($value) |
||
44 | |||
45 | /** |
||
46 | * Calls $this->quoteIdentifier() on every element of the array passed. |
||
47 | * |
||
48 | * @param array $array An array of strings to be quoted |
||
49 | * |
||
50 | * @return array The array of quoted strings |
||
51 | */ |
||
52 | public function quoteIdentifierArr(array $array) |
||
62 | |||
63 | /** |
||
64 | * Adds quotes around values when necessary. |
||
65 | * Based on FuelPHP's quoting function. |
||
66 | * |
||
67 | * @param Expr|mixed $value The input string, eventually wrapped in an expression to leave it untouched |
||
68 | * |
||
69 | * @return string The untouched Expression or the quoted string |
||
70 | */ |
||
71 | public function quote($value) |
||
104 | |||
105 | /** |
||
106 | * Calls $this->quote() on every element of the array passed. |
||
107 | * |
||
108 | * @param array $array The array of strings to quote |
||
109 | * |
||
110 | * @return array The array of quotes strings |
||
111 | */ |
||
112 | View Code Duplication | public function quoteArr(array $array) |
|
122 | |||
123 | View Code Duplication | public function quoteSetArr(array $array) |
|
133 | |||
134 | /** |
||
135 | * Escapes the query for the MATCH() function. |
||
136 | * |
||
137 | * @param string $string The string to escape for the MATCH |
||
138 | * |
||
139 | * @return string The escaped string |
||
140 | */ |
||
141 | public function escapeMatch($string) |
||
163 | |||
164 | /** |
||
165 | * Escapes the query for the MATCH() function |
||
166 | * Allows some of the control characters to pass through for use with a search field: -, |, " |
||
167 | * It also does some tricks to wrap/unwrap within " the string and prevents errors. |
||
168 | * |
||
169 | * @param string $string The string to escape for the MATCH |
||
170 | * |
||
171 | * @return string The escaped string |
||
172 | */ |
||
173 | public function halfEscapeMatch($string) |
||
205 | } |
||
206 |
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.