Conditions | 20 |
Paths | 636 |
Total Lines | 75 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 |
||
31 | public function process(string $filename): void |
||
32 | { |
||
33 | $extension = pathinfo($filename, PATHINFO_EXTENSION); |
||
34 | |||
35 | switch ($extension) { |
||
36 | case 'gz': |
||
37 | case 'gzip': |
||
38 | $this->compressor = CompressorFactory::create(Option::COMPRESSION_GZIP); |
||
39 | break; |
||
40 | |||
41 | case 'bz2': |
||
42 | case 'bzip2': |
||
43 | $this->compressor = CompressorFactory::create(Option::COMPRESSION_BZIP2); |
||
44 | break; |
||
45 | |||
46 | case 'sql': |
||
47 | $this->compressor = CompressorFactory::create(Option::COMPRESSION_NONE); |
||
48 | break; |
||
49 | |||
50 | default: |
||
51 | throw Exception::unavailableDriverForcompression($extension); |
||
52 | } |
||
53 | |||
54 | if (null === $filename = $this->getFile($filename)) { |
||
55 | throw Exception::failledToRead(func_get_arg(0)); |
||
56 | } |
||
57 | |||
58 | if ($this->option->disable_foreign_keys_check && '' !== $disableForeignKeysCheck = $this->adapter->startDisableForeignKeysCheck()) { |
||
59 | $this->pdo->exec($disableForeignKeysCheck); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Read backup file line by line |
||
64 | */ |
||
65 | $handle = fopen($filename, 'rb'); |
||
66 | |||
67 | if (! $handle) { |
||
68 | throw Exception::failledToRead($filename); |
||
69 | } |
||
70 | |||
71 | $buffer = ''; |
||
72 | |||
73 | try { |
||
74 | while (! feof($handle)) { |
||
75 | $line = fgets($handle); |
||
76 | |||
77 | if (substr($line, 0, 2) === '--' || ! $line) { |
||
78 | continue; // skip comments |
||
79 | } |
||
80 | |||
81 | $buffer .= $line; |
||
82 | |||
83 | // if it has a semicolon at the end, it's the end of the query |
||
84 | if (';' === substr(rtrim($line), -1, 1)) { |
||
85 | if (false !== $affectedRows = $this->pdo->exec($buffer)) { |
||
86 | if (preg_match('/^CREATE TABLE `([^`]+)`/i', $buffer, $tableName)) { |
||
87 | $this->event->emit('table.create', $tableName[1]); |
||
88 | } |
||
89 | if (preg_match('/^INSERT INTO `([^`]+)`/i', $buffer, $tableName)) { |
||
90 | $this->event->emit('table.insert', $tableName[1], $affectedRows); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $buffer = ''; |
||
95 | } |
||
96 | } |
||
97 | } catch (PDOException $e) { |
||
98 | throw Exception::pdoException($e->getMessage(), $buffer); |
||
99 | } finally { |
||
100 | fclose($handle); |
||
101 | unlink($filename); |
||
102 | } |
||
103 | |||
104 | if ($this->option->disable_foreign_keys_check && '' !== $enableForeignKeysCheck = $this->adapter->endDisableForeignKeysCheck()) { |
||
105 | $this->pdo->exec($enableForeignKeysCheck); |
||
106 | } |
||
139 |