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 |
||
29 | class TaskTableMap extends TableMap |
||
30 | { |
||
31 | use InstancePoolTrait; |
||
32 | use TableMapTrait; |
||
33 | |||
34 | /** |
||
35 | * The (dot-path) name of this class |
||
36 | */ |
||
37 | const CLASS_NAME = 'Models.Task.Map.TaskTableMap'; |
||
38 | |||
39 | /** |
||
40 | * The default database name for this class |
||
41 | */ |
||
42 | const DATABASE_NAME = 'default'; |
||
43 | |||
44 | /** |
||
45 | * The table name for this class |
||
46 | */ |
||
47 | const TABLE_NAME = 'tasks'; |
||
48 | |||
49 | /** |
||
50 | * The related Propel class for this table |
||
51 | */ |
||
52 | const OM_CLASS = '\\Core\\Models\\Task\\Task'; |
||
53 | |||
54 | /** |
||
55 | * A class that can be returned by this tableMap |
||
56 | */ |
||
57 | const CLASS_DEFAULT = 'Models.Task.Task'; |
||
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 = 'tasks.id'; |
||
78 | |||
79 | /** |
||
80 | * the column name for the created_at field |
||
81 | */ |
||
82 | const COL_CREATED_AT = 'tasks.created_at'; |
||
83 | |||
84 | /** |
||
85 | * the column name for the updated_at field |
||
86 | */ |
||
87 | const COL_UPDATED_AT = 'tasks.updated_at'; |
||
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', 'CreatedAt', 'UpdatedAt', ), |
||
102 | self::TYPE_CAMELNAME => array('id', 'createdAt', 'updatedAt', ), |
||
103 | self::TYPE_COLNAME => array(TaskTableMap::COL_ID, TaskTableMap::COL_CREATED_AT, TaskTableMap::COL_UPDATED_AT, ), |
||
104 | self::TYPE_FIELDNAME => array('id', 'created_at', 'updated_at', ), |
||
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, 'CreatedAt' => 1, 'UpdatedAt' => 2, ), |
||
116 | self::TYPE_CAMELNAME => array('id' => 0, 'createdAt' => 1, 'updatedAt' => 2, ), |
||
117 | self::TYPE_COLNAME => array(TaskTableMap::COL_ID => 0, TaskTableMap::COL_CREATED_AT => 1, TaskTableMap::COL_UPDATED_AT => 2, ), |
||
118 | self::TYPE_FIELDNAME => array('id' => 0, 'created_at' => 1, 'updated_at' => 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 | public function buildRelations() |
||
150 | |||
151 | /** |
||
152 | * 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. |
||
153 | * |
||
154 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
155 | * a multi-column primary key, a serialize()d version of the primary key will be returned. |
||
156 | * |
||
157 | * @param array $row resultset row. |
||
158 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
159 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
160 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
161 | * |
||
162 | * @return string The primary key hash of the row |
||
163 | */ |
||
164 | View Code Duplication | public static function getPrimaryKeyHashFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
|
173 | |||
174 | /** |
||
175 | * Retrieves the primary key from the DB resultset row |
||
176 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
177 | * a multi-column primary key, an array of the primary key columns will be returned. |
||
178 | * |
||
179 | * @param array $row resultset row. |
||
180 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
181 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
182 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
183 | * |
||
184 | * @return mixed The primary key of the row |
||
185 | */ |
||
186 | View Code Duplication | public static function getPrimaryKeyFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
|
194 | |||
195 | /** |
||
196 | * The class that the tableMap will make instances of. |
||
197 | * |
||
198 | * If $withPrefix is true, the returned path |
||
199 | * uses a dot-path notation which is translated into a path |
||
200 | * relative to a location on the PHP include_path. |
||
201 | * (e.g. path.to.MyClass -> 'path/to/MyClass.php') |
||
202 | * |
||
203 | * @param boolean $withPrefix Whether or not to return the path with the class name |
||
204 | * @return string path.to.ClassName |
||
205 | */ |
||
206 | public static function getOMClass($withPrefix = true) |
||
210 | |||
211 | /** |
||
212 | * Populates an object of the default type or an object that inherit from the default. |
||
213 | * |
||
214 | * @param array $row row returned by DataFetcher->fetch(). |
||
215 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
216 | * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). |
||
217 | One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
218 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
219 | * |
||
220 | * @throws PropelException Any exceptions caught during processing will be |
||
221 | * rethrown wrapped into a PropelException. |
||
222 | * @return array (Task object, last column rank) |
||
223 | */ |
||
224 | View Code Duplication | public static function populateObject($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
|
242 | |||
243 | /** |
||
244 | * The returned array will contain objects of the default type or |
||
245 | * objects that inherit from the default. |
||
246 | * |
||
247 | * @param DataFetcherInterface $dataFetcher |
||
248 | * @return array |
||
249 | * @throws PropelException Any exceptions caught during processing will be |
||
250 | * rethrown wrapped into a PropelException. |
||
251 | */ |
||
252 | View Code Duplication | public static function populateObjects(DataFetcherInterface $dataFetcher) |
|
277 | /** |
||
278 | * Add all the columns needed to create a new object. |
||
279 | * |
||
280 | * Note: any columns that were marked with lazyLoad="true" in the |
||
281 | * XML schema will not be added to the select list and only loaded |
||
282 | * on demand. |
||
283 | * |
||
284 | * @param Criteria $criteria object containing the columns to add. |
||
285 | * @param string $alias optional table alias |
||
286 | * @throws PropelException Any exceptions caught during processing will be |
||
287 | * rethrown wrapped into a PropelException. |
||
288 | */ |
||
289 | public static function addSelectColumns(Criteria $criteria, $alias = null) |
||
301 | |||
302 | /** |
||
303 | * Returns the TableMap related to this object. |
||
304 | * This method is not needed for general use but a specific application could have a need. |
||
305 | * @return TableMap |
||
306 | * @throws PropelException Any exceptions caught during processing will be |
||
307 | * rethrown wrapped into a PropelException. |
||
308 | */ |
||
309 | public static function getTableMap() |
||
313 | |||
314 | /** |
||
315 | * Add a TableMap instance to the database for this tableMap class. |
||
316 | */ |
||
317 | View Code Duplication | public static function buildTableMap() |
|
324 | |||
325 | /** |
||
326 | * Performs a DELETE on the database, given a Task or Criteria object OR a primary key value. |
||
327 | * |
||
328 | * @param mixed $values Criteria or Task object or primary key or array of primary keys |
||
329 | * which is used to create the DELETE statement |
||
330 | * @param ConnectionInterface $con the connection to use |
||
331 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
332 | * if supported by native driver or if emulated using Propel. |
||
333 | * @throws PropelException Any exceptions caught during processing will be |
||
334 | * rethrown wrapped into a PropelException. |
||
335 | */ |
||
336 | View Code Duplication | public static function doDelete($values, ConnectionInterface $con = null) |
|
365 | |||
366 | /** |
||
367 | * Deletes all rows from the tasks table. |
||
368 | * |
||
369 | * @param ConnectionInterface $con the connection to use |
||
370 | * @return int The number of affected rows (if supported by underlying database driver). |
||
371 | */ |
||
372 | public static function doDeleteAll(ConnectionInterface $con = null) |
||
376 | |||
377 | /** |
||
378 | * Performs an INSERT on the database, given a Task or Criteria object. |
||
379 | * |
||
380 | * @param mixed $criteria Criteria or Task object containing data that is used to create the INSERT statement. |
||
381 | * @param ConnectionInterface $con the ConnectionInterface connection to use |
||
382 | * @return mixed The new primary key. |
||
383 | * @throws PropelException Any exceptions caught during processing will be |
||
384 | * rethrown wrapped into a PropelException. |
||
385 | */ |
||
386 | View Code Duplication | public static function doInsert($criteria, ConnectionInterface $con = null) |
|
412 | |||
413 | } // TaskTableMap |
||
414 | // This is the static code needed to register the TableMap for this table with the main Propel class. |
||
417 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.