| Conditions | 15 |
| Paths | 15 |
| Total Lines | 76 |
| 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 |
||
| 90 | * @throws \InvalidArgumentException |
||
| 91 | */ |
||
| 92 | public function read(string $file) |
||
| 93 | { |
||
| 94 | // Read file from string |
||
| 95 | if (!$source = file_get_contents($file)) { |
||
| 96 | throw new ErrorException('Provided file could not to be read'); |
||
| 97 | } |
||
| 98 | |||
| 99 | // Convert JSON to array |
||
| 100 | if (!$array = $this->isJson($source)) { |
||
| 101 | throw new ErrorException('Provided string is not a valid JSON'); |
||
| 102 | } |
||
| 103 | |||
| 104 | // Initiate composer object |
||
| 105 | $this->composer = new Composer(); |
||
| 106 | |||
| 107 | // Parse values |
||
| 108 | foreach ($array as $key => $value) { |
||
| 109 | |||
| 110 | switch ($key) { |
||
| 111 | |||
| 112 | /* |
||
| 113 | * Specific objects |
||
| 114 | */ |
||
| 115 | |||
| 116 | case 'authors': |
||
| 117 | $this->composer->authors = $this->parseAuthors($value); |
||
| 118 | break; |
||
| 119 | case 'support': |
||
| 120 | $this->composer->support = $this->parseSupport($value); |
||
| 121 | break; |
||
| 122 | case 'repositories': |
||
| 123 | $this->composer->repositories = $this->parseRepositories($value); |
||
| 124 | break; |
||
| 125 | case 'autoload': |
||
| 126 | $this->composer->autoload = $this->parseAutoload($value); |
||
| 127 | break; |
||
| 128 | case 'autoload-dev': |
||
| 129 | $this->composer->autoloadDev = $this->parseAutoload($value); |
||
| 130 | break; |
||
| 131 | |||
| 132 | /* |
||
| 133 | * Convert format of keys |
||
| 134 | */ |
||
| 135 | |||
| 136 | case 'require-dev': |
||
| 137 | $this->composer->requireDev = $value; |
||
| 138 | break; |
||
| 139 | case 'include-path': |
||
| 140 | $this->composer->includePath = $value; |
||
| 141 | break; |
||
| 142 | case 'target-dir': |
||
| 143 | $this->composer->targetDir = $value; |
||
| 144 | break; |
||
| 145 | case 'minimum-stability': |
||
| 146 | $this->composer->minimumStability = $value; |
||
| 147 | break; |
||
| 148 | case 'prefer-stable': |
||
| 149 | $this->composer->preferStable = $value; |
||
| 150 | break; |
||
| 151 | case 'non-feature-branches'; |
||
| 152 | $this->composer->nonFeatureBranches = $value; |
||
| 153 | break; |
||
| 154 | |||
| 155 | /* |
||
| 156 | * Default values |
||
| 157 | */ |
||
| 158 | |||
| 159 | default: |
||
| 160 | $this->composer->$key = $value; |
||
| 161 | break; |
||
| 162 | } |
||
| 163 | |||
| 164 | } |
||
| 165 | |||
| 166 | return $this->composer; |
||
| 265 | } |