Conditions | 16 |
Paths | 118 |
Total Lines | 53 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
69 | public function locateResource($name, $dir = null, $first = true) |
||
70 | { |
||
71 | if ('@' !== $name[0]) { |
||
72 | throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name)); |
||
73 | } |
||
74 | |||
75 | if (false !== strpos($name, '..')) { |
||
76 | throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name)); |
||
77 | } |
||
78 | $moduleName = substr($name, 1); |
||
79 | $path = ''; |
||
80 | if (false !== strpos($moduleName, '/')) { |
||
81 | list($moduleName, $path) = explode('/', $moduleName, 2); |
||
82 | } |
||
83 | |||
84 | |||
85 | $isResource = 0 === strpos($path, 'Resources') && null !== $dir; |
||
86 | $overridePath = substr($path, 9); |
||
87 | $resourceModule = null; |
||
88 | $modules = array($this->getModule($moduleName)); |
||
89 | $files = array(); |
||
90 | |||
91 | foreach ($modules as $module) { |
||
92 | if ($isResource && file_exists($file = $dir . '/' . $module->getName() . $overridePath)) { |
||
93 | if (null !== $resourceModule) { |
||
94 | throw new \RuntimeException(sprintf('"%s" resource is hidden by a resource from the "%s" derived module. Create a "%s" file to override the module resource.', |
||
95 | $file, |
||
96 | $resourceModule, |
||
97 | $dir . '/' . $modules[0]->getName() . $overridePath |
||
98 | )); |
||
99 | } |
||
100 | |||
101 | if ($first) { |
||
102 | return $file; |
||
103 | } |
||
104 | $files[] = $file; |
||
105 | } |
||
106 | |||
107 | if (file_exists($file = $this->getResourcesPath($module) . '/' . $path)) { |
||
108 | if ($first && !$isResource) { |
||
109 | return $file; |
||
110 | } |
||
111 | $files[] = $file; |
||
112 | $resourceModule = $module->getName(); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | if (count($files) > 0) { |
||
117 | return $first && $isResource ? $files[0] : $files; |
||
118 | } |
||
119 | |||
120 | throw new \InvalidArgumentException(sprintf('Unable to find file "%s".', $name)); |
||
121 | } |
||
122 | |||
133 |
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.