Complex classes like TMappedStatement 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 TMappedStatement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class TMappedStatement extends TComponent implements IMappedStatement |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var TSqlMapStatement current SQL statement. |
||
| 26 | */ |
||
| 27 | private $_statement; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var TPreparedCommand SQL command prepareer |
||
| 31 | */ |
||
| 32 | private $_command; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var TSqlMapper sqlmap used by this mapper. |
||
| 36 | */ |
||
| 37 | private $_manager; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var TPostSelectBinding[] post select statement queue. |
||
| 41 | */ |
||
| 42 | private $_selectQueue=array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var boolean true when data is mapped to a particular row. |
||
| 46 | */ |
||
| 47 | private $_IsRowDataFound = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var TSQLMapObjectCollectionTree group by object collection tree |
||
| 51 | */ |
||
| 52 | private $_groupBy; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Post select is to query for list. |
||
| 56 | */ |
||
| 57 | const QUERY_FOR_LIST = 0; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Post select is to query for list. |
||
| 61 | */ |
||
| 62 | const QUERY_FOR_ARRAY = 1; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Post select is to query for object. |
||
| 66 | */ |
||
| 67 | const QUERY_FOR_OBJECT = 2; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string Name used to identify the TMappedStatement amongst the others. |
||
| 71 | * This the name of the SQL statement by default. |
||
| 72 | */ |
||
| 73 | public function getID() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return TSqlMapStatement The SQL statment used by this MappedStatement |
||
| 80 | */ |
||
| 81 | public function getStatement() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return TSqlMapper The SqlMap used by this MappedStatement |
||
| 88 | */ |
||
| 89 | public function getManager() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return TPreparedCommand command to prepare SQL statements. |
||
| 96 | */ |
||
| 97 | public function getCommand() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Empty the group by results cache. |
||
| 104 | */ |
||
| 105 | protected function initialGroupByResults() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Creates a new mapped statement. |
||
| 112 | * @param TSqlMapper an sqlmap. |
||
| 113 | * @param TSqlMapStatement An SQL statement. |
||
| 114 | */ |
||
| 115 | public function __construct(TSqlMapManager $sqlMap, TSqlMapStatement $statement) |
||
| 122 | |||
| 123 | public function getSqlString() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Execute SQL Query. |
||
| 130 | * @param IDbConnection database connection |
||
| 131 | * @param array SQL statement and parameters. |
||
| 132 | * @return mixed record set if applicable. |
||
| 133 | * @throws TSqlMapExecutionException if execution error or false record set. |
||
| 134 | * @throws TSqlMapQueryExecutionException if any execution error |
||
| 135 | */ |
||
| 136 | /* protected function executeSQLQuery($connection, $sql) |
||
| 137 | { |
||
| 138 | try |
||
| 139 | { |
||
| 140 | if(!($recordSet = $connection->execute($sql['sql'],$sql['parameters']))) |
||
| 141 | { |
||
| 142 | throw new TSqlMapExecutionException( |
||
| 143 | 'sqlmap_execution_error_no_record', $this->getID(), |
||
| 144 | $connection->ErrorMsg()); |
||
| 145 | } |
||
| 146 | return $recordSet; |
||
| 147 | } |
||
| 148 | catch (Exception $e) |
||
| 149 | { |
||
| 150 | throw new TSqlMapQueryExecutionException($this->getStatement(), $e); |
||
| 151 | } |
||
| 152 | }*/ |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Execute SQL Query with limits. |
||
| 156 | * @param IDbConnection database connection |
||
| 157 | * @param array SQL statement and parameters. |
||
| 158 | * @return mixed record set if applicable. |
||
| 159 | * @throws TSqlMapExecutionException if execution error or false record set. |
||
| 160 | * @throws TSqlMapQueryExecutionException if any execution error |
||
| 161 | */ |
||
| 162 | protected function executeSQLQueryLimit($connection, $command, $max, $skip) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Executes the SQL and retuns a List of result objects. |
||
| 193 | * @param IDbConnection database connection |
||
| 194 | * @param mixed The object used to set the parameters in the SQL. |
||
| 195 | * @param object result collection object. |
||
| 196 | * @param integer The number of rows to skip over. |
||
| 197 | * @param integer The maximum number of rows to return. |
||
| 198 | * @return array a list of result objects |
||
| 199 | * @param callback row delegate handler |
||
| 200 | * @see executeQueryForList() |
||
| 201 | */ |
||
| 202 | public function executeQueryForList($connection, $parameter, $result=null, $skip=-1, $max=-1, $delegate=null) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Executes the SQL and retuns a List of result objects. |
||
| 210 | * |
||
| 211 | * This method should only be called by internal developers, consider using |
||
| 212 | * <tt>executeQueryForList()</tt> first. |
||
| 213 | * |
||
| 214 | * @param IDbConnection database connection |
||
| 215 | * @param mixed The object used to set the parameters in the SQL. |
||
| 216 | * @param array SQL string and subsititution parameters. |
||
| 217 | * @param object result collection object. |
||
| 218 | * @param integer The number of rows to skip over. |
||
| 219 | * @param integer The maximum number of rows to return. |
||
| 220 | * @param callback row delegate handler |
||
| 221 | * @return array a list of result objects |
||
| 222 | * @see executeQueryForList() |
||
| 223 | */ |
||
| 224 | public function runQueryForList($connection, $parameter, $sql, $result, $delegate=null) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Executes the SQL and retuns all rows selected in a map that is keyed on |
||
| 265 | * the property named in the keyProperty parameter. The value at each key |
||
| 266 | * will be the value of the property specified in the valueProperty parameter. |
||
| 267 | * If valueProperty is null, the entire result object will be entered. |
||
| 268 | * @param IDbConnection database connection |
||
| 269 | * @param mixed The object used to set the parameters in the SQL. |
||
| 270 | * @param string The property of the result object to be used as the key. |
||
| 271 | * @param string The property of the result object to be used as the value (or null). |
||
| 272 | * @param callback row delegate handler |
||
| 273 | * @return array An array of object containing the rows keyed by keyProperty. |
||
| 274 | */ |
||
| 275 | public function executeQueryForMap($connection, $parameter, $keyProperty, $valueProperty=null, $skip=-1, $max=-1, $delegate=null) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Executes the SQL and retuns all rows selected in a map that is keyed on |
||
| 283 | * the property named in the keyProperty parameter. The value at each key |
||
| 284 | * will be the value of the property specified in the valueProperty parameter. |
||
| 285 | * If valueProperty is null, the entire result object will be entered. |
||
| 286 | * |
||
| 287 | * This method should only be called by internal developers, consider using |
||
| 288 | * <tt>executeQueryForMap()</tt> first. |
||
| 289 | * |
||
| 290 | * @param IDbConnection database connection |
||
| 291 | * @param mixed The object used to set the parameters in the SQL. |
||
| 292 | * @param array SQL string and subsititution parameters. |
||
| 293 | * @param string The property of the result object to be used as the key. |
||
| 294 | * @param string The property of the result object to be used as the value (or null). |
||
| 295 | * @param callback row delegate, a callback function |
||
| 296 | * @return array An array of object containing the rows keyed by keyProperty. |
||
| 297 | * @see executeQueryForMap() |
||
| 298 | */ |
||
| 299 | public function runQueryForMap($connection, $parameter, $command, $keyProperty, $valueProperty=null, $delegate=null) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Raises delegate handler. |
||
| 335 | * This method is invoked for each new list item. It is the responsibility |
||
| 336 | * of the handler to add the item to the list. |
||
| 337 | * @param object event parameter |
||
| 338 | */ |
||
| 339 | protected function raiseRowDelegate($handler, $param) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Executes an SQL statement that returns a single row as an object of the |
||
| 367 | * type of the <tt>$result</tt> passed in as a parameter. |
||
| 368 | * @param IDbConnection database connection |
||
| 369 | * @param mixed The parameter data (object, arrary, primitive) used to set the parameters in the SQL |
||
| 370 | * @param mixed The result object. |
||
| 371 | * @return ${return} |
||
| 372 | */ |
||
| 373 | public function executeQueryForObject($connection, $parameter, $result=null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Executes an SQL statement that returns a single row as an object of the |
||
| 381 | * type of the <tt>$result</tt> passed in as a parameter. |
||
| 382 | * |
||
| 383 | * This method should only be called by internal developers, consider using |
||
| 384 | * <tt>executeQueryForObject()</tt> first. |
||
| 385 | * |
||
| 386 | * @param IDbConnection database connection |
||
| 387 | * @param array SQL string and subsititution parameters. |
||
| 388 | * @param object The result object. |
||
| 389 | * @return object the object. |
||
| 390 | * @see executeQueryForObject() |
||
| 391 | */ |
||
| 392 | public function runQueryForObject($connection, $command, &$result) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Execute an insert statement. Fill the parameter object with the ouput |
||
| 414 | * parameters if any, also could return the insert generated key. |
||
| 415 | * @param IDbConnection database connection |
||
| 416 | * @param mixed The parameter object used to fill the statement. |
||
| 417 | * @return string the insert generated key. |
||
| 418 | */ |
||
| 419 | public function executeInsert($connection, $parameter) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Gets the insert generated ID before executing an insert statement. |
||
| 437 | * @param IDbConnection database connection |
||
| 438 | * @param mixed insert statement parameter. |
||
| 439 | * @return string new insert ID if pre-select key statement was executed, null otherwise. |
||
| 440 | */ |
||
| 441 | protected function getPreGeneratedSelectKey($connection, $parameter) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Gets the inserted row ID after executing an insert statement. |
||
| 453 | * @param IDbConnection database connection |
||
| 454 | * @param mixed insert statement parameter. |
||
| 455 | * @return string last insert ID, null otherwise. |
||
| 456 | */ |
||
| 457 | protected function getPostGeneratedSelectKey($connection, $parameter) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Execute the select key statement, used to obtain last insert ID. |
||
| 469 | * @param IDbConnection database connection |
||
| 470 | * @param mixed insert statement parameter |
||
| 471 | * @param TSqlMapSelectKey select key statement |
||
| 472 | * @return string last insert ID. |
||
| 473 | */ |
||
| 474 | protected function executeSelectKey($connection, $parameter, $selectKey) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Execute an update statement. Also used for delete statement. |
||
| 486 | * Return the number of rows effected. |
||
| 487 | * @param IDbConnection database connection |
||
| 488 | * @param mixed The object used to set the parameters in the SQL. |
||
| 489 | * @return integer The number of rows effected. |
||
| 490 | */ |
||
| 491 | public function executeUpdate($connection, $parameter) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Process 'select' result properties |
||
| 503 | * @param IDbConnection database connection |
||
| 504 | */ |
||
| 505 | protected function executePostSelect($connection) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Raise the execute query event. |
||
| 534 | * @param array prepared SQL statement and subsititution parameters |
||
| 535 | */ |
||
| 536 | public function onExecuteQuery($sql) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Apply result mapping. |
||
| 543 | * @param array a result set row retrieved from the database |
||
| 544 | * @param object the result object, will create if necessary. |
||
| 545 | * @return object the result filled with data, null if not filled. |
||
| 546 | */ |
||
| 547 | protected function applyResultMap($row, &$resultObject=null) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Fill the result using ResultClass, will creates new result object if required. |
||
| 569 | * @param string result object class name |
||
| 570 | * @param array a result set row retrieved from the database |
||
| 571 | * @param object the result object, will create if necessary. |
||
| 572 | * @return object result object filled with data |
||
| 573 | */ |
||
| 574 | protected function fillResultClass($resultClass, $row, $resultObject) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Apply the result to a TList or an array. |
||
| 592 | * @param array a result set row retrieved from the database |
||
| 593 | * @param object result object, array or list |
||
| 594 | * @return object result filled with data. |
||
| 595 | */ |
||
| 596 | protected function fillResultArrayList($row, $resultObject) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Apply the result to an object. |
||
| 609 | * @param array a result set row retrieved from the database |
||
| 610 | * @param object result object, array or list |
||
| 611 | * @return object result filled with data. |
||
| 612 | */ |
||
| 613 | protected function fillResultObjectProperty($row, $resultObject) |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Fills the result object according to result mappings. |
||
| 633 | * @param string result map name. |
||
| 634 | * @param array a result set row retrieved from the database |
||
| 635 | * @param object result object to fill, will create new instances if required. |
||
| 636 | * @return object result object filled with data. |
||
| 637 | */ |
||
| 638 | protected function fillResultMap($resultMapName, $row, $parentGroup=null, &$resultObject=null) |
||
| 661 | |||
| 662 | /** |
||
| 663 | * ResultMap with GroupBy property. Save object collection graph in a tree |
||
| 664 | * and collect the result later. |
||
| 665 | * @param TResultMap result mapping details. |
||
| 666 | * @param array a result set row retrieved from the database |
||
| 667 | * @param object the result object |
||
| 668 | * @return object result object. |
||
| 669 | */ |
||
| 670 | protected function addResultMapGroupBy($resultMap, $row, $parent, &$resultObject) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Gets the result 'group by' groupping key for each row. |
||
| 712 | * @param TResultMap result mapping details. |
||
| 713 | * @param array a result set row retrieved from the database |
||
| 714 | * @return string groupping key. |
||
| 715 | */ |
||
| 716 | protected function getResultMapGroupKey($resultMap, $row) |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Fill the result map using default settings. If <tt>$resultMap</tt> is null |
||
| 727 | * the result object returned will be guessed from <tt>$resultObject</tt>. |
||
| 728 | * @param TResultMap result mapping details. |
||
| 729 | * @param array a result set row retrieved from the database |
||
| 730 | * @param object the result object |
||
| 731 | * @return mixed the result object filled with data. |
||
| 732 | */ |
||
| 733 | protected function fillDefaultResultMap($resultMap, $row, $resultObject) |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Retrieve the result map as an array. |
||
| 752 | * @param TResultMap result mapping details. |
||
| 753 | * @param array a result set row retrieved from the database |
||
| 754 | * @param object the result object |
||
| 755 | * @return array array list of result objects. |
||
| 756 | */ |
||
| 757 | protected function fillArrayResultMap($resultMap, $row, $resultObject) |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Converts the first array value to scalar value of given type. |
||
| 773 | * @param array list of results |
||
| 774 | * @param string scalar type. |
||
| 775 | * @return mixed scalar value. |
||
| 776 | */ |
||
| 777 | protected function getScalarResult($result, $type) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Set a property of the result object with appropriate value. |
||
| 786 | * @param TResultMap result mapping details. |
||
| 787 | * @param TResultProperty the result property to fill. |
||
| 788 | * @param array a result set row retrieved from the database |
||
| 789 | * @param object the result object |
||
| 790 | */ |
||
| 791 | protected function setObjectProperty($resultMap, $property, $row, &$resultObject) |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Add nested result property to post select queue. |
||
| 836 | * @param string post select statement ID |
||
| 837 | * @param TResultMap current result mapping details. |
||
| 838 | * @param TResultProperty current result property. |
||
| 839 | * @param array a result set row retrieved from the database |
||
| 840 | * @param object the result object |
||
| 841 | */ |
||
| 842 | protected function enquequePostSelect($select, $resultMap, $property, $row, $resultObject) |
||
| 872 | |||
| 873 | /** |
||
| 874 | * Finds in the post select property the SQL statement primary selection keys. |
||
| 875 | * @param TResultMap result mapping details |
||
| 876 | * @param TResultProperty result property |
||
| 877 | * @param array current row data. |
||
| 878 | * @return array list of primary key values. |
||
| 879 | */ |
||
| 880 | protected function getPostSelectKeys($resultMap, $property,$row) |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Fills the property with result mapping results. |
||
| 902 | * @param TResultMap nested result mapping details. |
||
| 903 | * @param array a result set row retrieved from the database |
||
| 904 | * @param object the result object |
||
| 905 | * @return boolean true if the data was found, false otherwise. |
||
| 906 | */ |
||
| 907 | protected function fillPropertyWithResultMap($resultMap, $row, &$resultObject) |
||
| 919 | |||
| 920 | public function __wakeup() |
||
| 925 | |||
| 926 | public function __sleep() |
||
| 934 | } |
||
| 935 | |||
| 1237 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..