| Conditions | 10 |
| Paths | 9 |
| Total Lines | 31 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 26 | public function convert(Exception $exception, ?Query $query): DriverException |
||
| 27 | { |
||
| 28 | switch ($exception->getCode()) { |
||
| 29 | case '4000': |
||
| 30 | return new SyntaxErrorException($exception, $query); |
||
| 31 | |||
| 32 | case '4008': |
||
| 33 | case '4043': |
||
| 34 | return new InvalidFieldNameException($exception, $query); |
||
| 35 | |||
| 36 | case '4041': |
||
| 37 | return new TableNotFoundException($exception, $query); |
||
| 38 | |||
| 39 | case '4045': |
||
| 40 | return new SchemaDoesNotExist($exception, $query); |
||
| 41 | |||
| 42 | case '4091': |
||
| 43 | return new UniqueConstraintViolationException($exception, $query); |
||
| 44 | |||
| 45 | case '4093': |
||
| 46 | return new TableExistsException($exception, $query); |
||
| 47 | } |
||
| 48 | |||
| 49 | // Prior to fixing https://bugs.php.net/bug.php?id=64705 (PHP 7.4.10), |
||
| 50 | // in some cases (mainly connection errors) the PDO exception wouldn't provide a SQLSTATE via its code. |
||
| 51 | // We have to match against the SQLSTATE in the error message in these cases. |
||
| 52 | if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { |
||
| 53 | return new ConnectionException($exception, $query); |
||
| 54 | } |
||
| 55 | |||
| 56 | return new DriverException($exception, $query); |
||
| 57 | } |
||
| 59 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: