Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Parser 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Parser |
||
| 21 | { |
||
| 22 | |||
| 23 | static protected $nextBlockId = 0; // used to uniquely identify blocks |
||
| 24 | |||
| 25 | static protected $precedence = [ |
||
| 26 | '=<' => 0, |
||
| 27 | '>=' => 0, |
||
| 28 | '=' => 0, |
||
| 29 | '<' => 0, |
||
| 30 | '>' => 0, |
||
| 31 | |||
| 32 | '+' => 1, |
||
| 33 | '-' => 1, |
||
| 34 | '*' => 2, |
||
| 35 | '/' => 2, |
||
| 36 | '%' => 2, |
||
| 37 | ]; |
||
| 38 | |||
| 39 | static protected $whitePattern; |
||
| 40 | static protected $commentMulti; |
||
| 41 | |||
| 42 | static protected $commentSingle = "//"; |
||
| 43 | static protected $commentMultiLeft = "/*"; |
||
| 44 | static protected $commentMultiRight = "*/"; |
||
| 45 | |||
| 46 | // regex string to match any of the operators |
||
| 47 | static protected $operatorString; |
||
| 48 | |||
| 49 | // these properties will supress division unless it's inside parenthases |
||
| 50 | static protected $supressDivisionProps = |
||
| 51 | ['/border-radius$/i', '/^font$/i']; |
||
| 52 | |||
| 53 | private $blockDirectives = [ |
||
| 54 | 'font-face', |
||
| 55 | 'keyframes', |
||
| 56 | 'page', |
||
| 57 | '-moz-document', |
||
| 58 | 'viewport', |
||
| 59 | '-moz-viewport', |
||
| 60 | '-o-viewport', |
||
| 61 | '-ms-viewport', |
||
| 62 | ]; |
||
| 63 | private $lineDirectives = ['charset']; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * if we are in parens we can be more liberal with whitespace around |
||
| 67 | * operators because it must evaluate to a single value and thus is less |
||
| 68 | * ambiguous. |
||
| 69 | * |
||
| 70 | * Consider: |
||
| 71 | * property1: 10 -5; // is two numbers, 10 and -5 |
||
| 72 | * property2: (10 -5); // should evaluate to 5 |
||
| 73 | */ |
||
| 74 | protected $inParens = false; |
||
| 75 | |||
| 76 | // caches preg escaped literals |
||
| 77 | static protected $literalCache = []; |
||
| 78 | /** @var int */ |
||
| 79 | public $count; |
||
| 80 | /** @var int */ |
||
| 81 | private $line; |
||
| 82 | /** @var array */ |
||
| 83 | private $seenComments; |
||
| 84 | /** @var string */ |
||
| 85 | public $buffer; |
||
| 86 | |||
| 87 | private $env; |
||
| 88 | /** @var bool */ |
||
| 89 | private $inExp; |
||
| 90 | /** @var string */ |
||
| 91 | private $currentProperty; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | private $writeComments = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Parser constructor. |
||
| 100 | * |
||
| 101 | * @param \LesserPhp\Compiler $lessc |
||
| 102 | * @param null $sourceName |
||
| 103 | */ |
||
| 104 | 49 | public function __construct(Compiler $lessc, $sourceName = null) |
|
| 105 | { |
||
| 106 | 49 | $this->eatWhiteDefault = true; |
|
|
|
|||
| 107 | // reference to less needed for vPrefix, mPrefix, and parentSelector |
||
| 108 | 49 | $this->lessc = $lessc; |
|
| 109 | |||
| 110 | 49 | $this->sourceName = $sourceName; // name used for error messages |
|
| 111 | |||
| 112 | 49 | if (!self::$operatorString) { |
|
| 113 | 1 | self::$operatorString = |
|
| 114 | 1 | '(' . implode('|', array_map([Compiler::class, 'pregQuote'], array_keys(self::$precedence))) . ')'; |
|
| 115 | |||
| 116 | 1 | $commentSingle = Compiler::pregQuote(self::$commentSingle); |
|
| 117 | 1 | $commentMultiLeft = Compiler::pregQuote(self::$commentMultiLeft); |
|
| 118 | 1 | $commentMultiRight = Compiler::pregQuote(self::$commentMultiRight); |
|
| 119 | |||
| 120 | 1 | self::$commentMulti = $commentMultiLeft . '.*?' . $commentMultiRight; |
|
| 121 | 1 | self::$whitePattern = '/' . $commentSingle . '[^\n]*\s*|(' . self::$commentMulti . ')\s*|\s+/Ais'; |
|
| 122 | } |
||
| 123 | 49 | } |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param $buffer |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | * @throws \LesserPhp\Exception\GeneralException |
||
| 130 | */ |
||
| 131 | 49 | public function parse($buffer) |
|
| 132 | { |
||
| 133 | 49 | $this->count = 0; |
|
| 134 | 49 | $this->line = 1; |
|
| 135 | |||
| 136 | 49 | $this->env = null; // block stack |
|
| 137 | 49 | $this->buffer = $this->writeComments ? $buffer : $this->removeComments($buffer); |
|
| 138 | 49 | $this->pushSpecialBlock('root'); |
|
| 139 | 49 | $this->eatWhiteDefault = true; |
|
| 140 | 49 | $this->seenComments = []; |
|
| 141 | |||
| 142 | 49 | $this->whitespace(); |
|
| 143 | |||
| 144 | // parse the entire file |
||
| 145 | 49 | while (false !== $this->parseChunk()) { |
|
| 146 | ; |
||
| 147 | } |
||
| 148 | |||
| 149 | 49 | if ($this->count !== strlen($this->buffer)) { |
|
| 150 | // var_dump($this->count); |
||
| 151 | // var_dump($this->buffer); |
||
| 152 | $this->throwError(); |
||
| 153 | } |
||
| 154 | |||
| 155 | // TODO report where the block was opened |
||
| 156 | 49 | if (!property_exists($this->env, 'parent') || $this->env->parent !== null) { |
|
| 157 | throw new GeneralException('parse error: unclosed block'); |
||
| 158 | } |
||
| 159 | |||
| 160 | 49 | return $this->env; |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Parse a single chunk off the head of the buffer and append it to the |
||
| 165 | * current parse environment. |
||
| 166 | * Returns false when the buffer is empty, or when there is an error. |
||
| 167 | * |
||
| 168 | * This function is called repeatedly until the entire document is |
||
| 169 | * parsed. |
||
| 170 | * |
||
| 171 | * This parser is most similar to a recursive descent parser. Single |
||
| 172 | * functions represent discrete grammatical rules for the language, and |
||
| 173 | * they are able to capture the text that represents those rules. |
||
| 174 | * |
||
| 175 | * Consider the function \LesserPhp\Compiler::keyword(). (all parse functions are |
||
| 176 | * structured the same) |
||
| 177 | * |
||
| 178 | * The function takes a single reference argument. When calling the |
||
| 179 | * function it will attempt to match a keyword on the head of the buffer. |
||
| 180 | * If it is successful, it will place the keyword in the referenced |
||
| 181 | * argument, advance the position in the buffer, and return true. If it |
||
| 182 | * fails then it won't advance the buffer and it will return false. |
||
| 183 | * |
||
| 184 | * All of these parse functions are powered by \LesserPhp\Compiler::match(), which behaves |
||
| 185 | * the same way, but takes a literal regular expression. Sometimes it is |
||
| 186 | * more convenient to use match instead of creating a new function. |
||
| 187 | * |
||
| 188 | * Because of the format of the functions, to parse an entire string of |
||
| 189 | * grammatical rules, you can chain them together using &&. |
||
| 190 | * |
||
| 191 | * But, if some of the rules in the chain succeed before one fails, then |
||
| 192 | * the buffer position will be left at an invalid state. In order to |
||
| 193 | * avoid this, \LesserPhp\Compiler::seek() is used to remember and set buffer positions. |
||
| 194 | * |
||
| 195 | * Before parsing a chain, use $s = $this->seek() to remember the current |
||
| 196 | * position into $s. Then if a chain fails, use $this->seek($s) to |
||
| 197 | * go back where we started. |
||
| 198 | * @throws \LesserPhp\Exception\GeneralException |
||
| 199 | */ |
||
| 200 | 49 | protected function parseChunk() |
|
| 201 | { |
||
| 202 | 49 | if (empty($this->buffer)) { |
|
| 203 | return false; |
||
| 204 | } |
||
| 205 | 49 | $s = $this->seek(); |
|
| 206 | |||
| 207 | 49 | if ($this->whitespace()) { |
|
| 208 | 46 | return true; |
|
| 209 | } |
||
| 210 | |||
| 211 | // setting a property |
||
| 212 | 49 | View Code Duplication | if ($this->keyword($key) && $this->assign() && $this->propertyValue($value, $key) && $this->end()) { |
| 213 | 47 | $this->append(['assign', $key, $value], $s); |
|
| 214 | |||
| 215 | 47 | return true; |
|
| 216 | } else { |
||
| 217 | 49 | $this->seek($s); |
|
| 218 | } |
||
| 219 | |||
| 220 | // look for special css blocks |
||
| 221 | 49 | if ($this->literal('@', false)) { |
|
| 222 | 26 | $this->count--; |
|
| 223 | |||
| 224 | // media |
||
| 225 | 26 | if ($this->literal('@media')) { |
|
| 226 | 3 | return $this->handleLiteralMedia($s); |
|
| 227 | } |
||
| 228 | |||
| 229 | 26 | if ($this->literal('@', false) && $this->keyword($directiveName)) { |
|
| 230 | 26 | if ($this->isDirective($directiveName, $this->blockDirectives)) { |
|
| 231 | 4 | if ($this->handleDirectiveBlock($directiveName) === true) { |
|
| 232 | 4 | return true; |
|
| 233 | } |
||
| 234 | 25 | } elseif ($this->isDirective($directiveName, $this->lineDirectives)) { |
|
| 235 | 1 | if ($this->handleDirectiveLine($directiveName) === true) { |
|
| 236 | 1 | return true; |
|
| 237 | } |
||
| 238 | 25 | } elseif ($this->literal(':', true)) { |
|
| 239 | 24 | if ($this->handleRulesetDefinition($directiveName) === true) { |
|
| 240 | 1 | return true; |
|
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | 25 | $this->seek($s); |
|
| 246 | } |
||
| 247 | |||
| 248 | 49 | if ($this->literal('&', false)) { |
|
| 249 | 4 | $this->count--; |
|
| 250 | 4 | if ($this->literal('&:extend')) { |
|
| 251 | // hierauf folgt was in runden klammern, und zwar das element, das erweitert werden soll |
||
| 252 | // heißt also, das was in klammern steht wird um die aktuellen klassen erweitert |
||
| 253 | /* |
||
| 254 | Aus |
||
| 255 | |||
| 256 | nav ul { |
||
| 257 | &:extend(.inline); |
||
| 258 | background: blue; |
||
| 259 | } |
||
| 260 | .inline { |
||
| 261 | color: red; |
||
| 262 | } |
||
| 263 | |||
| 264 | |||
| 265 | Wird: |
||
| 266 | |||
| 267 | nav ul { |
||
| 268 | background: blue; |
||
| 269 | } |
||
| 270 | .inline, |
||
| 271 | nav ul { |
||
| 272 | color: red; |
||
| 273 | } |
||
| 274 | |||
| 275 | */ |
||
| 276 | // echo "Here we go"; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | |||
| 281 | // setting a variable |
||
| 282 | 49 | View Code Duplication | if ($this->variable($var) && $this->assign() && |
| 283 | 49 | $this->propertyValue($value) && $this->end() |
|
| 284 | ) { |
||
| 285 | 23 | $this->append(['assign', $var, $value], $s); |
|
| 286 | |||
| 287 | 23 | return true; |
|
| 288 | } else { |
||
| 289 | 49 | $this->seek($s); |
|
| 290 | } |
||
| 291 | |||
| 292 | 49 | if ($this->import($importValue)) { |
|
| 293 | 3 | $this->append($importValue, $s); |
|
| 294 | |||
| 295 | 3 | return true; |
|
| 296 | } |
||
| 297 | |||
| 298 | // opening parametric mixin |
||
| 299 | 49 | if ($this->tag($tag, true) && $this->argumentDef($args, $isVararg) && |
|
| 300 | 49 | ($this->guards($guards) || true) && |
|
| 301 | 49 | $this->literal('{') |
|
| 302 | ) { |
||
| 303 | 18 | $block = $this->pushBlock($this->fixTags([$tag])); |
|
| 304 | 18 | $block->args = $args; |
|
| 305 | 18 | $block->isVararg = $isVararg; |
|
| 306 | 18 | if (!empty($guards)) { |
|
| 307 | 5 | $block->guards = $guards; |
|
| 308 | } |
||
| 309 | |||
| 310 | 18 | return true; |
|
| 311 | } else { |
||
| 312 | 49 | $this->seek($s); |
|
| 313 | } |
||
| 314 | |||
| 315 | // opening a simple block |
||
| 316 | 49 | if ($this->tags($tags) && $this->literal('{', false)) { |
|
| 317 | 46 | $tags = $this->fixTags($tags); |
|
| 318 | 46 | $this->pushBlock($tags); |
|
| 319 | |||
| 320 | 46 | return true; |
|
| 321 | } else { |
||
| 322 | 49 | $this->seek($s); |
|
| 323 | } |
||
| 324 | |||
| 325 | // closing a block |
||
| 326 | 49 | if ($this->literal('}', false)) { |
|
| 327 | try { |
||
| 328 | 46 | $block = $this->pop(); |
|
| 329 | } catch (\Exception $e) { |
||
| 330 | $this->seek($s); |
||
| 331 | $this->throwError($e->getMessage()); |
||
| 332 | } |
||
| 333 | |||
| 334 | 46 | $hidden = false; |
|
| 335 | 46 | if ($block->type === null) { |
|
| 336 | 46 | $hidden = true; |
|
| 337 | 46 | if (!isset($block->args)) { |
|
| 338 | 46 | foreach ($block->tags as $tag) { |
|
| 339 | 46 | if (!is_string($tag) || $tag[0] !== $this->lessc->getMPrefix()) { |
|
| 340 | 46 | $hidden = false; |
|
| 341 | 46 | break; |
|
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | 46 | foreach ($block->tags as $tag) { |
|
| 347 | 46 | if (is_string($tag)) { |
|
| 348 | 46 | $this->env->children[$tag][] = $block; |
|
| 349 | } |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | 46 | if (!$hidden) { |
|
| 354 | 46 | $this->append(['block', $block], $s); |
|
| 355 | } |
||
| 356 | |||
| 357 | // this is done here so comments aren't bundled into he block that |
||
| 358 | // was just closed |
||
| 359 | 46 | $this->whitespace(); |
|
| 360 | |||
| 361 | 46 | return true; |
|
| 362 | } |
||
| 363 | |||
| 364 | // mixin |
||
| 365 | 49 | if ($this->mixinTags($tags) && |
|
| 366 | 49 | ($this->argumentDef($argv, $isVararg) || true) && |
|
| 367 | 49 | ($this->keyword($suffix) || true) && $this->end() |
|
| 368 | ) { |
||
| 369 | 22 | $tags = $this->fixTags($tags); |
|
| 370 | 22 | $this->append(['mixin', $tags, $argv, $suffix], $s); |
|
| 371 | |||
| 372 | 22 | return true; |
|
| 373 | } else { |
||
| 374 | 49 | $this->seek($s); |
|
| 375 | } |
||
| 376 | |||
| 377 | // spare ; |
||
| 378 | 49 | if ($this->literal(';')) { |
|
| 379 | 4 | return true; |
|
| 380 | } |
||
| 381 | |||
| 382 | 49 | return false; // got nothing, throw error |
|
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $directiveName |
||
| 387 | * @param array $directives |
||
| 388 | * |
||
| 389 | * @return bool |
||
| 390 | */ |
||
| 391 | 26 | protected function isDirective($directiveName, array $directives) |
|
| 392 | { |
||
| 393 | // TODO: cache pattern in parser |
||
| 394 | 26 | $pattern = implode('|', array_map([Compiler::class, 'pregQuote'], $directives)); |
|
| 395 | 26 | $pattern = '/^(-[a-z-]+-)?(' . $pattern . ')$/i'; |
|
| 396 | |||
| 397 | 26 | return (preg_match($pattern, $directiveName) === 1); |
|
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param array $tags |
||
| 402 | * |
||
| 403 | * @return mixed |
||
| 404 | */ |
||
| 405 | 46 | protected function fixTags(array $tags) |
|
| 406 | { |
||
| 407 | // move @ tags out of variable namespace |
||
| 408 | 46 | foreach ($tags as &$tag) { |
|
| 409 | 46 | if ($tag[0] === $this->lessc->getVPrefix()) { |
|
| 410 | 46 | $tag[0] = $this->lessc->getMPrefix(); |
|
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | 46 | return $tags; |
|
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * a list of expressions |
||
| 419 | * |
||
| 420 | * @param $exps |
||
| 421 | * |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | 48 | protected function expressionList(&$exps) |
|
| 425 | { |
||
| 426 | 48 | $values = []; |
|
| 427 | |||
| 428 | 48 | while ($this->expression($exp)) { |
|
| 429 | 48 | $values[] = $exp; |
|
| 430 | } |
||
| 431 | |||
| 432 | 48 | if (count($values) === 0) { |
|
| 433 | 26 | return false; |
|
| 434 | } |
||
| 435 | |||
| 436 | 48 | $exps = Compiler::compressList($values, ' '); |
|
| 437 | |||
| 438 | 48 | return true; |
|
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Attempt to consume an expression. |
||
| 443 | * @link http://en.wikipedia.org/wiki/Operator-precedence_parser#Pseudo-code |
||
| 444 | * |
||
| 445 | * @param $out |
||
| 446 | * |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | 48 | protected function expression(&$out) |
|
| 450 | { |
||
| 451 | 48 | if ($this->value($lhs)) { |
|
| 452 | 48 | $out = $this->expHelper($lhs, 0); |
|
| 453 | |||
| 454 | // look for / shorthand |
||
| 455 | 48 | if (!empty($this->env->supressedDivision)) { |
|
| 456 | 2 | unset($this->env->supressedDivision); |
|
| 457 | 2 | $s = $this->seek(); |
|
| 458 | 2 | if ($this->literal("/") && $this->value($rhs)) { |
|
| 459 | $out = [ |
||
| 460 | 2 | "list", |
|
| 461 | 2 | "", |
|
| 462 | 2 | [$out, ["keyword", "/"], $rhs], |
|
| 463 | ]; |
||
| 464 | } else { |
||
| 465 | $this->seek($s); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | 48 | return true; |
|
| 470 | } |
||
| 471 | |||
| 472 | 48 | return false; |
|
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * recursively parse infix equation with $lhs at precedence $minP |
||
| 477 | * |
||
| 478 | * @param $lhs |
||
| 479 | * @param $minP |
||
| 480 | * |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | 48 | protected function expHelper($lhs, $minP) |
|
| 484 | { |
||
| 485 | 48 | $this->inExp = true; |
|
| 486 | 48 | $ss = $this->seek(); |
|
| 487 | |||
| 488 | 48 | while (true) { |
|
| 489 | 48 | $whiteBefore = isset($this->buffer[$this->count - 1]) && ctype_space($this->buffer[$this->count - 1]); |
|
| 490 | |||
| 491 | // If there is whitespace before the operator, then we require |
||
| 492 | // whitespace after the operator for it to be an expression |
||
| 493 | 48 | $needWhite = $whiteBefore && !$this->inParens; |
|
| 494 | |||
| 495 | 48 | if ($this->match(self::$operatorString . ($needWhite ? '\s' : ''), $m) && |
|
| 496 | 48 | self::$precedence[$m[1]] >= $minP |
|
| 497 | ) { |
||
| 498 | 20 | if (!$this->inParens && |
|
| 499 | 20 | isset($this->env->currentProperty) && |
|
| 500 | 20 | $m[1] === '/' && |
|
| 501 | 20 | empty($this->env->supressedDivision) |
|
| 502 | ) { |
||
| 503 | 5 | foreach (self::$supressDivisionProps as $pattern) { |
|
| 504 | 5 | if (preg_match($pattern, $this->env->currentProperty)) { |
|
| 505 | 2 | $this->env->supressedDivision = true; |
|
| 506 | 5 | break 2; |
|
| 507 | } |
||
| 508 | } |
||
| 509 | } |
||
| 510 | |||
| 511 | 20 | $whiteAfter = isset($this->buffer[$this->count - 1]) && ctype_space($this->buffer[$this->count - 1]); |
|
| 512 | |||
| 513 | 20 | if (!$this->value($rhs)) { |
|
| 514 | break; |
||
| 515 | } |
||
| 516 | |||
| 517 | // peek for next operator to see what to do with rhs |
||
| 518 | 20 | if ($this->peek(self::$operatorString, $next) && |
|
| 519 | 20 | self::$precedence[$next[1]] > self::$precedence[$m[1]] |
|
| 520 | ) { |
||
| 521 | 1 | $rhs = $this->expHelper($rhs, self::$precedence[$next[1]]); |
|
| 522 | } |
||
| 523 | |||
| 524 | 20 | $lhs = ['expression', $m[1], $lhs, $rhs, $whiteBefore, $whiteAfter]; |
|
| 525 | 20 | $ss = $this->seek(); |
|
| 526 | |||
| 527 | 20 | continue; |
|
| 528 | } |
||
| 529 | |||
| 530 | 48 | break; |
|
| 531 | } |
||
| 532 | |||
| 533 | 48 | $this->seek($ss); |
|
| 534 | |||
| 535 | 48 | return $lhs; |
|
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * consume a list of values for a property |
||
| 540 | * |
||
| 541 | * @param $value |
||
| 542 | * @param string $keyName |
||
| 543 | * |
||
| 544 | * @return bool |
||
| 545 | */ |
||
| 546 | 48 | public function propertyValue(&$value, $keyName = null) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * @param $out |
||
| 582 | * |
||
| 583 | * @return bool |
||
| 584 | */ |
||
| 585 | 48 | protected function parenValue(&$out) |
|
| 586 | { |
||
| 587 | 48 | $s = $this->seek(); |
|
| 610 | |||
| 611 | /** |
||
| 612 | * a single value |
||
| 613 | * |
||
| 614 | * @param array $value |
||
| 615 | * |
||
| 616 | * @return bool |
||
| 617 | */ |
||
| 618 | 48 | protected function value(&$value) |
|
| 687 | |||
| 688 | /** |
||
| 689 | * an import statement |
||
| 690 | * |
||
| 691 | * @param array $out |
||
| 692 | * |
||
| 693 | * @return bool|null |
||
| 694 | */ |
||
| 695 | 49 | protected function import(&$out) |
|
| 713 | |||
| 714 | /** |
||
| 715 | * @param $out |
||
| 716 | * |
||
| 717 | * @return bool |
||
| 718 | */ |
||
| 719 | 3 | protected function mediaQueryList(&$out) |
|
| 729 | |||
| 730 | /** |
||
| 731 | * @param $out |
||
| 732 | * |
||
| 733 | * @return bool |
||
| 734 | */ |
||
| 735 | 3 | protected function mediaQuery(&$out) |
|
| 778 | |||
| 779 | /** |
||
| 780 | * @param $out |
||
| 781 | * |
||
| 782 | * @return bool |
||
| 783 | */ |
||
| 784 | 1 | protected function mediaExpression(&$out) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * an unbounded string stopped by $end |
||
| 812 | * |
||
| 813 | * @param $end |
||
| 814 | * @param $out |
||
| 815 | * @param null $nestingOpen |
||
| 816 | * @param null $rejectStrs |
||
| 817 | * |
||
| 818 | * @return bool |
||
| 819 | */ |
||
| 820 | 26 | protected function openString($end, &$out, $nestingOpen = null, $rejectStrs = null) |
|
| 890 | |||
| 891 | /** |
||
| 892 | * @param $out |
||
| 893 | * |
||
| 894 | * @return bool |
||
| 895 | */ |
||
| 896 | 48 | protected function stringValue(&$out) |
|
| 949 | |||
| 950 | /** |
||
| 951 | * @param $out |
||
| 952 | * |
||
| 953 | * @return bool |
||
| 954 | */ |
||
| 955 | 19 | protected function interpolation(&$out) |
|
| 979 | |||
| 980 | /** |
||
| 981 | * @param $unit |
||
| 982 | * |
||
| 983 | * @return bool |
||
| 984 | */ |
||
| 985 | 49 | protected function unit(&$unit) |
|
| 1003 | |||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * a # color |
||
| 1007 | * |
||
| 1008 | * @param $out |
||
| 1009 | * |
||
| 1010 | * @return bool |
||
| 1011 | */ |
||
| 1012 | 48 | protected function color(&$out) |
|
| 1026 | |||
| 1027 | /** |
||
| 1028 | * consume an argument definition list surrounded by () |
||
| 1029 | * each argument is a variable name with optional value |
||
| 1030 | * or at the end a ... or a variable named followed by ... |
||
| 1031 | * arguments are separated by , unless a ; is in the list, then ; is the |
||
| 1032 | * delimiter. |
||
| 1033 | * |
||
| 1034 | * @param $args |
||
| 1035 | * @param $isVararg |
||
| 1036 | * |
||
| 1037 | * @return bool |
||
| 1038 | * @throws \LesserPhp\Exception\GeneralException |
||
| 1039 | */ |
||
| 1040 | 45 | protected function argumentDef(&$args, &$isVararg) |
|
| 1144 | |||
| 1145 | /** |
||
| 1146 | * consume a list of tags |
||
| 1147 | * this accepts a hanging delimiter |
||
| 1148 | * |
||
| 1149 | * @param array $tags |
||
| 1150 | * @param bool $simple |
||
| 1151 | * @param string $delim |
||
| 1152 | * |
||
| 1153 | * @return bool |
||
| 1154 | */ |
||
| 1155 | 49 | View Code Duplication | protected function tags(&$tags, $simple = false, $delim = ',') |
| 1167 | |||
| 1168 | /** |
||
| 1169 | * list of tags of specifying mixin path |
||
| 1170 | * optionally separated by > (lazy, accepts extra >) |
||
| 1171 | * |
||
| 1172 | * @param array $tags |
||
| 1173 | * |
||
| 1174 | * @return bool |
||
| 1175 | */ |
||
| 1176 | 49 | View Code Duplication | protected function mixinTags(&$tags) |
| 1186 | |||
| 1187 | /** |
||
| 1188 | * a bracketed value (contained within in a tag definition) |
||
| 1189 | * |
||
| 1190 | * @param array $parts |
||
| 1191 | * @param bool $hasExpression |
||
| 1192 | * |
||
| 1193 | * @return bool |
||
| 1194 | */ |
||
| 1195 | 49 | protected function tagBracket(&$parts, &$hasExpression) |
|
| 1266 | |||
| 1267 | /** |
||
| 1268 | * a space separated list of selectors |
||
| 1269 | * |
||
| 1270 | * @param $tag |
||
| 1271 | * @param bool $simple |
||
| 1272 | * |
||
| 1273 | * @return bool |
||
| 1274 | */ |
||
| 1275 | 49 | protected function tag(&$tag, $simple = false) |
|
| 1347 | |||
| 1348 | /** |
||
| 1349 | * a css function |
||
| 1350 | * |
||
| 1351 | * @param array $func |
||
| 1352 | * |
||
| 1353 | * @return bool |
||
| 1354 | */ |
||
| 1355 | 48 | protected function func(&$func) |
|
| 1402 | |||
| 1403 | /** |
||
| 1404 | * consume a less variable |
||
| 1405 | * |
||
| 1406 | * @param $name |
||
| 1407 | * |
||
| 1408 | * @return bool |
||
| 1409 | */ |
||
| 1410 | 49 | protected function variable(&$name) |
|
| 1430 | |||
| 1431 | /** |
||
| 1432 | * Consume an assignment operator |
||
| 1433 | * Can optionally take a name that will be set to the current property name |
||
| 1434 | * |
||
| 1435 | * @param string $name |
||
| 1436 | * |
||
| 1437 | * @return bool |
||
| 1438 | */ |
||
| 1439 | 48 | protected function assign($name = null) |
|
| 1447 | |||
| 1448 | /** |
||
| 1449 | * consume a keyword |
||
| 1450 | * |
||
| 1451 | * @param $word |
||
| 1452 | * |
||
| 1453 | * @return bool |
||
| 1454 | */ |
||
| 1455 | 49 | protected function keyword(&$word) |
|
| 1465 | |||
| 1466 | /** |
||
| 1467 | * consume an end of statement delimiter |
||
| 1468 | * |
||
| 1469 | * @return bool |
||
| 1470 | */ |
||
| 1471 | 48 | protected function end() |
|
| 1482 | |||
| 1483 | /** |
||
| 1484 | * @param $guards |
||
| 1485 | * |
||
| 1486 | * @return bool |
||
| 1487 | */ |
||
| 1488 | 19 | protected function guards(&$guards) |
|
| 1516 | |||
| 1517 | /** |
||
| 1518 | * a bunch of guards that are and'd together |
||
| 1519 | * |
||
| 1520 | * @param $guardGroup |
||
| 1521 | * |
||
| 1522 | * @return bool |
||
| 1523 | */ |
||
| 1524 | 5 | protected function guardGroup(&$guardGroup) |
|
| 1544 | |||
| 1545 | /** |
||
| 1546 | * @param $guard |
||
| 1547 | * |
||
| 1548 | * @return bool |
||
| 1549 | */ |
||
| 1550 | 5 | protected function guard(&$guard) |
|
| 1568 | |||
| 1569 | /* raw parsing functions */ |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * @param string $what |
||
| 1573 | * @param bool $eatWhitespace |
||
| 1574 | * |
||
| 1575 | * @return bool |
||
| 1576 | */ |
||
| 1577 | 49 | protected function literal($what, $eatWhitespace = null) |
|
| 1603 | |||
| 1604 | /** |
||
| 1605 | * @param $out |
||
| 1606 | * @param string $parseItem |
||
| 1607 | * @param string $delim |
||
| 1608 | * @param bool $flatten |
||
| 1609 | * |
||
| 1610 | * @return bool |
||
| 1611 | */ |
||
| 1612 | 3 | protected function genericList(&$out, $parseItem, $delim = "", $flatten = true) |
|
| 1640 | |||
| 1641 | /** |
||
| 1642 | * advance counter to next occurrence of $what |
||
| 1643 | * $until - don't include $what in advance |
||
| 1644 | * $allowNewline, if string, will be used as valid char set |
||
| 1645 | * |
||
| 1646 | * @param $what |
||
| 1647 | * @param $out |
||
| 1648 | * @param bool $until |
||
| 1649 | * @param bool $allowNewline |
||
| 1650 | * |
||
| 1651 | * @return bool |
||
| 1652 | */ |
||
| 1653 | protected function to($what, &$out, $until = false, $allowNewline = false) |
||
| 1671 | |||
| 1672 | /** |
||
| 1673 | * try to match something on head of buffer |
||
| 1674 | * |
||
| 1675 | * @param string $regex |
||
| 1676 | * @param $out |
||
| 1677 | * @param bool $eatWhitespace |
||
| 1678 | * |
||
| 1679 | * @return bool |
||
| 1680 | */ |
||
| 1681 | 49 | protected function match($regex, &$out, $eatWhitespace = null) |
|
| 1699 | |||
| 1700 | /** |
||
| 1701 | * match some whitespace |
||
| 1702 | * |
||
| 1703 | * @return bool |
||
| 1704 | */ |
||
| 1705 | 49 | protected function whitespace() |
|
| 1724 | |||
| 1725 | /** |
||
| 1726 | * match something without consuming it |
||
| 1727 | * |
||
| 1728 | * @param string $regex |
||
| 1729 | * @param array $out |
||
| 1730 | * @param int $from |
||
| 1731 | * |
||
| 1732 | * @return int |
||
| 1733 | */ |
||
| 1734 | 24 | protected function peek($regex, &$out = null, $from = null) |
|
| 1743 | |||
| 1744 | /** |
||
| 1745 | * seek to a spot in the buffer or return where we are on no argument |
||
| 1746 | * |
||
| 1747 | * @param int $where |
||
| 1748 | * |
||
| 1749 | * @return bool|int |
||
| 1750 | */ |
||
| 1751 | 49 | protected function seek($where = null) |
|
| 1761 | |||
| 1762 | /* misc functions */ |
||
| 1763 | |||
| 1764 | /** |
||
| 1765 | * @param string $msg |
||
| 1766 | * @param int $count |
||
| 1767 | * |
||
| 1768 | * @throws \LesserPhp\Exception\GeneralException |
||
| 1769 | */ |
||
| 1770 | 5 | public function throwError($msg = 'parse error', $count = null) |
|
| 1789 | |||
| 1790 | /** |
||
| 1791 | * @param null $selectors |
||
| 1792 | * @param null $type |
||
| 1793 | * |
||
| 1794 | * @return \stdClass |
||
| 1795 | */ |
||
| 1796 | 49 | protected function pushBlock($selectors = null, $type = null) |
|
| 1822 | |||
| 1823 | /** |
||
| 1824 | * push a block that doesn't multiply tags |
||
| 1825 | * |
||
| 1826 | * @param $type |
||
| 1827 | * |
||
| 1828 | * @return \stdClass |
||
| 1829 | */ |
||
| 1830 | 49 | protected function pushSpecialBlock($type) |
|
| 1834 | |||
| 1835 | /** |
||
| 1836 | * append a property to the current block |
||
| 1837 | * |
||
| 1838 | * @param $prop |
||
| 1839 | * @param $pos |
||
| 1840 | */ |
||
| 1841 | 49 | protected function append($prop, $pos = null) |
|
| 1848 | |||
| 1849 | /** |
||
| 1850 | * pop something off the stack |
||
| 1851 | * |
||
| 1852 | * @return mixed |
||
| 1853 | */ |
||
| 1854 | 46 | protected function pop() |
|
| 1861 | |||
| 1862 | /** |
||
| 1863 | * remove comments from $text |
||
| 1864 | * todo: make it work for all functions, not just url |
||
| 1865 | * |
||
| 1866 | * @param string $text |
||
| 1867 | * |
||
| 1868 | * @return string |
||
| 1869 | */ |
||
| 1870 | 49 | protected function removeComments($text) |
|
| 1940 | |||
| 1941 | /** |
||
| 1942 | * @param bool $writeComments |
||
| 1943 | */ |
||
| 1944 | 49 | public function setWriteComments($writeComments) |
|
| 1948 | |||
| 1949 | /** |
||
| 1950 | * @param $s |
||
| 1951 | * |
||
| 1952 | * @return bool |
||
| 1953 | */ |
||
| 1954 | 3 | protected function handleLiteralMedia($s) |
|
| 1968 | |||
| 1969 | /** |
||
| 1970 | * @param string $directiveName |
||
| 1971 | * |
||
| 1972 | * @return bool |
||
| 1973 | */ |
||
| 1974 | 4 | View Code Duplication | protected function handleDirectiveBlock($directiveName) |
| 1989 | |||
| 1990 | /** |
||
| 1991 | * @param string $directiveName |
||
| 1992 | * |
||
| 1993 | * @return bool |
||
| 1994 | */ |
||
| 1995 | 1 | protected function handleDirectiveLine($directiveName) |
|
| 2005 | |||
| 2006 | /** |
||
| 2007 | * @param string $directiveName |
||
| 2008 | * |
||
| 2009 | * @return bool |
||
| 2010 | */ |
||
| 2011 | 24 | View Code Duplication | protected function handleRulesetDefinition($directiveName) |
| 2027 | } |
||
| 2028 |
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: