| Conditions | 16 | 
| Paths | 16 | 
| Total Lines | 65 | 
| 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 | ||
| 134 | protected function streamFileIntoDB($path, string $connectionName = null) | ||
| 135 |     { | ||
| 136 |         if ($connectionName !== null) { | ||
| 137 | DB::setDefaultConnection($connectionName); | ||
| 138 | } | ||
| 139 | |||
| 140 | $tmpLine = ''; | ||
| 141 | $counter = $this->showProgress ? 0 : false; | ||
| 142 | |||
| 143 | event(new SnapshotStatus($this, 'Importing SQL...')); | ||
| 144 | |||
| 145 |         $this->getFileHandler($path)->each(function ($line) use (&$tmpLine, &$counter, $connectionName) { | ||
| 146 |             if ($counter !== false && $counter % 500 === 0) { | ||
| 147 | echo '.'; | ||
| 148 | } | ||
| 149 | |||
| 150 | // Skip it if line is a comment | ||
| 151 |             if (substr($line, 0, 2) === '--' || trim($line) == '') { | ||
| 152 | return; | ||
| 153 | } | ||
| 154 | |||
| 155 | $tmpLine .= $line; | ||
| 156 | |||
| 157 | // If the line ends with a semicolon, it is the end of the query - run it | ||
| 158 |             if (substr(trim($line), -1, 1) === ';') { | ||
| 159 |                 try { | ||
| 160 | DB::connection($connectionName)->unprepared($tmpLine); | ||
| 161 |                 } catch (Exception $e) { | ||
| 162 |                     if ($counter !== false) { | ||
| 163 | echo 'E'; | ||
| 164 | } | ||
| 165 | |||
| 166 |                     preg_match_all('/INSERT INTO `(.*)`/mU', $e->getMessage(), $matches); | ||
| 167 | |||
| 168 |                     if (is_array($matches)) { | ||
| 169 | unset($matches[0]); | ||
| 170 | |||
| 171 |                         foreach ($matches as $match) { | ||
| 172 |                             if (empty($match[0])) { | ||
| 173 | continue; | ||
| 174 | } | ||
| 175 | $tableName = $match[0]; | ||
| 176 |                             if (! isset($this->errors[$tableName])) { | ||
| 177 | $this->errors[$tableName] = 0; | ||
| 178 | } | ||
| 179 | $this->errors[$tableName]++; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | $tmpLine = ''; | ||
| 185 | } | ||
| 186 | $counter++; | ||
| 187 | }); | ||
| 188 | |||
| 189 |         if ($counter !== false) { | ||
| 190 | echo PHP_EOL; | ||
| 191 | } | ||
| 192 | |||
| 193 |         if (! empty($this->errors)) { | ||
| 194 | return $this->errors; | ||
| 195 | } | ||
| 196 | |||
| 197 | return true; | ||
| 198 | } | ||
| 199 | |||
| 251 | 
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.