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 RouteTableMap 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 RouteTableMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class RouteTableMap extends TableMap |
||
| 32 | { |
||
| 33 | use InstancePoolTrait; |
||
| 34 | use TableMapTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The (dot-path) name of this class |
||
| 38 | */ |
||
| 39 | const CLASS_NAME = 'core.models.Map.RouteTableMap'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The default database name for this class |
||
| 43 | */ |
||
| 44 | const DATABASE_NAME = 'Shop'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The table name for this class |
||
| 48 | */ |
||
| 49 | const TABLE_NAME = 'route'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The related Propel class for this table |
||
| 53 | */ |
||
| 54 | const OM_CLASS = '\\core\\models\\Route'; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * A class that can be returned by this tableMap |
||
| 58 | */ |
||
| 59 | const CLASS_DEFAULT = 'core.models.Route'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The total number of columns |
||
| 63 | */ |
||
| 64 | const NUM_COLUMNS = 5; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The number of lazy-loaded columns |
||
| 68 | */ |
||
| 69 | const NUM_LAZY_LOAD_COLUMNS = 0; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The number of columns to hydrate (NUM_COLUMNS - NUM_LAZY_LOAD_COLUMNS) |
||
| 73 | */ |
||
| 74 | const NUM_HYDRATE_COLUMNS = 5; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * the column name for the id field |
||
| 78 | */ |
||
| 79 | const COL_ID = 'route.id'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * the column name for the entity_id field |
||
| 83 | */ |
||
| 84 | const COL_ENTITY_ID = 'route.entity_id'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * the column name for the type field |
||
| 88 | */ |
||
| 89 | const COL_TYPE = 'route.type'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * the column name for the parent_url field |
||
| 93 | */ |
||
| 94 | const COL_PARENT_URL = 'route.parent_url'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * the column name for the url field |
||
| 98 | */ |
||
| 99 | const COL_URL = 'route.url'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The default string format for model objects of the related table |
||
| 103 | */ |
||
| 104 | const DEFAULT_STRING_FORMAT = 'YAML'; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * holds an array of fieldnames |
||
| 108 | * |
||
| 109 | * first dimension keys are the type constants |
||
| 110 | * e.g. self::$fieldNames[self::TYPE_PHPNAME][0] = 'Id' |
||
| 111 | */ |
||
| 112 | protected static $fieldNames = array ( |
||
| 113 | self::TYPE_PHPNAME => array('Id', 'EntityId', 'Type', 'ParentUrl', 'Url', ), |
||
| 114 | self::TYPE_CAMELNAME => array('id', 'entityId', 'type', 'parentUrl', 'url', ), |
||
| 115 | self::TYPE_COLNAME => array(RouteTableMap::COL_ID, RouteTableMap::COL_ENTITY_ID, RouteTableMap::COL_TYPE, RouteTableMap::COL_PARENT_URL, RouteTableMap::COL_URL, ), |
||
| 116 | self::TYPE_FIELDNAME => array('id', 'entity_id', 'type', 'parent_url', 'url', ), |
||
| 117 | self::TYPE_NUM => array(0, 1, 2, 3, 4, ) |
||
| 118 | ); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * holds an array of keys for quick access to the fieldnames array |
||
| 122 | * |
||
| 123 | * first dimension keys are the type constants |
||
| 124 | * e.g. self::$fieldKeys[self::TYPE_PHPNAME]['Id'] = 0 |
||
| 125 | */ |
||
| 126 | protected static $fieldKeys = array ( |
||
| 127 | self::TYPE_PHPNAME => array('Id' => 0, 'EntityId' => 1, 'Type' => 2, 'ParentUrl' => 3, 'Url' => 4, ), |
||
| 128 | self::TYPE_CAMELNAME => array('id' => 0, 'entityId' => 1, 'type' => 2, 'parentUrl' => 3, 'url' => 4, ), |
||
| 129 | self::TYPE_COLNAME => array(RouteTableMap::COL_ID => 0, RouteTableMap::COL_ENTITY_ID => 1, RouteTableMap::COL_TYPE => 2, RouteTableMap::COL_PARENT_URL => 3, RouteTableMap::COL_URL => 4, ), |
||
| 130 | self::TYPE_FIELDNAME => array('id' => 0, 'entity_id' => 1, 'type' => 2, 'parent_url' => 3, 'url' => 4, ), |
||
| 131 | self::TYPE_NUM => array(0, 1, 2, 3, 4, ) |
||
| 132 | ); |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Initialize the table attributes and columns |
||
| 136 | * Relations are not initialized by this method since they are lazy loaded |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | * @throws PropelException |
||
| 140 | */ |
||
| 141 | public function initialize() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Build the RelationMap objects for this table relationships |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function buildRelations() |
|
| 178 | /** |
||
| 179 | * Method to invalidate the instance pool of all tables related to route * by a foreign key with ON DELETE CASCADE |
||
| 180 | */ |
||
| 181 | public static function clearRelatedInstancePool() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * 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. |
||
| 191 | * |
||
| 192 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
| 193 | * a multi-column primary key, a serialize()d version of the primary key will be returned. |
||
| 194 | * |
||
| 195 | * @param array $row resultset row. |
||
| 196 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
| 197 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
| 198 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
| 199 | * |
||
| 200 | * @return string The primary key hash of the row |
||
| 201 | */ |
||
| 202 | View Code Duplication | public static function getPrimaryKeyHashFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Retrieves the primary key from the DB resultset row |
||
| 214 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
| 215 | * a multi-column primary key, an array of the primary key columns will be returned. |
||
| 216 | * |
||
| 217 | * @param array $row resultset row. |
||
| 218 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
| 219 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
| 220 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
| 221 | * |
||
| 222 | * @return mixed The primary key of the row |
||
| 223 | */ |
||
| 224 | View Code Duplication | public static function getPrimaryKeyFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * The class that the tableMap will make instances of. |
||
| 235 | * |
||
| 236 | * If $withPrefix is true, the returned path |
||
| 237 | * uses a dot-path notation which is translated into a path |
||
| 238 | * relative to a location on the PHP include_path. |
||
| 239 | * (e.g. path.to.MyClass -> 'path/to/MyClass.php') |
||
| 240 | * |
||
| 241 | * @param boolean $withPrefix Whether or not to return the path with the class name |
||
| 242 | * @return string path.to.ClassName |
||
| 243 | */ |
||
| 244 | public static function getOMClass($withPrefix = true) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Populates an object of the default type or an object that inherit from the default. |
||
| 251 | * |
||
| 252 | * @param array $row row returned by DataFetcher->fetch(). |
||
| 253 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
| 254 | * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). |
||
| 255 | One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
| 256 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
| 257 | * |
||
| 258 | * @throws PropelException Any exceptions caught during processing will be |
||
| 259 | * rethrown wrapped into a PropelException. |
||
| 260 | * @return array (Route object, last column rank) |
||
| 261 | */ |
||
| 262 | View Code Duplication | public static function populateObject($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * The returned array will contain objects of the default type or |
||
| 283 | * objects that inherit from the default. |
||
| 284 | * |
||
| 285 | * @param DataFetcherInterface $dataFetcher |
||
| 286 | * @return array |
||
| 287 | * @throws PropelException Any exceptions caught during processing will be |
||
| 288 | * rethrown wrapped into a PropelException. |
||
| 289 | */ |
||
| 290 | View Code Duplication | public static function populateObjects(DataFetcherInterface $dataFetcher) |
|
| 315 | /** |
||
| 316 | * Add all the columns needed to create a new object. |
||
| 317 | * |
||
| 318 | * Note: any columns that were marked with lazyLoad="true" in the |
||
| 319 | * XML schema will not be added to the select list and only loaded |
||
| 320 | * on demand. |
||
| 321 | * |
||
| 322 | * @param Criteria $criteria object containing the columns to add. |
||
| 323 | * @param string $alias optional table alias |
||
| 324 | * @throws PropelException Any exceptions caught during processing will be |
||
| 325 | * rethrown wrapped into a PropelException. |
||
| 326 | */ |
||
| 327 | public static function addSelectColumns(Criteria $criteria, $alias = null) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Returns the TableMap related to this object. |
||
| 346 | * This method is not needed for general use but a specific application could have a need. |
||
| 347 | * @return TableMap |
||
| 348 | * @throws PropelException Any exceptions caught during processing will be |
||
| 349 | * rethrown wrapped into a PropelException. |
||
| 350 | */ |
||
| 351 | public static function getTableMap() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Add a TableMap instance to the database for this tableMap class. |
||
| 358 | */ |
||
| 359 | public static function buildTableMap() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Performs a DELETE on the database, given a Route or Criteria object OR a primary key value. |
||
| 369 | * |
||
| 370 | * @param mixed $values Criteria or Route object or primary key or array of primary keys |
||
| 371 | * which is used to create the DELETE statement |
||
| 372 | * @param ConnectionInterface $con the connection to use |
||
| 373 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
| 374 | * if supported by native driver or if emulated using Propel. |
||
| 375 | * @throws PropelException Any exceptions caught during processing will be |
||
| 376 | * rethrown wrapped into a PropelException. |
||
| 377 | */ |
||
| 378 | public static function doDelete($values, ConnectionInterface $con = null) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Deletes all rows from the route table. |
||
| 410 | * |
||
| 411 | * @param ConnectionInterface $con the connection to use |
||
| 412 | * @return int The number of affected rows (if supported by underlying database driver). |
||
| 413 | */ |
||
| 414 | public static function doDeleteAll(ConnectionInterface $con = null) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Performs an INSERT on the database, given a Route or Criteria object. |
||
| 421 | * |
||
| 422 | * @param mixed $criteria Criteria or Route object containing data that is used to create the INSERT statement. |
||
| 423 | * @param ConnectionInterface $con the ConnectionInterface connection to use |
||
| 424 | * @return mixed The new primary key. |
||
| 425 | * @throws PropelException Any exceptions caught during processing will be |
||
| 426 | * rethrown wrapped into a PropelException. |
||
| 427 | */ |
||
| 428 | View Code Duplication | public static function doInsert($criteria, ConnectionInterface $con = null) |
|
| 454 | |||
| 455 | } // RouteTableMap |
||
| 456 | // This is the static code needed to register the TableMap for this table with the main Propel class. |
||
| 459 |