| Conditions | 8 |
| Paths | 20 |
| Total Lines | 52 |
| Code Lines | 34 |
| 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 |
||
| 54 | public function addResource($serviceId, $modelClass, $adminClass, $controllerName, $managerType) |
||
| 55 | { |
||
| 56 | $rc = new \ReflectionClass($modelClass); |
||
| 57 | $modelClassShortName = $rc->getShortName(); |
||
| 58 | |||
| 59 | $code = "services:\n"; |
||
| 60 | |||
| 61 | if (is_file($this->file)) { |
||
| 62 | $code = rtrim(file_get_contents($this->file)); |
||
| 63 | $data = (array) Yaml::parse($code); |
||
| 64 | |||
| 65 | if ($code !== '') { |
||
| 66 | $code .= "\n"; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (array_key_exists('services', $data)) { |
||
| 70 | if (array_key_exists($serviceId, (array) $data['services'])) { |
||
| 71 | throw new \RuntimeException(sprintf( |
||
| 72 | 'The service "%s" is already defined in the file "%s".', |
||
| 73 | $serviceId, |
||
| 74 | realpath($this->file) |
||
| 75 | )); |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($data['services'] !== null) { |
||
| 79 | $code .= "\n"; |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | $code .= $code === '' ? '' : "\n" . "services:\n"; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | $code .= sprintf( |
||
| 87 | $this->template, |
||
| 88 | $serviceId, |
||
| 89 | $adminClass, |
||
| 90 | $modelClass, |
||
| 91 | $controllerName, |
||
| 92 | $modelClassShortName, |
||
| 93 | $managerType, |
||
| 94 | current(array_slice(explode('\\', $modelClass), -1)) |
||
| 95 | ); |
||
| 96 | @mkdir(dirname($this->file), 0777, true); |
||
| 97 | |||
| 98 | if (@file_put_contents($this->file, $code) === false) { |
||
| 99 | throw new \RuntimeException(sprintf( |
||
| 100 | 'Unable to append service "%s" to the file "%s". You will have to do it manually.', |
||
| 101 | $serviceId, |
||
| 102 | $this->file |
||
| 103 | )); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 |