| Conditions | 7 |
| Paths | 32 |
| Total Lines | 56 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 114 | public function createAssets(array $paths, $type) |
||
| 115 | { |
||
| 116 | // Gather all resource files for this type |
||
| 117 | $files = []; |
||
| 118 | foreach ($paths as $path) { |
||
| 119 | $files = array_filter(array_merge($this->scanFolderRecursively($path, $type), $files)); |
||
| 120 | } |
||
| 121 | |||
| 122 | // Create resources timestamps |
||
| 123 | $timeStamps = []; |
||
| 124 | foreach ($files as $file) { |
||
| 125 | $timeStamps[$file] = filemtime($file); |
||
| 126 | } |
||
| 127 | |||
| 128 | // Generate resources cache stamp by hashing combined files modification timestamp |
||
| 129 | $cacheStamp = md5(implode('', $timeStamps)); |
||
| 130 | |||
| 131 | // TODO: We need cache for list of files to check if we need to preload them by storing modified date |
||
| 132 | |||
| 133 | // Here we need to prepare resource - gather LESS variables for example |
||
| 134 | foreach ($files as $file) { |
||
| 135 | // Fire event for preloading resource |
||
| 136 | Event::fire(self::E_RESOURCE_PRELOAD, [$file, pathinfo($file, PATHINFO_EXTENSION)]); |
||
| 137 | } |
||
| 138 | |||
| 139 | $wwwRoot = getcwd(); |
||
| 140 | $projectRoot = dirname($wwwRoot).'/'; |
||
| 141 | |||
| 142 | // Here we can compile resources |
||
| 143 | foreach ($files as $file) { |
||
| 144 | $extension = pathinfo($file, PATHINFO_EXTENSION); |
||
| 145 | $fileName = pathinfo($file, PATHINFO_FILENAME); |
||
| 146 | $relativePath = str_replace($projectRoot, '', $file); |
||
| 147 | |||
| 148 | // Compiled resource |
||
| 149 | $compiled = ''; |
||
| 150 | Event::fire(self::E_RESOURCE_COMPILE, [$file, &$extension, &$compiled]); |
||
| 151 | |||
| 152 | // Generate cached resource path with possible new extension after compiling |
||
| 153 | $resource = dirname($this->cache_path.$relativePath).'/'.$fileName.'.'.$extension; |
||
| 154 | |||
| 155 | // Create folder structure and file only if it is not empty |
||
| 156 | if (strlen($compiled)) { |
||
| 157 | // Create cache path |
||
| 158 | $path = dirname($resource); |
||
| 159 | if (!file_exists($path)) { |
||
| 160 | mkdir($path, 0777, true); |
||
| 161 | } |
||
| 162 | file_put_contents($resource, $compiled); |
||
| 163 | |||
| 164 | // Add this resource to resource collection grouped by resource type |
||
| 165 | $this->resources[$extension][] = $resource; |
||
| 166 | $this->resourceUrls[$extension][] = str_replace($wwwRoot, '', $resource); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 199 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: