| Conditions | 21 |
| Paths | 24 |
| Total Lines | 73 |
| Code Lines | 49 |
| 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 |
||
| 46 | public function resolveContainer(): Container |
||
| 47 | { |
||
| 48 | $stream = ''; |
||
| 49 | if ($this->fileType === 'application/x-gzip') { |
||
| 50 | $stream = 'compress.zlib://'; |
||
| 51 | } elseif ($this->fileType === 'application/x-bzip2') { |
||
| 52 | $stream = 'compress.bzip2://'; |
||
| 53 | } |
||
| 54 | |||
| 55 | $stubContent = null; |
||
| 56 | $manifestContent = null; |
||
| 57 | $manifestLength = null; |
||
| 58 | $resource = fopen($stream . $this->fileName, 'r'); |
||
| 59 | while (!feof($resource)) { |
||
|
|
|||
| 60 | $line = fgets($resource); |
||
| 61 | // stop reading file when manifest can be extracted |
||
| 62 | if ($manifestLength !== null && $manifestContent !== null && strlen($manifestContent) >= $manifestLength) { |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | |||
| 66 | $stubPosition = strpos($line, '<?php'); |
||
| 67 | $manifestPosition = strpos($line, '__HALT_COMPILER()'); |
||
| 68 | |||
| 69 | // line contains both, start of (empty) stub and start of manifest |
||
| 70 | if ($stubContent === null && $stubPosition !== false |
||
| 71 | && $manifestContent === null && $manifestPosition !== false) { |
||
| 72 | $stubContent = substr($line, $stubPosition, $manifestPosition - $stubPosition - 1); |
||
| 73 | $manifestContent = preg_replace('#^.*__HALT_COMPILER\(\)[^>]*\?>(\r|\n)*#', '', $line); |
||
| 74 | $manifestLength = $this->resolveManifestLength($manifestContent); |
||
| 75 | // line contains start of stub |
||
| 76 | } elseif ($stubContent === null && $stubPosition !== false) { |
||
| 77 | $stubContent = substr($line, $stubPosition); |
||
| 78 | // line contains start of manifest |
||
| 79 | } elseif ($manifestContent === null && $manifestPosition !== false) { |
||
| 80 | $manifestContent = preg_replace('#^.*__HALT_COMPILER\(\)[^>]*\?>(\r|\n)*#', '', $line); |
||
| 81 | $manifestLength = $this->resolveManifestLength($manifestContent); |
||
| 82 | // manifest has been started (thus is cannot be stub anymore), add content |
||
| 83 | } elseif ($manifestContent !== null) { |
||
| 84 | $manifestContent .= $line; |
||
| 85 | $manifestLength = $this->resolveManifestLength($manifestContent); |
||
| 86 | // stub has been started (thus cannot be manifest here, yet), add content |
||
| 87 | } elseif ($stubContent !== null) { |
||
| 88 | $stubContent .= $line; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | fclose($resource); |
||
| 92 | |||
| 93 | if ($stubContent === null) { |
||
| 94 | throw new \UnexpectedValueException( |
||
| 95 | 'Cannot resolve stub', |
||
| 96 | 1547807881 |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | if ($manifestContent === null || $manifestLength === null) { |
||
| 100 | throw new \UnexpectedValueException( |
||
| 101 | 'Cannot resolve manifest', |
||
| 102 | 1547807882 |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | if (strlen($manifestContent) < $manifestLength) { |
||
| 106 | throw new \UnexpectedValueException( |
||
| 107 | sprintf( |
||
| 108 | 'Exected manifest length %d, got %d', |
||
| 109 | strlen($manifestContent), |
||
| 110 | $manifestLength |
||
| 111 | ), |
||
| 112 | 1547807883 |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | return new Container( |
||
| 117 | Stub::fromContent($stubContent), |
||
| 118 | Manifest::fromContent($manifestContent) |
||
| 119 | ); |
||
| 193 |