| Conditions | 13 |
| Paths | 53 |
| Total Lines | 48 |
| Code Lines | 32 |
| 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 |
||
| 164 | protected function createFixtures(array $fixtures) |
||
| 165 | { |
||
| 166 | // normalize fixture configurations |
||
| 167 | $config = []; // configuration provided in test case |
||
| 168 | $aliases = []; // class name => alias or class name |
||
| 169 | |||
| 170 | foreach ($fixtures as $name => $fixture) { |
||
| 171 | if (!is_array($fixture)) { |
||
| 172 | $class = ltrim($fixture, '\\'); |
||
| 173 | $fixtures[$name] = ['class' => $class]; |
||
| 174 | $aliases[$class] = is_int($name) ? $class : $name; |
||
| 175 | } elseif (isset($fixture['class'])) { |
||
| 176 | $class = ltrim($fixture['class'], '\\'); |
||
| 177 | $config[$class] = $fixture; |
||
| 178 | $aliases[$class] = $name; |
||
| 179 | } else { |
||
| 180 | throw new InvalidConfigException("You must specify 'class' for the fixture '$name'."); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | // create fixture instances |
||
| 185 | $instances = []; |
||
| 186 | $stack = array_reverse($fixtures); |
||
| 187 | |||
| 188 | while (($fixture = array_pop($stack)) !== null) { |
||
| 189 | if ($fixture instanceof Fixture) { |
||
| 190 | $class = get_class($fixture); |
||
| 191 | $name = isset($aliases[$class]) ? $aliases[$class] : $class; |
||
| 192 | unset($instances[$name]); // unset so that the fixture is added to the last in the next line |
||
| 193 | $instances[$name] = $fixture; |
||
| 194 | } else { |
||
| 195 | $class = ltrim($fixture['class'], '\\'); |
||
| 196 | $name = isset($aliases[$class]) ? $aliases[$class] : $class; |
||
| 197 | if (!isset($instances[$name])) { |
||
| 198 | $instances[$name] = false; |
||
| 199 | |||
| 200 | $stack[] = $fixture = app()->make($fixture['class']); |
||
| 201 | foreach ($fixture->depends as $dep) { |
||
| 202 | // need to use the configuration provided in test case |
||
| 203 | $stack[] = isset($config[$dep]) ? $config[$dep] : ['class' => $dep]; |
||
| 204 | } |
||
| 205 | } elseif ($instances[$name] === false) { |
||
| 206 | throw new InvalidConfigException("A circular dependency is detected for fixture '$class'."); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | return $instances; |
||
| 212 | } |
||
| 214 |