Conditions | 5 |
Paths | 8 |
Total Lines | 56 |
Code Lines | 24 |
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($exclude=[]) |
||
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 compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.