| Conditions | 5 |
| Paths | 9 |
| Total Lines | 54 |
| 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 |
||
| 111 | public function getCss(): DataDisplayResponse { |
||
| 112 | $css = ''; |
||
| 113 | $imports = ''; |
||
| 114 | $userValues = $this->getUserValues(); |
||
| 115 | |||
| 116 | foreach ($userValues as $key => $scssFile) { |
||
| 117 | if ($scssFile !== false) { |
||
| 118 | $imports .= '@import "' . $scssFile . '";'; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | if ($imports !== '') { |
||
| 123 | $scss = new Compiler(); |
||
| 124 | $scss->setImportPaths([ |
||
| 125 | $this->appRoot . '/css/', |
||
| 126 | $this->serverRoot . '/core/css/' |
||
| 127 | ]); |
||
| 128 | |||
| 129 | // Continue after throw |
||
| 130 | $scss->setIgnoreErrors(true); |
||
| 131 | $scss->setFormatter(Crunched::class); |
||
| 132 | |||
| 133 | // Import theme, variables and compile css4 variables |
||
| 134 | try { |
||
| 135 | $css .= $scss->compile( |
||
| 136 | $imports . |
||
| 137 | '@import "variables.scss";' . |
||
| 138 | '@import "css-variables.scss";' |
||
| 139 | ); |
||
| 140 | } catch (ParserException $e) { |
||
| 141 | $this->logger->error($e->getMessage(), ['app' => 'core']); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | // We don't want to override vars with url since path is different |
||
| 146 | $css = $this->filterOutRule('/--[a-z-:]+url\([^;]+\)/mi', $css); |
||
| 147 | |||
| 148 | // Rebase all urls |
||
| 149 | $appWebRoot = substr($this->appRoot, strlen($this->serverRoot) - strlen(\OC::$WEBROOT)); |
||
| 150 | $css = $this->rebaseUrls($css, $appWebRoot . '/css'); |
||
| 151 | |||
| 152 | $response = new DataDisplayResponse($css, Http::STATUS_OK, ['Content-Type' => 'text/css']); |
||
| 153 | |||
| 154 | // Set cache control |
||
| 155 | $ttl = 31536000; |
||
| 156 | $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable'); |
||
| 157 | $expires = new \DateTime(); |
||
| 158 | $expires->setTimestamp($this->timeFactory->getTime()); |
||
| 159 | $expires->add(new \DateInterval('PT' . $ttl . 'S')); |
||
| 160 | $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
||
| 161 | $response->addHeader('Pragma', 'cache'); |
||
| 162 | |||
| 163 | return $response; |
||
| 164 | } |
||
| 165 | |||
| 203 |
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: