| Conditions | 13 |
| Paths | 641 |
| Total Lines | 51 |
| 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 |
||
| 65 | public static function fromDsn(string $dsn): self |
||
| 66 | { |
||
| 67 | if (false === $parsedDsn = \parse_url($dsn)) { |
||
| 68 | throw new InvalidException(\sprintf("DSN '%s' is invalid.", $dsn)); |
||
| 69 | } |
||
| 70 | |||
| 71 | $clientConfiguration = new self(); |
||
| 72 | |||
| 73 | if (isset($parsedDsn['scheme'])) { |
||
| 74 | $clientConfiguration->set('transport', $parsedDsn['scheme']); |
||
| 75 | } |
||
| 76 | |||
| 77 | if (isset($parsedDsn['host'])) { |
||
| 78 | $clientConfiguration->set('host', $parsedDsn['host']); |
||
| 79 | } |
||
| 80 | |||
| 81 | if (isset($parsedDsn['user'])) { |
||
| 82 | $clientConfiguration->set('username', \urldecode($parsedDsn['user'])); |
||
| 83 | } |
||
| 84 | |||
| 85 | if (isset($parsedDsn['pass'])) { |
||
| 86 | $clientConfiguration->set('password', \urldecode($parsedDsn['pass'])); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (isset($parsedDsn['port'])) { |
||
| 90 | $clientConfiguration->set('port', $parsedDsn['port']); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (isset($parsedDsn['path'])) { |
||
| 94 | $clientConfiguration->set('path', $parsedDsn['path']); |
||
| 95 | } |
||
| 96 | |||
| 97 | $options = []; |
||
| 98 | if (isset($parsedDsn['query'])) { |
||
| 99 | \parse_str($parsedDsn['query'], $options); |
||
| 100 | } |
||
| 101 | |||
| 102 | foreach ($options as $optionName => $optionValue) { |
||
|
|
|||
| 103 | if ('false' === $optionValue) { |
||
| 104 | $optionValue = false; |
||
| 105 | } elseif ('true' === $optionValue) { |
||
| 106 | $optionValue = true; |
||
| 107 | } elseif (\is_numeric($optionValue)) { |
||
| 108 | $optionValue = (int) $optionValue; |
||
| 109 | } |
||
| 110 | |||
| 111 | $clientConfiguration->set($optionName, $optionValue); |
||
| 112 | } |
||
| 113 | |||
| 114 | return $clientConfiguration; |
||
| 115 | } |
||
| 116 | |||
| 190 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.