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 TDBMService 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 TDBMService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class TDBMService |
||
49 | { |
||
50 | const MODE_CURSOR = 1; |
||
51 | const MODE_ARRAY = 2; |
||
52 | |||
53 | /** |
||
54 | * The database connection. |
||
55 | * |
||
56 | * @var Connection |
||
57 | */ |
||
58 | private $connection; |
||
59 | |||
60 | /** |
||
61 | * @var SchemaAnalyzer |
||
62 | */ |
||
63 | private $schemaAnalyzer; |
||
64 | |||
65 | /** |
||
66 | * @var MagicQuery |
||
67 | */ |
||
68 | private $magicQuery; |
||
69 | |||
70 | /** |
||
71 | * @var TDBMSchemaAnalyzer |
||
72 | */ |
||
73 | private $tdbmSchemaAnalyzer; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | private $cachePrefix; |
||
79 | |||
80 | /** |
||
81 | * Cache of table of primary keys. |
||
82 | * Primary keys are stored by tables, as an array of column. |
||
83 | * For instance $primary_key['my_table'][0] will return the first column of the primary key of table 'my_table'. |
||
84 | * |
||
85 | * @var string[] |
||
86 | */ |
||
87 | private $primaryKeysColumns; |
||
88 | |||
89 | /** |
||
90 | * Service storing objects in memory. |
||
91 | * Access is done by table name and then by primary key. |
||
92 | * If the primary key is split on several columns, access is done by an array of columns, serialized. |
||
93 | * |
||
94 | * @var StandardObjectStorage|WeakrefObjectStorage |
||
95 | */ |
||
96 | private $objectStorage; |
||
97 | |||
98 | /** |
||
99 | * The fetch mode of the result sets returned by `getObjects`. |
||
100 | * Can be one of: TDBMObjectArray::MODE_CURSOR or TDBMObjectArray::MODE_ARRAY or TDBMObjectArray::MODE_COMPATIBLE_ARRAY. |
||
101 | * |
||
102 | * In 'MODE_ARRAY' mode (default), the result is an array. Use this mode by default (unless the list returned is very big). |
||
103 | * In 'MODE_CURSOR' mode, the result is a Generator which is an iterable collection that can be scanned only once (only one "foreach") on it, |
||
104 | * and it cannot be accessed via key. Use this mode for large datasets processed by batch. |
||
105 | * In 'MODE_COMPATIBLE_ARRAY' mode, the result is an old TDBMObjectArray (used up to TDBM 3.2). |
||
106 | * You can access the array by key, or using foreach, several times. |
||
107 | * |
||
108 | * @var int |
||
109 | */ |
||
110 | private $mode = self::MODE_ARRAY; |
||
111 | |||
112 | /** |
||
113 | * Table of new objects not yet inserted in database or objects modified that must be saved. |
||
114 | * |
||
115 | * @var \SplObjectStorage of DbRow objects |
||
116 | */ |
||
117 | private $toSaveObjects; |
||
118 | |||
119 | /** |
||
120 | * A cache service to be used. |
||
121 | * |
||
122 | * @var Cache|null |
||
123 | */ |
||
124 | private $cache; |
||
125 | |||
126 | /** |
||
127 | * Map associating a table name to a fully qualified Bean class name. |
||
128 | * |
||
129 | * @var array |
||
130 | */ |
||
131 | private $tableToBeanMap = []; |
||
132 | |||
133 | /** |
||
134 | * @var \ReflectionClass[] |
||
135 | */ |
||
136 | private $reflectionClassCache = array(); |
||
137 | |||
138 | /** |
||
139 | * @var LoggerInterface |
||
140 | */ |
||
141 | private $rootLogger; |
||
142 | |||
143 | /** |
||
144 | * @var LevelFilter|NullLogger |
||
145 | */ |
||
146 | private $logger; |
||
147 | |||
148 | /** |
||
149 | * @var OrderByAnalyzer |
||
150 | */ |
||
151 | private $orderByAnalyzer; |
||
152 | |||
153 | /** |
||
154 | * @param Connection $connection The DBAL DB connection to use |
||
155 | * @param Cache|null $cache A cache service to be used |
||
156 | * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
||
157 | * Will be automatically created if not passed |
||
158 | */ |
||
159 | public function __construct(Connection $connection, Cache $cache = null, SchemaAnalyzer $schemaAnalyzer = null, LoggerInterface $logger = null) |
||
193 | |||
194 | /** |
||
195 | * Returns the object used to connect to the database. |
||
196 | * |
||
197 | * @return Connection |
||
198 | */ |
||
199 | public function getConnection() |
||
203 | |||
204 | /** |
||
205 | * Creates a unique cache key for the current connection. |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | private function getConnectionUniqueId() |
||
213 | |||
214 | /** |
||
215 | * Sets the default fetch mode of the result sets returned by `findObjects`. |
||
216 | * Can be one of: TDBMObjectArray::MODE_CURSOR or TDBMObjectArray::MODE_ARRAY. |
||
217 | * |
||
218 | * In 'MODE_ARRAY' mode (default), the result is a ResultIterator object that behaves like an array. Use this mode by default (unless the list returned is very big). |
||
219 | * In 'MODE_CURSOR' mode, the result is a ResultIterator object. If you scan it many times (by calling several time a foreach loop), the query will be run |
||
220 | * several times. In cursor mode, you cannot access the result set by key. Use this mode for large datasets processed by batch. |
||
221 | * |
||
222 | * @param int $mode |
||
223 | * |
||
224 | * @return $this |
||
225 | * |
||
226 | * @throws TDBMException |
||
227 | */ |
||
228 | public function setFetchMode($mode) |
||
237 | |||
238 | /** |
||
239 | * Returns a TDBMObject associated from table "$table_name". |
||
240 | * If the $filters parameter is an int/string, the object returned will be the object whose primary key = $filters. |
||
241 | * $filters can also be a set of TDBM_Filters (see the findObjects method for more details). |
||
242 | * |
||
243 | * For instance, if there is a table 'users', with a primary key on column 'user_id' and a column 'user_name', then |
||
244 | * $user = $tdbmService->getObject('users',1); |
||
245 | * echo $user->name; |
||
246 | * will return the name of the user whose user_id is one. |
||
247 | * |
||
248 | * If a table has a primary key over several columns, you should pass to $id an array containing the the value of the various columns. |
||
249 | * For instance: |
||
250 | * $group = $tdbmService->getObject('groups',array(1,2)); |
||
251 | * |
||
252 | * Note that TDBMObject performs caching for you. If you get twice the same object, the reference of the object you will get |
||
253 | * will be the same. |
||
254 | * |
||
255 | * For instance: |
||
256 | * $user1 = $tdbmService->getObject('users',1); |
||
257 | * $user2 = $tdbmService->getObject('users',1); |
||
258 | * $user1->name = 'John Doe'; |
||
259 | * echo $user2->name; |
||
260 | * will return 'John Doe'. |
||
261 | * |
||
262 | * You can use filters instead of passing the primary key. For instance: |
||
263 | * $user = $tdbmService->getObject('users',new EqualFilter('users', 'login', 'jdoe')); |
||
264 | * This will return the user whose login is 'jdoe'. |
||
265 | * Please note that if 2 users have the jdoe login in database, the method will throw a TDBM_DuplicateRowException. |
||
266 | * |
||
267 | * Also, you can specify the return class for the object (provided the return class extends TDBMObject). |
||
268 | * For instance: |
||
269 | * $user = $tdbmService->getObject('users',1,'User'); |
||
270 | * will return an object from the "User" class. The "User" class must extend the "TDBMObject" class. |
||
271 | * Please be sure not to override any method or any property unless you perfectly know what you are doing! |
||
272 | * |
||
273 | * @param string $table_name The name of the table we retrieve an object from |
||
274 | * @param mixed $filters If the filter is a string/integer, it will be considered as the id of the object (the value of the primary key). Otherwise, it can be a filter bag (see the filterbag parameter of the findObjects method for more details about filter bags) |
||
275 | * @param string $className Optional: The name of the class to instanciate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned |
||
276 | * @param bool $lazy_loading If set to true, and if the primary key is passed in parameter of getObject, the object will not be queried in database. It will be queried when you first try to access a column. If at that time the object cannot be found in database, an exception will be thrown |
||
277 | * |
||
278 | * @return TDBMObject |
||
279 | */ |
||
280 | /* public function getObject($table_name, $filters, $className = null, $lazy_loading = false) { |
||
281 | |||
282 | if (is_array($filters) || $filters instanceof FilterInterface) { |
||
283 | $isFilterBag = false; |
||
284 | if (is_array($filters)) { |
||
285 | // Is this a multiple primary key or a filter bag? |
||
286 | // Let's have a look at the first item of the array to decide. |
||
287 | foreach ($filters as $filter) { |
||
288 | if (is_array($filter) || $filter instanceof FilterInterface) { |
||
289 | $isFilterBag = true; |
||
290 | } |
||
291 | break; |
||
292 | } |
||
293 | } else { |
||
294 | $isFilterBag = true; |
||
295 | } |
||
296 | |||
297 | if ($isFilterBag == true) { |
||
298 | // If a filter bag was passer in parameter, let's perform a findObjects. |
||
299 | $objects = $this->findObjects($table_name, $filters, null, null, null, $className); |
||
300 | if (count($objects) == 0) { |
||
301 | return null; |
||
302 | } elseif (count($objects) > 1) { |
||
303 | throw new DuplicateRowException("Error while querying an object for table '$table_name': ".count($objects)." rows have been returned, but we should have received at most one."); |
||
304 | } |
||
305 | // Return the first and only object. |
||
306 | if ($objects instanceof \Generator) { |
||
307 | return $objects->current(); |
||
308 | } else { |
||
309 | return $objects[0]; |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | $id = $filters; |
||
314 | if ($this->connection == null) { |
||
315 | throw new TDBMException("Error while calling TdbmService->getObject(): No connection has been established on the database!"); |
||
316 | } |
||
317 | $table_name = $this->connection->toStandardcase($table_name); |
||
318 | |||
319 | // If the ID is null, let's throw an exception |
||
320 | if ($id === null) { |
||
321 | throw new TDBMException("The ID you passed to TdbmService->getObject is null for the object of type '$table_name'. Objects primary keys cannot be null."); |
||
322 | } |
||
323 | |||
324 | // If the primary key is split over many columns, the IDs are passed in an array. Let's serialize this array to store it. |
||
325 | if (is_array($id)) { |
||
326 | $id = serialize($id); |
||
327 | } |
||
328 | |||
329 | if ($className === null) { |
||
330 | if (isset($this->tableToBeanMap[$table_name])) { |
||
331 | $className = $this->tableToBeanMap[$table_name]; |
||
332 | } else { |
||
333 | $className = "Mouf\\Database\\TDBM\\TDBMObject"; |
||
334 | } |
||
335 | } |
||
336 | |||
337 | if ($this->objectStorage->has($table_name, $id)) { |
||
338 | $obj = $this->objectStorage->get($table_name, $id); |
||
339 | if (is_a($obj, $className)) { |
||
340 | return $obj; |
||
341 | } else { |
||
342 | throw new TDBMException("Error! The object with ID '$id' for table '$table_name' has already been retrieved. The type for this object is '".get_class($obj)."'' which is not a subtype of '$className'"); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | if ($className != "Mouf\\Database\\TDBM\\TDBMObject" && !is_subclass_of($className, "Mouf\\Database\\TDBM\\TDBMObject")) { |
||
347 | if (!class_exists($className)) { |
||
348 | throw new TDBMException("Error while calling TDBMService->getObject: The class ".$className." does not exist."); |
||
349 | } else { |
||
350 | throw new TDBMException("Error while calling TDBMService->getObject: The class ".$className." should extend TDBMObject."); |
||
351 | } |
||
352 | } |
||
353 | $obj = new $className($this, $table_name, $id); |
||
354 | |||
355 | if ($lazy_loading == false) { |
||
356 | // If we are not doing lazy loading, let's load the object: |
||
357 | $obj->_dbLoadIfNotLoaded(); |
||
358 | } |
||
359 | |||
360 | $this->objectStorage->set($table_name, $id, $obj); |
||
361 | |||
362 | return $obj; |
||
363 | }*/ |
||
364 | |||
365 | /** |
||
366 | * Removes the given object from database. |
||
367 | * This cannot be called on an object that is not attached to this TDBMService |
||
368 | * (will throw a TDBMInvalidOperationException). |
||
369 | * |
||
370 | * @param AbstractTDBMObject $object the object to delete |
||
371 | * |
||
372 | * @throws TDBMException |
||
373 | * @throws TDBMInvalidOperationException |
||
374 | */ |
||
375 | public function delete(AbstractTDBMObject $object) |
||
413 | |||
414 | /** |
||
415 | * Removes all many to many relationships for this object. |
||
416 | * |
||
417 | * @param AbstractTDBMObject $object |
||
418 | */ |
||
419 | private function deleteManyToManyRelationships(AbstractTDBMObject $object) |
||
432 | |||
433 | /** |
||
434 | * This function removes the given object from the database. It will also remove all objects relied to the one given |
||
435 | * by parameter before all. |
||
436 | * |
||
437 | * Notice: if the object has a multiple primary key, the function will not work. |
||
438 | * |
||
439 | * @param AbstractTDBMObject $objToDelete |
||
440 | */ |
||
441 | public function deleteCascade(AbstractTDBMObject $objToDelete) |
||
446 | |||
447 | /** |
||
448 | * This function is used only in TDBMService (private function) |
||
449 | * It will call deleteCascade function foreach object relied with a foreign key to the object given by parameter. |
||
450 | * |
||
451 | * @param AbstractTDBMObject $obj |
||
452 | */ |
||
453 | private function deleteAllConstraintWithThisObject(AbstractTDBMObject $obj) |
||
474 | |||
475 | /** |
||
476 | * This function performs a save() of all the objects that have been modified. |
||
477 | */ |
||
478 | public function completeSave() |
||
484 | |||
485 | /** |
||
486 | * Takes in input a filter_bag (which can be about anything from a string to an array of TDBMObjects... see above from documentation), |
||
487 | * and gives back a proper Filter object. |
||
488 | * |
||
489 | * @param mixed $filter_bag |
||
490 | * @param int $counter |
||
491 | * |
||
492 | * @return array First item: filter string, second item: parameters |
||
493 | * |
||
494 | * @throws TDBMException |
||
495 | */ |
||
496 | public function buildFilterFromFilterBag($filter_bag, $counter = 1) |
||
544 | |||
545 | /** |
||
546 | * @param string $table |
||
547 | * |
||
548 | * @return string[] |
||
549 | */ |
||
550 | public function getPrimaryKeyColumns($table) |
||
582 | |||
583 | /** |
||
584 | * This is an internal function, you should not use it in your application. |
||
585 | * This is used internally by TDBM to add an object to the object cache. |
||
586 | * |
||
587 | * @param DbRow $dbRow |
||
588 | */ |
||
589 | public function _addToCache(DbRow $dbRow) |
||
595 | |||
596 | /** |
||
597 | * This is an internal function, you should not use it in your application. |
||
598 | * This is used internally by TDBM to remove the object from the list of objects that have been |
||
599 | * created/updated but not saved yet. |
||
600 | * |
||
601 | * @param DbRow $myObject |
||
602 | */ |
||
603 | private function removeFromToSaveObjectList(DbRow $myObject) |
||
607 | |||
608 | /** |
||
609 | * This is an internal function, you should not use it in your application. |
||
610 | * This is used internally by TDBM to add an object to the list of objects that have been |
||
611 | * created/updated but not saved yet. |
||
612 | * |
||
613 | * @param AbstractTDBMObject $myObject |
||
614 | */ |
||
615 | public function _addToToSaveObjectList(DbRow $myObject) |
||
619 | |||
620 | /** |
||
621 | * Generates all the daos and beans. |
||
622 | * |
||
623 | * @param string $daoFactoryClassName The classe name of the DAO factory |
||
624 | * @param string $daonamespace The namespace for the DAOs, without trailing \ |
||
625 | * @param string $beannamespace The Namespace for the beans, without trailing \ |
||
626 | * @param bool $storeInUtc If the generated daos should store the date in UTC timezone instead of user's timezone |
||
627 | * @param string $composerFile If it's set, location of custom Composer file. Relative to project root |
||
628 | * |
||
629 | * @return \string[] the list of tables |
||
630 | */ |
||
631 | public function generateAllDaosAndBeans($daoFactoryClassName, $daonamespace, $beannamespace, $storeInUtc, $composerFile = null) |
||
643 | |||
644 | /** |
||
645 | * @param array<string, string> $tableToBeanMap |
||
646 | */ |
||
647 | public function setTableToBeanMap(array $tableToBeanMap) |
||
651 | |||
652 | /** |
||
653 | * Returns the fully qualified class name of the bean associated with table $tableName. |
||
654 | * |
||
655 | * |
||
656 | * @param string $tableName |
||
657 | * |
||
658 | * @return string |
||
659 | */ |
||
660 | public function getBeanClassName(string $tableName) : string |
||
668 | |||
669 | /** |
||
670 | * Saves $object by INSERTing or UPDAT(E)ing it in the database. |
||
671 | * |
||
672 | * @param AbstractTDBMObject $object |
||
673 | * |
||
674 | * @throws TDBMException |
||
675 | */ |
||
676 | public function save(AbstractTDBMObject $object) |
||
887 | |||
888 | private function persistManyToManyRelationships(AbstractTDBMObject $object) |
||
958 | |||
959 | private function getPivotFilters(AbstractTDBMObject $localBean, AbstractTDBMObject $remoteBean, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk) |
||
971 | |||
972 | /** |
||
973 | * Returns the "values" of the primary key. |
||
974 | * This returns the primary key from the $primaryKey attribute, not the one stored in the columns. |
||
975 | * |
||
976 | * @param AbstractTDBMObject $bean |
||
977 | * |
||
978 | * @return array numerically indexed array of values |
||
979 | */ |
||
980 | private function getPrimaryKeyValues(AbstractTDBMObject $bean) |
||
987 | |||
988 | /** |
||
989 | * Returns a unique hash used to store the object based on its primary key. |
||
990 | * If the array contains only one value, then the value is returned. |
||
991 | * Otherwise, a hash representing the array is returned. |
||
992 | * |
||
993 | * @param array $primaryKeys An array of columns => values forming the primary key |
||
994 | * |
||
995 | * @return string |
||
996 | */ |
||
997 | public function getObjectHash(array $primaryKeys) |
||
1007 | |||
1008 | /** |
||
1009 | * Returns an array of primary keys from the object. |
||
1010 | * The primary keys are extracted from the object columns and not from the primary keys stored in the |
||
1011 | * $primaryKeys variable of the object. |
||
1012 | * |
||
1013 | * @param DbRow $dbRow |
||
1014 | * |
||
1015 | * @return array Returns an array of column => value |
||
1016 | */ |
||
1017 | public function getPrimaryKeysForObjectFromDbRow(DbRow $dbRow) |
||
1024 | |||
1025 | /** |
||
1026 | * Returns an array of primary keys for the given row. |
||
1027 | * The primary keys are extracted from the object columns. |
||
1028 | * |
||
1029 | * @param $table |
||
1030 | * @param array $columns |
||
1031 | * |
||
1032 | * @return array |
||
1033 | */ |
||
1034 | public function _getPrimaryKeysFromObjectData($table, array $columns) |
||
1046 | |||
1047 | /** |
||
1048 | * Attaches $object to this TDBMService. |
||
1049 | * The $object must be in DETACHED state and will pass in NEW state. |
||
1050 | * |
||
1051 | * @param AbstractTDBMObject $object |
||
1052 | * |
||
1053 | * @throws TDBMInvalidOperationException |
||
1054 | */ |
||
1055 | public function attach(AbstractTDBMObject $object) |
||
1059 | |||
1060 | /** |
||
1061 | * Returns an associative array (column => value) for the primary keys from the table name and an |
||
1062 | * indexed array of primary key values. |
||
1063 | * |
||
1064 | * @param string $tableName |
||
1065 | * @param array $indexedPrimaryKeys |
||
1066 | */ |
||
1067 | public function _getPrimaryKeysFromIndexedPrimaryKeys($tableName, array $indexedPrimaryKeys) |
||
1078 | |||
1079 | /** |
||
1080 | * Return the list of tables (from child to parent) joining the tables passed in parameter. |
||
1081 | * Tables must be in a single line of inheritance. The method will find missing tables. |
||
1082 | * |
||
1083 | * Algorithm: one of those tables is the ultimate child. From this child, by recursively getting the parent, |
||
1084 | * we must be able to find all other tables. |
||
1085 | * |
||
1086 | * @param string[] $tables |
||
1087 | * |
||
1088 | * @return string[] |
||
1089 | */ |
||
1090 | public function _getLinkBetweenInheritedTables(array $tables) |
||
1099 | |||
1100 | /** |
||
1101 | * Return the list of tables (from child to parent) joining the tables passed in parameter. |
||
1102 | * Tables must be in a single line of inheritance. The method will find missing tables. |
||
1103 | * |
||
1104 | * Algorithm: one of those tables is the ultimate child. From this child, by recursively getting the parent, |
||
1105 | * we must be able to find all other tables. |
||
1106 | * |
||
1107 | * @param string[] $tables |
||
1108 | * |
||
1109 | * @return string[] |
||
1110 | */ |
||
1111 | private function _getLinkBetweenInheritedTablesWithoutCache(array $tables) |
||
1132 | |||
1133 | /** |
||
1134 | * Returns the list of tables related to this table (via a parent or child inheritance relationship). |
||
1135 | * |
||
1136 | * @param string $table |
||
1137 | * |
||
1138 | * @return string[] |
||
1139 | */ |
||
1140 | public function _getRelatedTablesByInheritance($table) |
||
1146 | |||
1147 | /** |
||
1148 | * Returns the list of tables related to this table (via a parent or child inheritance relationship). |
||
1149 | * |
||
1150 | * @param string $table |
||
1151 | * |
||
1152 | * @return string[] |
||
1153 | */ |
||
1154 | private function _getRelatedTablesByInheritanceWithoutCache($table) |
||
1174 | |||
1175 | /** |
||
1176 | * Explore all the children and descendant of $table and returns ForeignKeyConstraints on those. |
||
1177 | * |
||
1178 | * @param string $table |
||
1179 | * |
||
1180 | * @return string[] |
||
1181 | */ |
||
1182 | private function exploreChildrenTablesRelationships(SchemaAnalyzer $schemaAnalyzer, $table) |
||
1193 | |||
1194 | /** |
||
1195 | * Casts a foreign key into SQL, assuming table name is used with no alias. |
||
1196 | * The returned value does contain only one table. For instance:. |
||
1197 | * |
||
1198 | * " LEFT JOIN table2 ON table1.id = table2.table1_id" |
||
1199 | * |
||
1200 | * @param ForeignKeyConstraint $fk |
||
1201 | * @param bool $leftTableIsLocal |
||
1202 | * |
||
1203 | * @return string |
||
1204 | */ |
||
1205 | /*private function foreignKeyToSql(ForeignKeyConstraint $fk, $leftTableIsLocal) { |
||
1206 | $onClauses = []; |
||
1207 | $foreignTableName = $this->connection->quoteIdentifier($fk->getForeignTableName()); |
||
1208 | $foreignColumns = $fk->getForeignColumns(); |
||
1209 | $localTableName = $this->connection->quoteIdentifier($fk->getLocalTableName()); |
||
1210 | $localColumns = $fk->getLocalColumns(); |
||
1211 | $columnCount = count($localTableName); |
||
1212 | |||
1213 | for ($i = 0; $i < $columnCount; $i++) { |
||
1214 | $onClauses[] = sprintf("%s.%s = %s.%s", |
||
1215 | $localTableName, |
||
1216 | $this->connection->quoteIdentifier($localColumns[$i]), |
||
1217 | $foreignColumns, |
||
1218 | $this->connection->quoteIdentifier($foreignColumns[$i]) |
||
1219 | ); |
||
1220 | } |
||
1221 | |||
1222 | $onClause = implode(' AND ', $onClauses); |
||
1223 | |||
1224 | if ($leftTableIsLocal) { |
||
1225 | return sprintf(" LEFT JOIN %s ON (%s)", $foreignTableName, $onClause); |
||
1226 | } else { |
||
1227 | return sprintf(" LEFT JOIN %s ON (%s)", $localTableName, $onClause); |
||
1228 | } |
||
1229 | }*/ |
||
1230 | |||
1231 | /** |
||
1232 | * Returns a `ResultIterator` object representing filtered records of "$mainTable" . |
||
1233 | * |
||
1234 | * The findObjects method should be the most used query method in TDBM if you want to query the database for objects. |
||
1235 | * (Note: if you want to query the database for an object by its primary key, use the findObjectByPk method). |
||
1236 | * |
||
1237 | * The findObjects method takes in parameter: |
||
1238 | * - mainTable: the kind of bean you want to retrieve. In TDBM, a bean matches a database row, so the |
||
1239 | * `$mainTable` parameter should be the name of an existing table in database. |
||
1240 | * - filter: The filter is a filter bag. It is what you use to filter your request (the WHERE part in SQL). |
||
1241 | * It can be a string (SQL Where clause), or even a bean or an associative array (key = column to filter, value = value to find) |
||
1242 | * - parameters: The parameters used in the filter. If you pass a SQL string as a filter, be sure to avoid |
||
1243 | * concatenating parameters in the string (this leads to SQL injection and also to poor caching performance). |
||
1244 | * Instead, please consider passing parameters (see documentation for more details). |
||
1245 | * - additionalTablesFetch: An array of SQL tables names. The beans related to those tables will be fetched along |
||
1246 | * the main table. This is useful to avoid hitting the database with numerous subqueries. |
||
1247 | * - mode: The fetch mode of the result. See `setFetchMode()` method for more details. |
||
1248 | * |
||
1249 | * The `findObjects` method will return a `ResultIterator`. A `ResultIterator` is an object that behaves as an array |
||
1250 | * (in ARRAY mode) at least. It can be iterated using a `foreach` loop. |
||
1251 | * |
||
1252 | * Finally, if filter_bag is null, the whole table is returned. |
||
1253 | * |
||
1254 | * @param string $mainTable The name of the table queried |
||
1255 | * @param string|array|null $filter The SQL filters to apply to the query (the WHERE part). Columns from tables different from $mainTable must be prefixed by the table name (in the form: table.column) |
||
1256 | * @param array $parameters |
||
1257 | * @param string|UncheckedOrderBy|null $orderString The ORDER BY part of the query. Columns from tables different from $mainTable must be prefixed by the table name (in the form: table.column) |
||
1258 | * @param array $additionalTablesFetch |
||
1259 | * @param int $mode |
||
1260 | * @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned |
||
1261 | * |
||
1262 | * @return ResultIterator An object representing an array of results |
||
1263 | * |
||
1264 | * @throws TDBMException |
||
1265 | */ |
||
1266 | public function findObjects(string $mainTable, $filter = null, array $parameters = array(), $orderString = null, array $additionalTablesFetch = array(), $mode = null, string $className = null) |
||
1283 | |||
1284 | /** |
||
1285 | * @param string $mainTable The name of the table queried |
||
1286 | * @param string $from The from sql statement |
||
1287 | * @param string|array|null $filter The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column) |
||
1288 | * @param array $parameters |
||
1289 | * @param string|UncheckedOrderBy|null $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column) |
||
1290 | * @param int $mode |
||
1291 | * @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned |
||
1292 | * |
||
1293 | * @return ResultIterator An object representing an array of results |
||
1294 | * |
||
1295 | * @throws TDBMException |
||
1296 | */ |
||
1297 | public function findObjectsFromSql(string $mainTable, string $from, $filter = null, array $parameters = array(), $orderString = null, $mode = null, string $className = null) |
||
1314 | |||
1315 | /** |
||
1316 | * @param $table |
||
1317 | * @param array $primaryKeys |
||
1318 | * @param array $additionalTablesFetch |
||
1319 | * @param bool $lazy Whether to perform lazy loading on this object or not |
||
1320 | * @param string $className |
||
1321 | * |
||
1322 | * @return AbstractTDBMObject |
||
1323 | * |
||
1324 | * @throws TDBMException |
||
1325 | */ |
||
1326 | public function findObjectByPk(string $table, array $primaryKeys, array $additionalTablesFetch = array(), bool $lazy = false, string $className = null) |
||
1367 | |||
1368 | /** |
||
1369 | * Returns a unique bean (or null) according to the filters passed in parameter. |
||
1370 | * |
||
1371 | * @param string $mainTable The name of the table queried |
||
1372 | * @param string|array|null $filter The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column) |
||
1373 | * @param array $parameters |
||
1374 | * @param array $additionalTablesFetch |
||
1375 | * @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned |
||
1376 | * |
||
1377 | * @return AbstractTDBMObject|null The object we want, or null if no object matches the filters |
||
1378 | * |
||
1379 | * @throws TDBMException |
||
1380 | */ |
||
1381 | View Code Duplication | public function findObject(string $mainTable, $filter = null, array $parameters = array(), array $additionalTablesFetch = array(), string $className = null) |
|
1394 | |||
1395 | /** |
||
1396 | * Returns a unique bean (or null) according to the filters passed in parameter. |
||
1397 | * |
||
1398 | * @param string $mainTable The name of the table queried |
||
1399 | * @param string $from The from sql statement |
||
1400 | * @param string|array|null $filter The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column) |
||
1401 | * @param array $parameters |
||
1402 | * @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned |
||
1403 | * |
||
1404 | * @return AbstractTDBMObject|null The object we want, or null if no object matches the filters |
||
1405 | * |
||
1406 | * @throws TDBMException |
||
1407 | */ |
||
1408 | View Code Duplication | public function findObjectFromSql($mainTable, $from, $filter = null, array $parameters = array(), $className = null) |
|
1421 | |||
1422 | /** |
||
1423 | * Returns a unique bean according to the filters passed in parameter. |
||
1424 | * Throws a NoBeanFoundException if no bean was found for the filter passed in parameter. |
||
1425 | * |
||
1426 | * @param string $mainTable The name of the table queried |
||
1427 | * @param string|array|null $filter The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column) |
||
1428 | * @param array $parameters |
||
1429 | * @param array $additionalTablesFetch |
||
1430 | * @param string $className Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned |
||
1431 | * |
||
1432 | * @return AbstractTDBMObject The object we want |
||
1433 | * |
||
1434 | * @throws TDBMException |
||
1435 | */ |
||
1436 | public function findObjectOrFail(string $mainTable, $filter = null, array $parameters = array(), array $additionalTablesFetch = array(), string $className = null) |
||
1445 | |||
1446 | /** |
||
1447 | * @param array $beanData An array of data: array<table, array<column, value>> |
||
1448 | * |
||
1449 | * @return array an array with first item = class name, second item = table name and third item = list of tables needed |
||
1450 | * |
||
1451 | * @throws TDBMInheritanceException |
||
1452 | */ |
||
1453 | public function _getClassNameFromBeanData(array $beanData) |
||
1490 | |||
1491 | /** |
||
1492 | * Returns an item from cache or computes it using $closure and puts it in cache. |
||
1493 | * |
||
1494 | * @param string $key |
||
1495 | * @param callable $closure |
||
1496 | * |
||
1497 | * @return mixed |
||
1498 | */ |
||
1499 | View Code Duplication | private function fromCache(string $key, callable $closure) |
|
1509 | |||
1510 | /** |
||
1511 | * Returns the foreign key object. |
||
1512 | * |
||
1513 | * @param string $table |
||
1514 | * @param string $fkName |
||
1515 | * |
||
1516 | * @return ForeignKeyConstraint |
||
1517 | */ |
||
1518 | public function _getForeignKeyByName(string $table, string $fkName) |
||
1522 | |||
1523 | /** |
||
1524 | * @param $pivotTableName |
||
1525 | * @param AbstractTDBMObject $bean |
||
1526 | * |
||
1527 | * @return AbstractTDBMObject[] |
||
1528 | */ |
||
1529 | public function _getRelatedBeans(string $pivotTableName, AbstractTDBMObject $bean) |
||
1545 | |||
1546 | /** |
||
1547 | * @param $pivotTableName |
||
1548 | * @param AbstractTDBMObject $bean The LOCAL bean |
||
1549 | * |
||
1550 | * @return ForeignKeyConstraint[] First item: the LOCAL bean, second item: the REMOTE bean |
||
1551 | * |
||
1552 | * @throws TDBMException |
||
1553 | */ |
||
1554 | private function getPivotTableForeignKeys(string $pivotTableName, AbstractTDBMObject $bean) |
||
1572 | |||
1573 | /** |
||
1574 | * Returns a list of pivot tables linked to $bean. |
||
1575 | * |
||
1576 | * @param AbstractTDBMObject $bean |
||
1577 | * |
||
1578 | * @return string[] |
||
1579 | */ |
||
1580 | public function _getPivotTablesLinkedToBean(AbstractTDBMObject $bean) |
||
1597 | |||
1598 | /** |
||
1599 | * Array of types for tables. |
||
1600 | * Key: table name |
||
1601 | * Value: array of types indexed by column. |
||
1602 | * |
||
1603 | * @var array[] |
||
1604 | */ |
||
1605 | private $typesForTable = []; |
||
1606 | |||
1607 | /** |
||
1608 | * @internal |
||
1609 | * |
||
1610 | * @param string $tableName |
||
1611 | * |
||
1612 | * @return Type[] |
||
1613 | */ |
||
1614 | public function _getColumnTypesForTable(string $tableName) |
||
1625 | |||
1626 | /** |
||
1627 | * Sets the minimum log level. |
||
1628 | * $level must be one of Psr\Log\LogLevel::xxx. |
||
1629 | * |
||
1630 | * Defaults to LogLevel::WARNING |
||
1631 | * |
||
1632 | * @param string $level |
||
1633 | */ |
||
1634 | public function setLogLevel(string $level) |
||
1638 | } |
||
1639 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.