Conditions | 8 |
Paths | 18 |
Total Lines | 59 |
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 |
||
117 | public function getCss(): DataDisplayResponse { |
||
118 | $css = ''; |
||
119 | $imports = ''; |
||
120 | $userValues = $this->getUserValues(); |
||
121 | |||
122 | foreach ($userValues as $key => $scssFile) { |
||
123 | if ($scssFile !== false) { |
||
124 | $imports .= '@import "' . $scssFile . '";'; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | if ($imports !== '') { |
||
129 | $scss = new Compiler(); |
||
130 | $scss->setImportPaths([ |
||
131 | $this->appRoot . '/css/', |
||
132 | $this->serverRoot . '/core/css/' |
||
133 | ]); |
||
134 | |||
135 | // Continue after throw |
||
136 | $scss->setIgnoreErrors(true); |
||
137 | $scss->setFormatter(Crunched::class); |
||
138 | |||
139 | // Import theme, variables and compile css4 variables |
||
140 | try { |
||
141 | $css .= $scss->compile( |
||
142 | $imports . |
||
143 | '@import "variables.scss";' . |
||
144 | '@import "css-variables.scss";' |
||
145 | ); |
||
146 | } catch (ParserException $e) { |
||
147 | $this->logger->error($e->getMessage(), ['app' => 'core']); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | // We don't want to override vars with url since path is different |
||
152 | $css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css); |
||
153 | |||
154 | // Rebase all urls |
||
155 | $appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT)); |
||
156 | $css = $this->rebaseUrls($css, $appWebRoot . '/css'); |
||
157 | |||
158 | if (in_array('themedark', $userValues) && $this->iconsCacher->getCachedCSS() && $this->iconsCacher->getCachedCSS()->getSize() > 0) { |
||
159 | $iconsCss = $this->invertSvgIconsColor($this->iconsCacher->getCachedCSS()->getContent()); |
||
160 | $css = $css . $iconsCss; |
||
161 | } |
||
162 | |||
163 | $response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']); |
||
164 | |||
165 | // Set cache control |
||
166 | $ttl = 31536000; |
||
167 | $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable'); |
||
168 | $expires = new \DateTime(); |
||
169 | $expires->setTimestamp($this->timeFactory->getTime()); |
||
170 | $expires->add(new \DateInterval('PT' . $ttl . 'S')); |
||
171 | $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
||
172 | $response->addHeader('Pragma', 'cache'); |
||
173 | |||
174 | return $response; |
||
175 | } |
||
176 | |||
224 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: