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 SelfpriceQuery 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 SelfpriceQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
60 | abstract class SelfpriceQuery extends ModelCriteria |
||
61 | { |
||
62 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
63 | |||
64 | /** |
||
65 | * Initializes internal state of \Selfprice\Models\Selfprice\Base\SelfpriceQuery object. |
||
66 | * |
||
67 | * @param string $dbName The database name |
||
68 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
69 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
70 | */ |
||
71 | public function __construct($dbName = 'default', $modelName = '\\Selfprice\\Models\\Selfprice\\Selfprice', $modelAlias = null) |
||
75 | |||
76 | /** |
||
77 | * Returns a new ChildSelfpriceQuery object. |
||
78 | * |
||
79 | * @param string $modelAlias The alias of a model in the query |
||
80 | * @param Criteria $criteria Optional Criteria to build the query from |
||
81 | * |
||
82 | * @return ChildSelfpriceQuery |
||
83 | */ |
||
84 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
99 | |||
100 | /** |
||
101 | * Find object by primary key. |
||
102 | * Propel uses the instance pool to skip the database if the object exists. |
||
103 | * Go fast if the query is untouched. |
||
104 | * |
||
105 | * <code> |
||
106 | * $obj = $c->findPk(12, $con); |
||
107 | * </code> |
||
108 | * |
||
109 | * @param mixed $key Primary key to use for the query |
||
110 | * @param ConnectionInterface $con an optional connection object |
||
111 | * |
||
112 | * @return ChildSelfprice|array|mixed the result, formatted by the current formatter |
||
113 | */ |
||
114 | View Code Duplication | public function findPk($key, ConnectionInterface $con = null) |
|
135 | |||
136 | /** |
||
137 | * Find object by primary key using raw SQL to go fast. |
||
138 | * Bypass doSelect() and the object formatter by using generated code. |
||
139 | * |
||
140 | * @param mixed $key Primary key to use for the query |
||
141 | * @param ConnectionInterface $con A connection object |
||
142 | * |
||
143 | * @throws \Propel\Runtime\Exception\PropelException |
||
144 | * |
||
145 | * @return ChildSelfprice A model object, or null if the key is not found |
||
146 | */ |
||
147 | View Code Duplication | protected function findPkSimple($key, ConnectionInterface $con) |
|
169 | |||
170 | /** |
||
171 | * Find object by primary key. |
||
172 | * |
||
173 | * @param mixed $key Primary key to use for the query |
||
174 | * @param ConnectionInterface $con A connection object |
||
175 | * |
||
176 | * @return ChildSelfprice|array|mixed the result, formatted by the current formatter |
||
177 | */ |
||
178 | View Code Duplication | protected function findPkComplex($key, ConnectionInterface $con) |
|
188 | |||
189 | /** |
||
190 | * Find objects by primary key |
||
191 | * <code> |
||
192 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
193 | * </code> |
||
194 | * @param array $keys Primary keys to use for the query |
||
195 | * @param ConnectionInterface $con an optional connection object |
||
196 | * |
||
197 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
198 | */ |
||
199 | View Code Duplication | public function findPks($keys, ConnectionInterface $con = null) |
|
212 | |||
213 | /** |
||
214 | * Filter the query by primary key |
||
215 | * |
||
216 | * @param mixed $key Primary key to use for the query |
||
217 | * |
||
218 | * @return $this|ChildSelfpriceQuery The current query, for fluid interface |
||
219 | */ |
||
220 | public function filterByPrimaryKey($key) |
||
225 | |||
226 | /** |
||
227 | * Filter the query by a list of primary keys |
||
228 | * |
||
229 | * @param array $keys The list of primary key to use for the query |
||
230 | * |
||
231 | * @return $this|ChildSelfpriceQuery The current query, for fluid interface |
||
232 | */ |
||
233 | public function filterByPrimaryKeys($keys) |
||
238 | |||
239 | /** |
||
240 | * Filter the query on the id column |
||
241 | * |
||
242 | * Example usage: |
||
243 | * <code> |
||
244 | * $query->filterById(1234); // WHERE id = 1234 |
||
245 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
246 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
247 | * </code> |
||
248 | * |
||
249 | * @param mixed $id The value to use as filter. |
||
250 | * Use scalar values for equality. |
||
251 | * Use array values for in_array() equivalent. |
||
252 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
253 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
254 | * |
||
255 | * @return $this|ChildSelfpriceQuery The current query, for fluid interface |
||
256 | */ |
||
257 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
279 | |||
280 | /** |
||
281 | * Filter the query on the name column |
||
282 | * |
||
283 | * Example usage: |
||
284 | * <code> |
||
285 | * $query->filterByName('fooValue'); // WHERE name = 'fooValue' |
||
286 | * $query->filterByName('%fooValue%'); // WHERE name LIKE '%fooValue%' |
||
287 | * </code> |
||
288 | * |
||
289 | * @param string $name The value to use as filter. |
||
290 | * Accepts wildcards (* and % trigger a LIKE) |
||
291 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
292 | * |
||
293 | * @return $this|ChildSelfpriceQuery The current query, for fluid interface |
||
294 | */ |
||
295 | View Code Duplication | public function filterByName($name = null, $comparison = null) |
|
308 | |||
309 | /** |
||
310 | * Filter the query on the datecreate column |
||
311 | * |
||
312 | * Example usage: |
||
313 | * <code> |
||
314 | * $query->filterByDatecreate('2011-03-14'); // WHERE datecreate = '2011-03-14' |
||
315 | * $query->filterByDatecreate('now'); // WHERE datecreate = '2011-03-14' |
||
316 | * $query->filterByDatecreate(array('max' => 'yesterday')); // WHERE datecreate > '2011-03-13' |
||
317 | * </code> |
||
318 | * |
||
319 | * @param mixed $datecreate The value to use as filter. |
||
320 | * Values can be integers (unix timestamps), DateTime objects, or strings. |
||
321 | * Empty strings are treated as NULL. |
||
322 | * Use scalar values for equality. |
||
323 | * Use array values for in_array() equivalent. |
||
324 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
325 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
326 | * |
||
327 | * @return $this|ChildSelfpriceQuery The current query, for fluid interface |
||
328 | */ |
||
329 | View Code Duplication | public function filterByDatecreate($datecreate = null, $comparison = null) |
|
351 | |||
352 | /** |
||
353 | * Filter the query on the desc column |
||
354 | * |
||
355 | * Example usage: |
||
356 | * <code> |
||
357 | * $query->filterByDesc('fooValue'); // WHERE desc = 'fooValue' |
||
358 | * $query->filterByDesc('%fooValue%'); // WHERE desc LIKE '%fooValue%' |
||
359 | * </code> |
||
360 | * |
||
361 | * @param string $desc The value to use as filter. |
||
362 | * Accepts wildcards (* and % trigger a LIKE) |
||
363 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
364 | * |
||
365 | * @return $this|ChildSelfpriceQuery The current query, for fluid interface |
||
366 | */ |
||
367 | View Code Duplication | public function filterByDesc($desc = null, $comparison = null) |
|
380 | |||
381 | /** |
||
382 | * Exclude object from result |
||
383 | * |
||
384 | * @param ChildSelfprice $selfprice Object to remove from the list of results |
||
385 | * |
||
386 | * @return $this|ChildSelfpriceQuery The current query, for fluid interface |
||
387 | */ |
||
388 | public function prune($selfprice = null) |
||
396 | |||
397 | /** |
||
398 | * Deletes all rows from the selfprice table. |
||
399 | * |
||
400 | * @param ConnectionInterface $con the connection to use |
||
401 | * @return int The number of affected rows (if supported by underlying database driver). |
||
402 | */ |
||
403 | View Code Duplication | public function doDeleteAll(ConnectionInterface $con = null) |
|
423 | |||
424 | /** |
||
425 | * Performs a DELETE on the database based on the current ModelCriteria |
||
426 | * |
||
427 | * @param ConnectionInterface $con the connection to use |
||
428 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
429 | * if supported by native driver or if emulated using Propel. |
||
430 | * @throws PropelException Any exceptions caught during processing will be |
||
431 | * rethrown wrapped into a PropelException. |
||
432 | */ |
||
433 | View Code Duplication | public function delete(ConnectionInterface $con = null) |
|
457 | |||
458 | } // SelfpriceQuery |
||
459 |
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.