Conditions | 6 |
Paths | 9 |
Total Lines | 52 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
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 |
||
104 | public function createAssets(array $files) |
||
105 | { |
||
106 | $wwwRoot = getcwd(); |
||
107 | |||
108 | $assets = []; |
||
109 | |||
110 | // Scan folder and gather |
||
111 | foreach ($files as $file) { |
||
112 | // Generate cached resource path with possible new extension after compiling |
||
113 | $assets[$file] = $this->getAssetPathData($file); |
||
114 | $extension = pathinfo($assets[$file], PATHINFO_EXTENSION); |
||
115 | |||
116 | // If cached assets was modified or new |
||
117 | if (!file_exists($assets[$file]) || filemtime($file) !== filemtime($assets[$file])) { |
||
118 | // Read asset content |
||
119 | $this->cache[$file] = file_get_contents($file); |
||
120 | |||
121 | // Fire event for analyzing resource |
||
122 | Event::fire(self::E_RESOURCE_PRELOAD, [$file, pathinfo($file, PATHINFO_EXTENSION), &$this->cache[$file]]); |
||
123 | } else { |
||
124 | // Add this resource to resource collection grouped by resource type |
||
125 | $this->resources[$extension][] = $assets[$file]; |
||
126 | $this->resourceUrls[$extension][] = str_replace($wwwRoot, '', $assets[$file]); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | $wwwRoot = getcwd(); |
||
131 | foreach ($this->cache as $file => $content) { |
||
132 | $extension = pathinfo($file, PATHINFO_EXTENSION); |
||
133 | |||
134 | $compiled = $content; |
||
135 | Event::fire(self::E_RESOURCE_COMPILE, [$file, &$extension, &$compiled]); |
||
136 | |||
137 | // Create folder structure and file only if it is not empty |
||
138 | $resource = $this->getAssetPathData($file, $extension); |
||
139 | |||
140 | // Create cache path |
||
141 | $path = dirname($resource); |
||
142 | if (!file_exists($path)) { |
||
143 | mkdir($path, 0777, true); |
||
144 | } |
||
145 | |||
146 | file_put_contents($resource, $compiled); |
||
147 | |||
148 | // Sync cached file with source file |
||
149 | touch($resource, filemtime($file)); |
||
150 | |||
151 | // Add this resource to resource collection grouped by resource type |
||
152 | $this->resources[$extension][] = $resource; |
||
153 | $this->resourceUrls[$extension][] = str_replace($wwwRoot, '', $resource); |
||
154 | } |
||
155 | } |
||
156 | |||
185 |
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: