| Total Complexity | 49 |
| Total Lines | 383 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Compiler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Compiler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Compiler extends AbstractProvider |
||
| 26 | {
|
||
| 27 | /** |
||
| 28 | * Compiler::$config |
||
| 29 | * |
||
| 30 | * Compiler Config |
||
| 31 | * |
||
| 32 | * @var DataStructures\Config |
||
| 33 | */ |
||
| 34 | private $config; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Compiler::$sourceFilePath |
||
| 38 | * |
||
| 39 | * Compiler Source File Path |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $sourceFilePath; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Compiler::$sourceFileDirectory |
||
| 47 | * |
||
| 48 | * Compiler Source File Directory |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $sourceFileDirectory; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Compiler::$sourceString |
||
| 56 | * |
||
| 57 | * Compiler Source String |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $sourceString; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Compiler::$vars |
||
| 65 | * |
||
| 66 | * Compiler Vars |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private $vars = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Compiler::$template |
||
| 74 | * |
||
| 75 | * @var \O2System\Parser\Template\Collection |
||
| 76 | */ |
||
| 77 | private $template; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Compiler::$string |
||
| 81 | * |
||
| 82 | * @var \O2System\Parser\String\Collection |
||
| 83 | */ |
||
| 84 | private $string; |
||
| 85 | |||
| 86 | // ------------------------------------------------------------------------ |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Compiler::__construct |
||
| 90 | * |
||
| 91 | * @param DataStructures\Config $config |
||
| 92 | */ |
||
| 93 | public function __construct(DataStructures\Config $config) |
||
| 94 | {
|
||
| 95 | language() |
||
| 96 | ->addFilePath(__DIR__ . DIRECTORY_SEPARATOR) |
||
| 97 | ->loadFile('parser');
|
||
| 98 | |||
| 99 | $this->template = new Template\Collection(); |
||
| 100 | $this->string = new String\Collection(); |
||
| 101 | |||
| 102 | $this->config = $config; |
||
| 103 | |||
| 104 | if ($this->config->offsetExists('template')) {
|
||
| 105 | if(is_array($this->config->template)) {
|
||
| 106 | foreach($this->config->template as $engine) {
|
||
| 107 | $this->template->load($engine); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($this->config->offsetExists('string')) {
|
||
| 113 | if(is_array($this->config->string)) {
|
||
| 114 | foreach($this->config->string as $engine) {
|
||
| 115 | $this->string->load($engine); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // ------------------------------------------------------------------------ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Compiler::getSourceString |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getSourceString() |
||
| 129 | {
|
||
| 130 | return $this->sourceString; |
||
| 131 | } |
||
| 132 | |||
| 133 | // ------------------------------------------------------------------------ |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Compiler::loadFile |
||
| 137 | * |
||
| 138 | * @param string $filePath |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function loadFile($filePath) |
||
| 143 | {
|
||
| 144 | if ($filePath instanceof \SplFileInfo) {
|
||
| 145 | $filePath = $filePath->getRealPath(); |
||
| 146 | } |
||
| 147 | |||
| 148 | if (isset($this->sourceFileDirectory)) {
|
||
| 149 | if (is_file($this->sourceFileDirectory . $filePath)) {
|
||
| 150 | $filePath = $this->sourceFileDirectory . $filePath; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | if (is_file($filePath)) {
|
||
| 155 | $this->sourceFilePath = realpath($filePath); |
||
| 156 | $this->sourceFileDirectory = pathinfo($this->sourceFilePath, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR; |
||
| 157 | |||
| 158 | return $this->loadString(file_get_contents($filePath)); |
||
| 159 | } |
||
| 160 | |||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | // ------------------------------------------------------------------------ |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Compiler::loadString |
||
| 168 | * |
||
| 169 | * @param string $string |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | public function loadString($string) |
||
| 174 | {
|
||
| 175 | $this->sourceString = $string; |
||
| 176 | |||
| 177 | if ($this->config->allowPhpScripts === false) {
|
||
| 178 | $this->sourceString = preg_replace( |
||
| 179 | '/<\\?.*(\\?>|$)/Us', |
||
| 180 | '', |
||
| 181 | str_replace('<?=', '<?php echo ', $this->sourceString)
|
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | $this->sourceString = str_replace( |
||
| 186 | [ |
||
| 187 | '__DIR__', |
||
| 188 | '__FILE__', |
||
| 189 | ], |
||
| 190 | [ |
||
| 191 | "'" . $this->getSourceFileDirectory() . "'", |
||
| 192 | "'" . $this->getSourceFilePath() . "'", |
||
| 193 | ], |
||
| 194 | $this->sourceString |
||
| 195 | ); |
||
| 196 | |||
| 197 | return empty($this->sourceString); |
||
| 198 | } |
||
| 199 | |||
| 200 | // ------------------------------------------------------------------------ |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Compiler::getSourceFileDirectory |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getSourceFileDirectory() |
||
| 208 | {
|
||
| 209 | return $this->sourceFileDirectory; |
||
| 210 | } |
||
| 211 | |||
| 212 | // ------------------------------------------------------------------------ |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Compiler::getSourceFilePath |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getSourceFilePath() |
||
| 222 | } |
||
| 223 | |||
| 224 | // ------------------------------------------------------------------------ |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Compiler::parse |
||
| 228 | * |
||
| 229 | * @param array $vars |
||
| 230 | * |
||
| 231 | * @return bool|string Returns FALSE if failed. |
||
| 232 | */ |
||
| 233 | public function parse(array $vars = []) |
||
| 234 | {
|
||
| 235 | $this->loadVars($vars); |
||
| 236 | |||
| 237 | $output = $this->parsePhp(); |
||
| 238 | |||
| 239 | // Run Template Engines |
||
| 240 | $output = $this->template->parse($output, $this->vars); |
||
| 241 | |||
| 242 | // Run String Engines |
||
| 243 | $output = $this->string->parse($output, $this->vars); |
||
| 244 | |||
| 245 | return $output; |
||
| 246 | } |
||
| 247 | |||
| 248 | // ------------------------------------------------------------------------ |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Compiler::parsePhp |
||
| 252 | * |
||
| 253 | * @param array $vars |
||
| 254 | * |
||
| 255 | * @return bool|string Returns FALSE if failed |
||
| 256 | */ |
||
| 257 | public function parsePhp(array $vars = []) |
||
| 258 | {
|
||
| 259 | if(count($vars)) {
|
||
| 260 | $this->loadVars($vars); |
||
| 261 | } |
||
| 262 | |||
| 263 | extract($this->vars); |
||
| 264 | |||
| 265 | /* |
||
| 266 | * Buffer the output |
||
| 267 | * |
||
| 268 | * We buffer the output for two reasons: |
||
| 269 | * 1. Speed. You get a significant speed boost. |
||
| 270 | * 2. So that the final rendered template can be post-processed by |
||
| 271 | * the output class. Why do we need post processing? For one thing, |
||
| 272 | * in order to show the elapsed page load time. Unless we can |
||
| 273 | * intercept the content right before it's sent to the browser and |
||
| 274 | * then stop the timer it won't be accurate. |
||
| 275 | */ |
||
| 276 | ob_start(); |
||
| 277 | |||
| 278 | echo eval('?>' . str_replace([';?>', ')?>', ');?>'], ['; ?>', '); ?>', '); ?>'], $this->sourceString));
|
||
| 279 | |||
| 280 | $output = ob_get_contents(); |
||
| 281 | @ob_end_clean(); |
||
| 282 | |||
| 283 | $lastError = error_get_last(); |
||
| 284 | |||
| 285 | if (is_array($lastError)) {
|
||
| 286 | $this->errorFilePath = $this->getSourceFilePath(); |
||
| 287 | } |
||
| 288 | |||
| 289 | return $output; |
||
| 290 | } |
||
| 291 | |||
| 292 | // ------------------------------------------------------------------------ |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Compiler::loadVars |
||
| 296 | * |
||
| 297 | * @param array $vars |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public function loadVars(array $vars) |
||
| 306 | } |
||
| 307 | |||
| 308 | // ------------------------------------------------------------------------ |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Drivers::validateSyntax |
||
| 312 | * |
||
| 313 | * Check the syntax of some PHP code. |
||
| 314 | * |
||
| 315 | * @param string $sourceCode PHP code to check. |
||
| 316 | * |
||
| 317 | * @return bool|array If false, then check was successful, otherwise an array(message,line) of errors is |
||
| 318 | * returned. |
||
| 319 | */ |
||
| 320 | function validateSyntax($sourceCode) |
||
| 408 | } |
||
| 409 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths