| Conditions | 13 |
| Paths | 81 |
| Total Lines | 49 |
| Code Lines | 30 |
| 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 |
||
| 68 | protected function load($dsl, $context) |
||
| 69 | { |
||
| 70 | if (!isset($dsl['file'])) { |
||
| 71 | throw new \Exception("Invalid step definition: miss 'file' for loading references"); |
||
| 72 | } |
||
| 73 | $fileName = $dsl['file']; |
||
| 74 | |||
| 75 | $overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false; |
||
| 76 | |||
| 77 | $fileName = str_replace('{ENV}', $this->container->get('kernel')->getEnvironment(), $fileName); |
||
| 78 | |||
| 79 | if (!is_file($fileName) && is_file(dirname($context['path']) . '/references/' . $fileName)) { |
||
| 80 | $fileName = dirname($context['path']) . '/references/' . $fileName; |
||
| 81 | } |
||
| 82 | |||
| 83 | if (!is_file($fileName)) { |
||
| 84 | throw new \Exception("Invalid step definition: invalid file '$fileName' for loading references"); |
||
| 85 | } |
||
| 86 | $data = file_get_contents($fileName); |
||
| 87 | |||
| 88 | $ext = pathinfo($dsl['file'], PATHINFO_EXTENSION); |
||
| 89 | switch ($ext) { |
||
| 90 | case 'json': |
||
| 91 | $data = json_decode($data, true); |
||
| 92 | break; |
||
| 93 | case 'yml': |
||
| 94 | case 'yaml': |
||
| 95 | $data = Yaml::parse($data); |
||
| 96 | break; |
||
| 97 | default: |
||
| 98 | throw new \Exception("Invalid step definition: unsupported file extension for loading references from"); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!is_array($data)) { |
||
| 102 | throw new \Exception("Invalid step definition: file does not contain an array of key/value pairs"); |
||
| 103 | } |
||
| 104 | |||
| 105 | foreach ($data as $refName => $value) { |
||
| 106 | if (preg_match('/%.+%$/', $value)) { |
||
| 107 | $value = $this->container->getParameter(trim($value, '%')); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!$this->referenceResolver->addReference($refName, $value, $overwrite)) { |
||
| 111 | throw new \Exception("Failed adding to Reference Resolver the reference: $refName"); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return $data; |
||
| 116 | } |
||
| 117 | |||
| 175 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.