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 PageLinkProductsTableMap 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 PageLinkProductsTableMap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | View Code Duplication | class PageLinkProductsTableMap extends TableMap |
|
30 | { |
||
31 | use InstancePoolTrait; |
||
32 | use TableMapTrait; |
||
33 | |||
34 | /** |
||
35 | * The (dot-path) name of this class |
||
36 | */ |
||
37 | const CLASS_NAME = 'mod_link.models.Map.PageLinkProductsTableMap'; |
||
38 | |||
39 | /** |
||
40 | * The default database name for this class |
||
41 | */ |
||
42 | const DATABASE_NAME = 'Shop'; |
||
43 | |||
44 | /** |
||
45 | * The table name for this class |
||
46 | */ |
||
47 | const TABLE_NAME = 'page_link_products'; |
||
48 | |||
49 | /** |
||
50 | * The related Propel class for this table |
||
51 | */ |
||
52 | const OM_CLASS = '\\mod_link\\models\\PageLinkProducts'; |
||
53 | |||
54 | /** |
||
55 | * A class that can be returned by this tableMap |
||
56 | */ |
||
57 | const CLASS_DEFAULT = 'mod_link.models.PageLinkProducts'; |
||
58 | |||
59 | /** |
||
60 | * The total number of columns |
||
61 | */ |
||
62 | const NUM_COLUMNS = 2; |
||
63 | |||
64 | /** |
||
65 | * The number of lazy-loaded columns |
||
66 | */ |
||
67 | const NUM_LAZY_LOAD_COLUMNS = 0; |
||
68 | |||
69 | /** |
||
70 | * The number of columns to hydrate (NUM_COLUMNS - NUM_LAZY_LOAD_COLUMNS) |
||
71 | */ |
||
72 | const NUM_HYDRATE_COLUMNS = 2; |
||
73 | |||
74 | /** |
||
75 | * the column name for the link_id field |
||
76 | */ |
||
77 | const COL_LINK_ID = 'page_link_products.link_id'; |
||
78 | |||
79 | /** |
||
80 | * the column name for the product_id field |
||
81 | */ |
||
82 | const COL_PRODUCT_ID = 'page_link_products.product_id'; |
||
83 | |||
84 | /** |
||
85 | * The default string format for model objects of the related table |
||
86 | */ |
||
87 | const DEFAULT_STRING_FORMAT = 'YAML'; |
||
88 | |||
89 | /** |
||
90 | * holds an array of fieldnames |
||
91 | * |
||
92 | * first dimension keys are the type constants |
||
93 | * e.g. self::$fieldNames[self::TYPE_PHPNAME][0] = 'Id' |
||
94 | */ |
||
95 | protected static $fieldNames = array ( |
||
96 | self::TYPE_PHPNAME => array('LinkId', 'ProductId', ), |
||
97 | self::TYPE_CAMELNAME => array('linkId', 'productId', ), |
||
98 | self::TYPE_COLNAME => array(PageLinkProductsTableMap::COL_LINK_ID, PageLinkProductsTableMap::COL_PRODUCT_ID, ), |
||
99 | self::TYPE_FIELDNAME => array('link_id', 'product_id', ), |
||
100 | self::TYPE_NUM => array(0, 1, ) |
||
101 | ); |
||
102 | |||
103 | /** |
||
104 | * holds an array of keys for quick access to the fieldnames array |
||
105 | * |
||
106 | * first dimension keys are the type constants |
||
107 | * e.g. self::$fieldKeys[self::TYPE_PHPNAME]['Id'] = 0 |
||
108 | */ |
||
109 | protected static $fieldKeys = array ( |
||
110 | self::TYPE_PHPNAME => array('LinkId' => 0, 'ProductId' => 1, ), |
||
111 | self::TYPE_CAMELNAME => array('linkId' => 0, 'productId' => 1, ), |
||
112 | self::TYPE_COLNAME => array(PageLinkProductsTableMap::COL_LINK_ID => 0, PageLinkProductsTableMap::COL_PRODUCT_ID => 1, ), |
||
113 | self::TYPE_FIELDNAME => array('link_id' => 0, 'product_id' => 1, ), |
||
114 | self::TYPE_NUM => array(0, 1, ) |
||
115 | ); |
||
116 | |||
117 | /** |
||
118 | * Initialize the table attributes and columns |
||
119 | * Relations are not initialized by this method since they are lazy loaded |
||
120 | * |
||
121 | * @return void |
||
122 | * @throws PropelException |
||
123 | */ |
||
124 | public function initialize() |
||
138 | |||
139 | /** |
||
140 | * Build the RelationMap objects for this table relationships |
||
141 | */ |
||
142 | public function buildRelations() |
||
152 | |||
153 | /** |
||
154 | * Adds an object to the instance pool. |
||
155 | * |
||
156 | * Propel keeps cached copies of objects in an instance pool when they are retrieved |
||
157 | * from the database. In some cases you may need to explicitly add objects |
||
158 | * to the cache in order to ensure that the same objects are always returned by find*() |
||
159 | * and findPk*() calls. |
||
160 | * |
||
161 | * @param \mod_link\models\PageLinkProducts $obj A \mod_link\models\PageLinkProducts object. |
||
162 | * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). |
||
163 | */ |
||
164 | public static function addInstanceToPool($obj, $key = null) |
||
173 | |||
174 | /** |
||
175 | * Removes an object from the instance pool. |
||
176 | * |
||
177 | * Propel keeps cached copies of objects in an instance pool when they are retrieved |
||
178 | * from the database. In some cases -- especially when you override doDelete |
||
179 | * methods in your stub classes -- you may need to explicitly remove objects |
||
180 | * from the cache in order to prevent returning objects that no longer exist. |
||
181 | * |
||
182 | * @param mixed $value A \mod_link\models\PageLinkProducts object or a primary key value. |
||
183 | */ |
||
184 | public static function removeInstanceFromPool($value) |
||
205 | |||
206 | /** |
||
207 | * Retrieves a string version of the primary key from the DB resultset row that can be used to uniquely identify a row in this table. |
||
208 | * |
||
209 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
210 | * a multi-column primary key, a serialize()d version of the primary key will be returned. |
||
211 | * |
||
212 | * @param array $row resultset row. |
||
213 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
214 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
215 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
216 | * |
||
217 | * @return string The primary key hash of the row |
||
218 | */ |
||
219 | public static function getPrimaryKeyHashFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
228 | |||
229 | /** |
||
230 | * Retrieves the primary key from the DB resultset row |
||
231 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
232 | * a multi-column primary key, an array of the primary key columns will be returned. |
||
233 | * |
||
234 | * @param array $row resultset row. |
||
235 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
236 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
237 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
238 | * |
||
239 | * @return mixed The primary key of the row |
||
240 | */ |
||
241 | public static function getPrimaryKeyFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
258 | |||
259 | /** |
||
260 | * The class that the tableMap will make instances of. |
||
261 | * |
||
262 | * If $withPrefix is true, the returned path |
||
263 | * uses a dot-path notation which is translated into a path |
||
264 | * relative to a location on the PHP include_path. |
||
265 | * (e.g. path.to.MyClass -> 'path/to/MyClass.php') |
||
266 | * |
||
267 | * @param boolean $withPrefix Whether or not to return the path with the class name |
||
268 | * @return string path.to.ClassName |
||
269 | */ |
||
270 | public static function getOMClass($withPrefix = true) |
||
274 | |||
275 | /** |
||
276 | * Populates an object of the default type or an object that inherit from the default. |
||
277 | * |
||
278 | * @param array $row row returned by DataFetcher->fetch(). |
||
279 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
280 | * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). |
||
281 | One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
282 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
283 | * |
||
284 | * @throws PropelException Any exceptions caught during processing will be |
||
285 | * rethrown wrapped into a PropelException. |
||
286 | * @return array (PageLinkProducts object, last column rank) |
||
287 | */ |
||
288 | public static function populateObject($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
306 | |||
307 | /** |
||
308 | * The returned array will contain objects of the default type or |
||
309 | * objects that inherit from the default. |
||
310 | * |
||
311 | * @param DataFetcherInterface $dataFetcher |
||
312 | * @return array |
||
313 | * @throws PropelException Any exceptions caught during processing will be |
||
314 | * rethrown wrapped into a PropelException. |
||
315 | */ |
||
316 | public static function populateObjects(DataFetcherInterface $dataFetcher) |
||
341 | /** |
||
342 | * Add all the columns needed to create a new object. |
||
343 | * |
||
344 | * Note: any columns that were marked with lazyLoad="true" in the |
||
345 | * XML schema will not be added to the select list and only loaded |
||
346 | * on demand. |
||
347 | * |
||
348 | * @param Criteria $criteria object containing the columns to add. |
||
349 | * @param string $alias optional table alias |
||
350 | * @throws PropelException Any exceptions caught during processing will be |
||
351 | * rethrown wrapped into a PropelException. |
||
352 | */ |
||
353 | public static function addSelectColumns(Criteria $criteria, $alias = null) |
||
363 | |||
364 | /** |
||
365 | * Returns the TableMap related to this object. |
||
366 | * This method is not needed for general use but a specific application could have a need. |
||
367 | * @return TableMap |
||
368 | * @throws PropelException Any exceptions caught during processing will be |
||
369 | * rethrown wrapped into a PropelException. |
||
370 | */ |
||
371 | public static function getTableMap() |
||
375 | |||
376 | /** |
||
377 | * Add a TableMap instance to the database for this tableMap class. |
||
378 | */ |
||
379 | public static function buildTableMap() |
||
386 | |||
387 | /** |
||
388 | * Performs a DELETE on the database, given a PageLinkProducts or Criteria object OR a primary key value. |
||
389 | * |
||
390 | * @param mixed $values Criteria or PageLinkProducts object or primary key or array of primary keys |
||
391 | * which is used to create the DELETE statement |
||
392 | * @param ConnectionInterface $con the connection to use |
||
393 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
394 | * if supported by native driver or if emulated using Propel. |
||
395 | * @throws PropelException Any exceptions caught during processing will be |
||
396 | * rethrown wrapped into a PropelException. |
||
397 | */ |
||
398 | public static function doDelete($values, ConnectionInterface $con = null) |
||
437 | |||
438 | /** |
||
439 | * Deletes all rows from the page_link_products table. |
||
440 | * |
||
441 | * @param ConnectionInterface $con the connection to use |
||
442 | * @return int The number of affected rows (if supported by underlying database driver). |
||
443 | */ |
||
444 | public static function doDeleteAll(ConnectionInterface $con = null) |
||
448 | |||
449 | /** |
||
450 | * Performs an INSERT on the database, given a PageLinkProducts or Criteria object. |
||
451 | * |
||
452 | * @param mixed $criteria Criteria or PageLinkProducts object containing data that is used to create the INSERT statement. |
||
453 | * @param ConnectionInterface $con the ConnectionInterface connection to use |
||
454 | * @return mixed The new primary key. |
||
455 | * @throws PropelException Any exceptions caught during processing will be |
||
456 | * rethrown wrapped into a PropelException. |
||
457 | */ |
||
458 | public static function doInsert($criteria, ConnectionInterface $con = null) |
||
480 | |||
481 | } // PageLinkProductsTableMap |
||
482 | // This is the static code needed to register the TableMap for this table with the main Propel class. |
||
485 |