| Conditions | 1 |
| Paths | 1 |
| Total Lines | 56 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 35 |
| CRAP Score | 1 |
| 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 |
||
| 66 | 2 | private function parse(Str $output): MapInterface |
|
| 67 | { |
||
| 68 | $lines = $output |
||
| 69 | 2 | ->trim() |
|
| 70 | 2 | ->split("\n"); |
|
| 71 | $columns = $lines |
||
| 72 | 2 | ->first() |
|
| 73 | 2 | ->pregSplit('~ +~') |
|
| 74 | 2 | ->reduce( |
|
| 75 | 2 | new Sequence, |
|
| 76 | static function(Sequence $columns, Str $column): Sequence { |
||
| 77 | 2 | $column = (string) $column; |
|
| 78 | |||
| 79 | 2 | return $columns->add(self::$columns[$column] ?? $column); |
|
| 80 | 2 | } |
|
| 81 | ); |
||
| 82 | |||
| 83 | return $lines |
||
| 84 | 2 | ->drop(1) |
|
| 85 | 2 | ->reduce( |
|
| 86 | 2 | new Sequence, |
|
| 87 | static function(Sequence $lines, Str $line) use ($columns): Sequence { |
||
| 88 | 2 | return $lines->add( |
|
| 89 | 2 | $line->pregSplit('~ +~', $columns->size()) |
|
| 90 | ); |
||
| 91 | 2 | } |
|
| 92 | ) |
||
| 93 | ->map(function(StreamInterface $parts) use ($columns): Volume { |
||
| 94 | 2 | return new Volume( |
|
| 95 | 2 | new MountPoint( |
|
| 96 | 2 | (string) $parts->get($columns->indexOf('mountPoint')) |
|
| 97 | ), |
||
| 98 | 2 | Bytes::fromString( |
|
| 99 | 2 | (string) $parts->get($columns->indexOf('size')) |
|
| 100 | ), |
||
| 101 | 2 | Bytes::fromString( |
|
| 102 | 2 | (string) $parts->get($columns->indexOf('available')) |
|
| 103 | ), |
||
| 104 | 2 | Bytes::fromString( |
|
| 105 | 2 | (string) $parts->get($columns->indexOf('used')) |
|
| 106 | ), |
||
| 107 | 2 | new Usage( |
|
| 108 | 2 | (float) (string) $parts->get($columns->indexOf('usage')) |
|
| 109 | ) |
||
| 110 | ); |
||
| 111 | 2 | }) |
|
| 112 | 2 | ->reduce( |
|
| 113 | 2 | new Map('string', Volume::class), |
|
| 114 | 2 | static function(Map $volumes, Volume $volume): Map { |
|
| 115 | 2 | return $volumes->put( |
|
| 116 | 2 | (string) $volume->mountPoint(), |
|
|
1 ignored issue
–
show
|
|||
| 117 | 2 | $volume |
|
|
1 ignored issue
–
show
|
|||
| 118 | ); |
||
| 119 | 2 | } |
|
| 120 | ); |
||
| 121 | } |
||
| 122 | } |
||
| 123 |
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: