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 BannersI18nTableMap 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 BannersI18nTableMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class BannersI18nTableMap extends TableMap |
||
| 30 | { |
||
| 31 | use InstancePoolTrait; |
||
| 32 | use TableMapTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The (dot-path) name of this class |
||
| 36 | */ |
||
| 37 | const CLASS_NAME = 'xbanners.models.Map.BannersI18nTableMap'; |
||
| 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 = 'banners_i18n'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The related Propel class for this table |
||
| 51 | */ |
||
| 52 | const OM_CLASS = '\\xbanners\\models\\BannersI18n'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * A class that can be returned by this tableMap |
||
| 56 | */ |
||
| 57 | const CLASS_DEFAULT = 'xbanners.models.BannersI18n'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The total number of columns |
||
| 61 | */ |
||
| 62 | const NUM_COLUMNS = 3; |
||
| 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 = 3; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * the column name for the id field |
||
| 76 | */ |
||
| 77 | const COL_ID = 'banners_i18n.id'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * the column name for the locale field |
||
| 81 | */ |
||
| 82 | const COL_LOCALE = 'banners_i18n.locale'; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * the column name for the name field |
||
| 86 | */ |
||
| 87 | const COL_NAME = 'banners_i18n.name'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The default string format for model objects of the related table |
||
| 91 | */ |
||
| 92 | const DEFAULT_STRING_FORMAT = 'YAML'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * holds an array of fieldnames |
||
| 96 | * |
||
| 97 | * first dimension keys are the type constants |
||
| 98 | * e.g. self::$fieldNames[self::TYPE_PHPNAME][0] = 'Id' |
||
| 99 | */ |
||
| 100 | protected static $fieldNames = array ( |
||
| 101 | self::TYPE_PHPNAME => array('Id', 'Locale', 'Name', ), |
||
| 102 | self::TYPE_CAMELNAME => array('id', 'locale', 'name', ), |
||
| 103 | self::TYPE_COLNAME => array(BannersI18nTableMap::COL_ID, BannersI18nTableMap::COL_LOCALE, BannersI18nTableMap::COL_NAME, ), |
||
| 104 | self::TYPE_FIELDNAME => array('id', 'locale', 'name', ), |
||
| 105 | self::TYPE_NUM => array(0, 1, 2, ) |
||
| 106 | ); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * holds an array of keys for quick access to the fieldnames array |
||
| 110 | * |
||
| 111 | * first dimension keys are the type constants |
||
| 112 | * e.g. self::$fieldKeys[self::TYPE_PHPNAME]['Id'] = 0 |
||
| 113 | */ |
||
| 114 | protected static $fieldKeys = array ( |
||
| 115 | self::TYPE_PHPNAME => array('Id' => 0, 'Locale' => 1, 'Name' => 2, ), |
||
| 116 | self::TYPE_CAMELNAME => array('id' => 0, 'locale' => 1, 'name' => 2, ), |
||
| 117 | self::TYPE_COLNAME => array(BannersI18nTableMap::COL_ID => 0, BannersI18nTableMap::COL_LOCALE => 1, BannersI18nTableMap::COL_NAME => 2, ), |
||
| 118 | self::TYPE_FIELDNAME => array('id' => 0, 'locale' => 1, 'name' => 2, ), |
||
| 119 | self::TYPE_NUM => array(0, 1, 2, ) |
||
| 120 | ); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Initialize the table attributes and columns |
||
| 124 | * Relations are not initialized by this method since they are lazy loaded |
||
| 125 | * |
||
| 126 | * @return void |
||
| 127 | * @throws PropelException |
||
| 128 | */ |
||
| 129 | public function initialize() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Build the RelationMap objects for this table relationships |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function buildRelations() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Adds an object to the instance pool. |
||
| 160 | * |
||
| 161 | * Propel keeps cached copies of objects in an instance pool when they are retrieved |
||
| 162 | * from the database. In some cases you may need to explicitly add objects |
||
| 163 | * to the cache in order to ensure that the same objects are always returned by find*() |
||
| 164 | * and findPk*() calls. |
||
| 165 | * |
||
| 166 | * @param \xbanners\models\BannersI18n $obj A \xbanners\models\BannersI18n object. |
||
| 167 | * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). |
||
| 168 | */ |
||
| 169 | View Code Duplication | public static function addInstanceToPool($obj, $key = null) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Removes an object from the instance pool. |
||
| 181 | * |
||
| 182 | * Propel keeps cached copies of objects in an instance pool when they are retrieved |
||
| 183 | * from the database. In some cases -- especially when you override doDelete |
||
| 184 | * methods in your stub classes -- you may need to explicitly remove objects |
||
| 185 | * from the cache in order to prevent returning objects that no longer exist. |
||
| 186 | * |
||
| 187 | * @param mixed $value A \xbanners\models\BannersI18n object or a primary key value. |
||
| 188 | */ |
||
| 189 | public static function removeInstanceFromPool($value) |
||
| 190 | { |
||
| 191 | if (Propel::isInstancePoolingEnabled() && null !== $value) { |
||
| 192 | if (is_object($value) && $value instanceof \xbanners\models\BannersI18n) { |
||
| 193 | $key = serialize([(null === $value->getId() || is_scalar($value->getId()) || is_callable([$value->getId(), '__toString']) ? (string) $value->getId() : $value->getId()), (null === $value->getLocale() || is_scalar($value->getLocale()) || is_callable([$value->getLocale(), '__toString']) ? (string) $value->getLocale() : $value->getLocale())]); |
||
| 194 | |||
| 195 | } elseif (is_array($value) && count($value) === 2) { |
||
| 196 | // assume we've been passed a primary key"; |
||
| 197 | $key = serialize([(null === $value[0] || is_scalar($value[0]) || is_callable([$value[0], '__toString']) ? (string) $value[0] : $value[0]), (null === $value[1] || is_scalar($value[1]) || is_callable([$value[1], '__toString']) ? (string) $value[1] : $value[1])]); |
||
| 198 | } elseif ($value instanceof Criteria) { |
||
| 199 | self::$instances = []; |
||
| 200 | |||
| 201 | return; |
||
| 202 | } else { |
||
| 203 | $e = new PropelException("Invalid value passed to removeInstanceFromPool(). Expected primary key or \xbanners\models\BannersI18n object; got " . (is_object($value) ? get_class($value) . ' object.' : var_export($value, true))); |
||
| 204 | throw $e; |
||
| 205 | } |
||
| 206 | |||
| 207 | unset(self::$instances[$key]); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * 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. |
||
| 213 | * |
||
| 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, a serialize()d version of the primary key 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 string The primary key hash of the row |
||
| 223 | */ |
||
| 224 | public static function getPrimaryKeyHashFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
| 225 | { |
||
| 226 | // If the PK cannot be derived from the row, return NULL. |
||
| 227 | if ($row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)] === null && $row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)] === null) { |
||
| 228 | return null; |
||
| 229 | } |
||
| 230 | |||
| 231 | return serialize([(null === $row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)] || is_scalar($row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)]) || is_callable([$row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)], '__toString']) ? (string) $row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)] : $row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)]), (null === $row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)] || is_scalar($row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)]) || is_callable([$row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)], '__toString']) ? (string) $row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)] : $row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)])]); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Retrieves the primary key from the DB resultset row |
||
| 236 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
| 237 | * a multi-column primary key, an array of the primary key columns will be returned. |
||
| 238 | * |
||
| 239 | * @param array $row resultset row. |
||
| 240 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
| 241 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
| 242 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
| 243 | * |
||
| 244 | * @return mixed The primary key of the row |
||
| 245 | */ |
||
| 246 | public static function getPrimaryKeyFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
| 247 | { |
||
| 248 | $pks = []; |
||
| 249 | |||
| 250 | $pks[] = (int) $row[ |
||
| 251 | $indexType == TableMap::TYPE_NUM |
||
| 252 | ? 0 + $offset |
||
| 253 | : self::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType) |
||
| 254 | ]; |
||
| 255 | $pks[] = (string) $row[ |
||
| 256 | $indexType == TableMap::TYPE_NUM |
||
| 257 | ? 1 + $offset |
||
| 258 | : self::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType) |
||
| 259 | ]; |
||
| 260 | |||
| 261 | return $pks; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * The class that the tableMap will make instances of. |
||
| 266 | * |
||
| 267 | * If $withPrefix is true, the returned path |
||
| 268 | * uses a dot-path notation which is translated into a path |
||
| 269 | * relative to a location on the PHP include_path. |
||
| 270 | * (e.g. path.to.MyClass -> 'path/to/MyClass.php') |
||
| 271 | * |
||
| 272 | * @param boolean $withPrefix Whether or not to return the path with the class name |
||
| 273 | * @return string path.to.ClassName |
||
| 274 | */ |
||
| 275 | public static function getOMClass($withPrefix = true) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Populates an object of the default type or an object that inherit from the default. |
||
| 282 | * |
||
| 283 | * @param array $row row returned by DataFetcher->fetch(). |
||
| 284 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
| 285 | * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). |
||
| 286 | One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
| 287 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
| 288 | * |
||
| 289 | * @throws PropelException Any exceptions caught during processing will be |
||
| 290 | * rethrown wrapped into a PropelException. |
||
| 291 | * @return array (BannersI18n object, last column rank) |
||
| 292 | */ |
||
| 293 | public static function populateObject($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
| 294 | { |
||
| 295 | $key = BannersI18nTableMap::getPrimaryKeyHashFromRow($row, $offset, $indexType); |
||
| 296 | if (null !== ($obj = BannersI18nTableMap::getInstanceFromPool($key))) { |
||
| 297 | // We no longer rehydrate the object, since this can cause data loss. |
||
| 298 | // See http://www.propelorm.org/ticket/509 |
||
| 299 | // $obj->hydrate($row, $offset, true); // rehydrate |
||
| 300 | $col = $offset + BannersI18nTableMap::NUM_HYDRATE_COLUMNS; |
||
| 301 | } else { |
||
| 302 | $cls = BannersI18nTableMap::OM_CLASS; |
||
| 303 | /** @var BannersI18n $obj */ |
||
| 304 | $obj = new $cls(); |
||
| 305 | $col = $obj->hydrate($row, $offset, false, $indexType); |
||
| 306 | BannersI18nTableMap::addInstanceToPool($obj, $key); |
||
| 307 | } |
||
| 308 | |||
| 309 | return array($obj, $col); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * The returned array will contain objects of the default type or |
||
| 314 | * objects that inherit from the default. |
||
| 315 | * |
||
| 316 | * @param DataFetcherInterface $dataFetcher |
||
| 317 | * @return array |
||
| 318 | * @throws PropelException Any exceptions caught during processing will be |
||
| 319 | * rethrown wrapped into a PropelException. |
||
| 320 | */ |
||
| 321 | public static function populateObjects(DataFetcherInterface $dataFetcher) |
||
| 322 | { |
||
| 323 | $results = array(); |
||
| 324 | |||
| 325 | // set the class once to avoid overhead in the loop |
||
| 326 | $cls = static::getOMClass(false); |
||
| 327 | // populate the object(s) |
||
| 328 | while ($row = $dataFetcher->fetch()) { |
||
| 329 | $key = BannersI18nTableMap::getPrimaryKeyHashFromRow($row, 0, $dataFetcher->getIndexType()); |
||
| 330 | if (null !== ($obj = BannersI18nTableMap::getInstanceFromPool($key))) { |
||
| 331 | // We no longer rehydrate the object, since this can cause data loss. |
||
| 332 | // See http://www.propelorm.org/ticket/509 |
||
| 333 | // $obj->hydrate($row, 0, true); // rehydrate |
||
| 334 | $results[] = $obj; |
||
| 335 | } else { |
||
| 336 | /** @var BannersI18n $obj */ |
||
| 337 | $obj = new $cls(); |
||
| 338 | $obj->hydrate($row); |
||
| 339 | $results[] = $obj; |
||
| 340 | BannersI18nTableMap::addInstanceToPool($obj, $key); |
||
| 341 | } // if key exists |
||
| 342 | } |
||
| 343 | |||
| 344 | return $results; |
||
| 345 | } |
||
| 346 | /** |
||
| 347 | * Add all the columns needed to create a new object. |
||
| 348 | * |
||
| 349 | * Note: any columns that were marked with lazyLoad="true" in the |
||
| 350 | * XML schema will not be added to the select list and only loaded |
||
| 351 | * on demand. |
||
| 352 | * |
||
| 353 | * @param Criteria $criteria object containing the columns to add. |
||
| 354 | * @param string $alias optional table alias |
||
| 355 | * @throws PropelException Any exceptions caught during processing will be |
||
| 356 | * rethrown wrapped into a PropelException. |
||
| 357 | */ |
||
| 358 | public static function addSelectColumns(Criteria $criteria, $alias = null) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the TableMap related to this object. |
||
| 373 | * This method is not needed for general use but a specific application could have a need. |
||
| 374 | * @return TableMap |
||
| 375 | * @throws PropelException Any exceptions caught during processing will be |
||
| 376 | * rethrown wrapped into a PropelException. |
||
| 377 | */ |
||
| 378 | public static function getTableMap() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Add a TableMap instance to the database for this tableMap class. |
||
| 385 | */ |
||
| 386 | View Code Duplication | public static function buildTableMap() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Performs a DELETE on the database, given a BannersI18n or Criteria object OR a primary key value. |
||
| 396 | * |
||
| 397 | * @param mixed $values Criteria or BannersI18n object or primary key or array of primary keys |
||
| 398 | * which is used to create the DELETE statement |
||
| 399 | * @param ConnectionInterface $con the connection to use |
||
| 400 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
| 401 | * if supported by native driver or if emulated using Propel. |
||
| 402 | * @throws PropelException Any exceptions caught during processing will be |
||
| 403 | * rethrown wrapped into a PropelException. |
||
| 404 | */ |
||
| 405 | public static function doDelete($values, ConnectionInterface $con = null) |
||
| 406 | { |
||
| 407 | if (null === $con) { |
||
| 408 | $con = Propel::getServiceContainer()->getWriteConnection(BannersI18nTableMap::DATABASE_NAME); |
||
| 409 | } |
||
| 410 | |||
| 411 | if ($values instanceof Criteria) { |
||
| 412 | // rename for clarity |
||
| 413 | $criteria = $values; |
||
| 414 | } elseif ($values instanceof \xbanners\models\BannersI18n) { // it's a model object |
||
| 415 | // create criteria based on pk values |
||
| 416 | $criteria = $values->buildPkeyCriteria(); |
||
| 417 | } else { // it's a primary key, or an array of pks |
||
| 418 | $criteria = new Criteria(BannersI18nTableMap::DATABASE_NAME); |
||
| 419 | // primary key is composite; we therefore, expect |
||
| 420 | // the primary key passed to be an array of pkey values |
||
| 421 | if (count($values) == count($values, COUNT_RECURSIVE)) { |
||
| 422 | // array is not multi-dimensional |
||
| 423 | $values = array($values); |
||
| 424 | } |
||
| 425 | foreach ($values as $value) { |
||
| 426 | $criterion = $criteria->getNewCriterion(BannersI18nTableMap::COL_ID, $value[0]); |
||
| 427 | $criterion->addAnd($criteria->getNewCriterion(BannersI18nTableMap::COL_LOCALE, $value[1])); |
||
| 428 | $criteria->addOr($criterion); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | $query = BannersI18nQuery::create()->mergeWith($criteria); |
||
| 433 | |||
| 434 | if ($values instanceof Criteria) { |
||
| 435 | BannersI18nTableMap::clearInstancePool(); |
||
| 436 | } elseif (!is_object($values)) { // it's a primary key, or an array of pks |
||
| 437 | foreach ((array) $values as $singleval) { |
||
| 438 | BannersI18nTableMap::removeInstanceFromPool($singleval); |
||
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | return $query->delete($con); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Deletes all rows from the banners_i18n table. |
||
| 447 | * |
||
| 448 | * @param ConnectionInterface $con the connection to use |
||
| 449 | * @return int The number of affected rows (if supported by underlying database driver). |
||
| 450 | */ |
||
| 451 | public static function doDeleteAll(ConnectionInterface $con = null) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Performs an INSERT on the database, given a BannersI18n or Criteria object. |
||
| 458 | * |
||
| 459 | * @param mixed $criteria Criteria or BannersI18n object containing data that is used to create the INSERT statement. |
||
| 460 | * @param ConnectionInterface $con the ConnectionInterface connection to use |
||
| 461 | * @return mixed The new primary key. |
||
| 462 | * @throws PropelException Any exceptions caught during processing will be |
||
| 463 | * rethrown wrapped into a PropelException. |
||
| 464 | */ |
||
| 465 | public static function doInsert($criteria, ConnectionInterface $con = null) |
||
| 487 | |||
| 488 | } // BannersI18nTableMap |
||
| 489 | // This is the static code needed to register the TableMap for this table with the main Propel class. |
||
| 492 |