Conditions | 5 |
Paths | 8 |
Total Lines | 56 |
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); |
||
41 | public function backupData(array $exclude=[]): string |
||
42 | { |
||
43 | $tables = $this->getDriver()->getTables(); |
||
44 | |||
45 | // Filter out the tables you don't want |
||
46 | if( ! empty($exclude)) |
||
47 | { |
||
48 | $tables = array_diff($tables, $exclude); |
||
49 | } |
||
50 | |||
51 | $outputSql = ''; |
||
52 | |||
53 | // Get the data for each object |
||
54 | foreach($tables as $t) |
||
55 | { |
||
56 | $sql = 'SELECT * FROM "'.trim($t).'"'; |
||
57 | $res = $this->getDriver()->query($sql); |
||
58 | $objRes = $res->fetchAll(\PDO::FETCH_ASSOC); |
||
59 | |||
60 | // Don't add to the file if the table is empty |
||
61 | if (count($objRes) < 1) |
||
62 | { |
||
63 | continue; |
||
64 | } |
||
65 | |||
66 | $res = NULL; |
||
|
|||
67 | |||
68 | // Nab the column names by getting the keys of the first row |
||
69 | $columns = @array_keys($objRes[0]); |
||
70 | |||
71 | $insertRows = []; |
||
72 | |||
73 | // Create the insert statements |
||
74 | foreach($objRes as $row) |
||
75 | { |
||
76 | $row = array_values($row); |
||
77 | |||
78 | // Quote values as needed by type |
||
79 | $row = array_map([$this->getDriver(), 'quote'], $row); |
||
80 | $row = array_map('trim', $row); |
||
81 | |||
82 | |||
83 | $rowString = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; |
||
84 | |||
85 | $row = NULL; |
||
86 | |||
87 | $insertRows[] = $rowString; |
||
88 | } |
||
89 | |||
90 | $objRes = NULL; |
||
91 | |||
92 | $outputSql .= "\n\n".implode("\n", $insertRows)."\n"; |
||
93 | } |
||
94 | |||
95 | return $outputSql; |
||
96 | } |
||
97 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.