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 PageLinkTableMap 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 PageLinkTableMap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class PageLinkTableMap 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.PageLinkTableMap'; |
||
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'; |
||
48 | |||
49 | /** |
||
50 | * The related Propel class for this table |
||
51 | */ |
||
52 | const OM_CLASS = '\\mod_link\\models\\PageLink'; |
||
53 | |||
54 | /** |
||
55 | * A class that can be returned by this tableMap |
||
56 | */ |
||
57 | const CLASS_DEFAULT = 'mod_link.models.PageLink'; |
||
58 | |||
59 | /** |
||
60 | * The total number of columns |
||
61 | */ |
||
62 | const NUM_COLUMNS = 6; |
||
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 = 6; |
||
73 | |||
74 | /** |
||
75 | * the column name for the id field |
||
76 | */ |
||
77 | const COL_ID = 'page_link.id'; |
||
78 | |||
79 | /** |
||
80 | * the column name for the page_id field |
||
81 | */ |
||
82 | const COL_PAGE_ID = 'page_link.page_id'; |
||
83 | |||
84 | /** |
||
85 | * the column name for the active_from field |
||
86 | */ |
||
87 | const COL_ACTIVE_FROM = 'page_link.active_from'; |
||
88 | |||
89 | /** |
||
90 | * the column name for the active_to field |
||
91 | */ |
||
92 | const COL_ACTIVE_TO = 'page_link.active_to'; |
||
93 | |||
94 | /** |
||
95 | * the column name for the show_on field |
||
96 | */ |
||
97 | const COL_SHOW_ON = 'page_link.show_on'; |
||
98 | |||
99 | /** |
||
100 | * the column name for the permanent field |
||
101 | */ |
||
102 | const COL_PERMANENT = 'page_link.permanent'; |
||
103 | |||
104 | /** |
||
105 | * The default string format for model objects of the related table |
||
106 | */ |
||
107 | const DEFAULT_STRING_FORMAT = 'YAML'; |
||
108 | |||
109 | /** |
||
110 | * holds an array of fieldnames |
||
111 | * |
||
112 | * first dimension keys are the type constants |
||
113 | * e.g. self::$fieldNames[self::TYPE_PHPNAME][0] = 'Id' |
||
114 | */ |
||
115 | protected static $fieldNames = array ( |
||
116 | self::TYPE_PHPNAME => array('Id', 'PageId', 'ActiveFrom', 'ActiveTo', 'ShowOn', 'Permanent', ), |
||
117 | self::TYPE_CAMELNAME => array('id', 'pageId', 'activeFrom', 'activeTo', 'showOn', 'permanent', ), |
||
118 | self::TYPE_COLNAME => array(PageLinkTableMap::COL_ID, PageLinkTableMap::COL_PAGE_ID, PageLinkTableMap::COL_ACTIVE_FROM, PageLinkTableMap::COL_ACTIVE_TO, PageLinkTableMap::COL_SHOW_ON, PageLinkTableMap::COL_PERMANENT, ), |
||
119 | self::TYPE_FIELDNAME => array('id', 'page_id', 'active_from', 'active_to', 'show_on', 'permanent', ), |
||
120 | self::TYPE_NUM => array(0, 1, 2, 3, 4, 5, ) |
||
121 | ); |
||
122 | |||
123 | /** |
||
124 | * holds an array of keys for quick access to the fieldnames array |
||
125 | * |
||
126 | * first dimension keys are the type constants |
||
127 | * e.g. self::$fieldKeys[self::TYPE_PHPNAME]['Id'] = 0 |
||
128 | */ |
||
129 | protected static $fieldKeys = array ( |
||
130 | self::TYPE_PHPNAME => array('Id' => 0, 'PageId' => 1, 'ActiveFrom' => 2, 'ActiveTo' => 3, 'ShowOn' => 4, 'Permanent' => 5, ), |
||
131 | self::TYPE_CAMELNAME => array('id' => 0, 'pageId' => 1, 'activeFrom' => 2, 'activeTo' => 3, 'showOn' => 4, 'permanent' => 5, ), |
||
132 | self::TYPE_COLNAME => array(PageLinkTableMap::COL_ID => 0, PageLinkTableMap::COL_PAGE_ID => 1, PageLinkTableMap::COL_ACTIVE_FROM => 2, PageLinkTableMap::COL_ACTIVE_TO => 3, PageLinkTableMap::COL_SHOW_ON => 4, PageLinkTableMap::COL_PERMANENT => 5, ), |
||
133 | self::TYPE_FIELDNAME => array('id' => 0, 'page_id' => 1, 'active_from' => 2, 'active_to' => 3, 'show_on' => 4, 'permanent' => 5, ), |
||
134 | self::TYPE_NUM => array(0, 1, 2, 3, 4, 5, ) |
||
135 | ); |
||
136 | |||
137 | /** |
||
138 | * Initialize the table attributes and columns |
||
139 | * Relations are not initialized by this method since they are lazy loaded |
||
140 | * |
||
141 | * @return void |
||
142 | * @throws PropelException |
||
143 | */ |
||
144 | View Code Duplication | public function initialize() |
|
161 | |||
162 | /** |
||
163 | * Build the RelationMap objects for this table relationships |
||
164 | */ |
||
165 | public function buildRelations() |
||
175 | |||
176 | /** |
||
177 | * 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. |
||
178 | * |
||
179 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
180 | * a multi-column primary key, a serialize()d version of the primary key will be returned. |
||
181 | * |
||
182 | * @param array $row resultset row. |
||
183 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
184 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
185 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
186 | * |
||
187 | * @return string The primary key hash of the row |
||
188 | */ |
||
189 | View Code Duplication | public static function getPrimaryKeyHashFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
|
198 | |||
199 | /** |
||
200 | * Retrieves the primary key from the DB resultset row |
||
201 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
202 | * a multi-column primary key, an array of the primary key columns will be returned. |
||
203 | * |
||
204 | * @param array $row resultset row. |
||
205 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
206 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
207 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
208 | * |
||
209 | * @return mixed The primary key of the row |
||
210 | */ |
||
211 | View Code Duplication | public static function getPrimaryKeyFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
|
219 | |||
220 | /** |
||
221 | * The class that the tableMap will make instances of. |
||
222 | * |
||
223 | * If $withPrefix is true, the returned path |
||
224 | * uses a dot-path notation which is translated into a path |
||
225 | * relative to a location on the PHP include_path. |
||
226 | * (e.g. path.to.MyClass -> 'path/to/MyClass.php') |
||
227 | * |
||
228 | * @param boolean $withPrefix Whether or not to return the path with the class name |
||
229 | * @return string path.to.ClassName |
||
230 | */ |
||
231 | public static function getOMClass($withPrefix = true) |
||
235 | |||
236 | /** |
||
237 | * Populates an object of the default type or an object that inherit from the default. |
||
238 | * |
||
239 | * @param array $row row returned by DataFetcher->fetch(). |
||
240 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
241 | * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). |
||
242 | One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
243 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
244 | * |
||
245 | * @throws PropelException Any exceptions caught during processing will be |
||
246 | * rethrown wrapped into a PropelException. |
||
247 | * @return array (PageLink object, last column rank) |
||
248 | */ |
||
249 | public static function populateObject($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
267 | |||
268 | /** |
||
269 | * The returned array will contain objects of the default type or |
||
270 | * objects that inherit from the default. |
||
271 | * |
||
272 | * @param DataFetcherInterface $dataFetcher |
||
273 | * @return array |
||
274 | * @throws PropelException Any exceptions caught during processing will be |
||
275 | * rethrown wrapped into a PropelException. |
||
276 | */ |
||
277 | public static function populateObjects(DataFetcherInterface $dataFetcher) |
||
302 | /** |
||
303 | * Add all the columns needed to create a new object. |
||
304 | * |
||
305 | * Note: any columns that were marked with lazyLoad="true" in the |
||
306 | * XML schema will not be added to the select list and only loaded |
||
307 | * on demand. |
||
308 | * |
||
309 | * @param Criteria $criteria object containing the columns to add. |
||
310 | * @param string $alias optional table alias |
||
311 | * @throws PropelException Any exceptions caught during processing will be |
||
312 | * rethrown wrapped into a PropelException. |
||
313 | */ |
||
314 | public static function addSelectColumns(Criteria $criteria, $alias = null) |
||
332 | |||
333 | /** |
||
334 | * Returns the TableMap related to this object. |
||
335 | * This method is not needed for general use but a specific application could have a need. |
||
336 | * @return TableMap |
||
337 | * @throws PropelException Any exceptions caught during processing will be |
||
338 | * rethrown wrapped into a PropelException. |
||
339 | */ |
||
340 | public static function getTableMap() |
||
344 | |||
345 | /** |
||
346 | * Add a TableMap instance to the database for this tableMap class. |
||
347 | */ |
||
348 | public static function buildTableMap() |
||
355 | |||
356 | /** |
||
357 | * Performs a DELETE on the database, given a PageLink or Criteria object OR a primary key value. |
||
358 | * |
||
359 | * @param mixed $values Criteria or PageLink object or primary key or array of primary keys |
||
360 | * which is used to create the DELETE statement |
||
361 | * @param ConnectionInterface $con the connection to use |
||
362 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
363 | * if supported by native driver or if emulated using Propel. |
||
364 | * @throws PropelException Any exceptions caught during processing will be |
||
365 | * rethrown wrapped into a PropelException. |
||
366 | */ |
||
367 | public static function doDelete($values, ConnectionInterface $con = null) |
||
396 | |||
397 | /** |
||
398 | * Deletes all rows from the page_link 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 | public static function doDeleteAll(ConnectionInterface $con = null) |
||
407 | |||
408 | /** |
||
409 | * Performs an INSERT on the database, given a PageLink or Criteria object. |
||
410 | * |
||
411 | * @param mixed $criteria Criteria or PageLink object containing data that is used to create the INSERT statement. |
||
412 | * @param ConnectionInterface $con the ConnectionInterface connection to use |
||
413 | * @return mixed The new primary key. |
||
414 | * @throws PropelException Any exceptions caught during processing will be |
||
415 | * rethrown wrapped into a PropelException. |
||
416 | */ |
||
417 | View Code Duplication | public static function doInsert($criteria, ConnectionInterface $con = null) |
|
443 | |||
444 | } // PageLinkTableMap |
||
445 | // This is the static code needed to register the TableMap for this table with the main Propel class. |
||
448 |