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 |
||
28 | class DbRow |
||
29 | { |
||
30 | /** |
||
31 | * The service this object is bound to. |
||
32 | * |
||
33 | * @var TDBMService |
||
34 | */ |
||
35 | protected $tdbmService; |
||
36 | |||
37 | /** |
||
38 | * The object containing this db row. |
||
39 | * |
||
40 | * @var AbstractTDBMObject |
||
41 | */ |
||
42 | private $object; |
||
43 | |||
44 | /** |
||
45 | * The name of the table the object if issued from. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $dbTableName; |
||
50 | |||
51 | /** |
||
52 | * The array of columns returned from database. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | private $dbRow = array(); |
||
57 | |||
58 | /** |
||
59 | * @var AbstractTDBMObject[] |
||
60 | */ |
||
61 | private $references = array(); |
||
62 | |||
63 | /** |
||
64 | * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
65 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
66 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
67 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $status; |
||
72 | |||
73 | /** |
||
74 | * The values of the primary key. |
||
75 | * This is set when the object is in "loaded" state. |
||
76 | * |
||
77 | * @var array An array of column => value |
||
78 | */ |
||
79 | private $primaryKeys; |
||
80 | |||
81 | /** |
||
82 | * You should never call the constructor directly. Instead, you should use the |
||
83 | * TDBMService class that will create TDBMObjects for you. |
||
84 | * |
||
85 | * Used with id!=false when we want to retrieve an existing object |
||
86 | * and id==false if we want a new object |
||
87 | * |
||
88 | * @param AbstractTDBMObject $object The object containing this db row. |
||
89 | * @param string $table_name |
||
90 | * @param array $primaryKeys |
||
91 | * @param TDBMService $tdbmService |
||
92 | * |
||
93 | * @throws TDBMException |
||
94 | * @throws TDBMInvalidOperationException |
||
95 | */ |
||
96 | public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
||
125 | |||
126 | public function _attach(TDBMService $tdbmService) |
||
135 | |||
136 | /** |
||
137 | * Sets the state of the TDBM Object |
||
138 | * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
139 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
140 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
141 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
142 | * |
||
143 | * @param string $state |
||
144 | */ |
||
145 | public function _setStatus($state) |
||
149 | |||
150 | /** |
||
151 | * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
||
152 | * If the object is in state 'not loaded', this method performs a query in database to load the object. |
||
153 | * |
||
154 | * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
||
155 | * cannot be found). |
||
156 | */ |
||
157 | public function _dbLoadIfNotLoaded() |
||
179 | |||
180 | public function get($var) |
||
220 | |||
221 | /** |
||
222 | * Returns true if a column is set, false otherwise. |
||
223 | * |
||
224 | * @param string $var |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | /*public function has($var) { |
||
229 | $this->_dbLoadIfNotLoaded(); |
||
230 | |||
231 | return isset($this->dbRow[$var]); |
||
232 | }*/ |
||
233 | |||
234 | View Code Duplication | public function set($var, $value) |
|
256 | |||
257 | /** |
||
258 | * @param string $foreignKeyName |
||
259 | * @param AbstractTDBMObject $bean |
||
260 | */ |
||
261 | View Code Duplication | public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
270 | |||
271 | /** |
||
272 | * @param string $foreignKeyName A unique name for this reference |
||
273 | * |
||
274 | * @return AbstractTDBMObject|null |
||
275 | */ |
||
276 | public function getRef($foreignKeyName) |
||
299 | |||
300 | /** |
||
301 | * Returns the name of the table this object comes from. |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function _getDbTableName() |
||
309 | |||
310 | /** |
||
311 | * Method used internally by TDBM. You should not use it directly. |
||
312 | * This method returns the status of the TDBMObject. |
||
313 | * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
314 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
315 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
316 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | public function _getStatus() |
||
324 | |||
325 | /** |
||
326 | * Override the native php clone function for TDBMObjects. |
||
327 | */ |
||
328 | public function __clone() |
||
346 | |||
347 | /** |
||
348 | * Returns raw database row. |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | public function _getDbRow() |
||
372 | |||
373 | /** |
||
374 | * Returns references array. |
||
375 | * |
||
376 | * @return AbstractTDBMObject[] |
||
377 | */ |
||
378 | public function _getReferences() |
||
382 | |||
383 | /** |
||
384 | * Returns the values of the primary key. |
||
385 | * This is set when the object is in "loaded" state. |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | public function _getPrimaryKeys() |
||
393 | |||
394 | /** |
||
395 | * Sets the values of the primary key. |
||
396 | * This is set when the object is in "loaded" state. |
||
397 | * |
||
398 | * @param array $primaryKeys |
||
399 | */ |
||
400 | public function _setPrimaryKeys(array $primaryKeys) |
||
407 | |||
408 | /** |
||
409 | * Returns the TDBMObject this bean is associated to. |
||
410 | * |
||
411 | * @return AbstractTDBMObject |
||
412 | */ |
||
413 | public function getTDBMObject() |
||
417 | |||
418 | /** |
||
419 | * Sets the TDBMObject this bean is associated to. |
||
420 | * Only used when cloning. |
||
421 | * |
||
422 | * @param AbstractTDBMObject $object |
||
423 | */ |
||
424 | public function setTDBMObject(AbstractTDBMObject $object) |
||
428 | } |
||
429 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.