| Conditions | 7 |
| Paths | 20 |
| Total Lines | 59 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php declare(strict_types=1); |
||
| 76 | |||
| 77 | // Filter out the tables you don't want |
||
| 78 | if( ! empty($exclude)) |
||
| 79 | { |
||
| 80 | $tables = array_diff($tables, $exclude); |
||
| 81 | } |
||
| 82 | |||
| 83 | $outputSql = ''; |
||
| 84 | |||
| 85 | // Get the data for each object |
||
| 86 | foreach($tables as $t) |
||
| 87 | { |
||
| 88 | $sql = 'SELECT * FROM "'.trim($t).'"'; |
||
| 89 | $res = $this->getDriver()->query($sql); |
||
| 90 | $objRes = $res->fetchAll(PDO::FETCH_ASSOC); |
||
| 91 | |||
| 92 | // Don't add to the file if the table is empty |
||
| 93 | if (count($objRes) < 1) |
||
| 94 | { |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | |||
| 98 | // Nab the column names by getting the keys of the first row |
||
| 99 | $columns = @array_keys($objRes[0]); |
||
| 100 | |||
| 101 | $insertRows = []; |
||
| 102 | |||
| 103 | // Create the insert statements |
||
| 104 | foreach($objRes as $row) |
||
| 105 | { |
||
| 106 | $row = array_values($row); |
||
| 107 | |||
| 108 | // Quote values as needed by type |
||
| 109 | if(stripos($t, 'RDB$') === FALSE) |
||
| 110 | { |
||
| 111 | $row = array_map([$this->getDriver(), 'quote'], $row); |
||
| 112 | $row = array_map('trim', $row); |
||
| 113 | } |
||
| 114 | |||
| 115 | $rowString = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; |
||
| 116 | |||
| 117 | $row = NULL; |
||
| 118 | |||
| 119 | $insertRows[] = $rowString; |
||
| 120 | } |
||
| 121 | |||
| 122 | $outputSql .= "\n\nSET TRANSACTION;\n".implode("\n", $insertRows)."\nCOMMIT;"; |
||
| 123 | } |
||
| 124 | |||
| 125 | return $outputSql; |
||
| 126 | } |
||
| 127 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: