1 <?php declare(strict_types=1);
6 * This parser is based on a skeleton written by Moriyoshi Koizumi, which in
7 * turn is based on work by Masato Bito.
9 use PhpParser\Node\Expr;
10 use PhpParser\Node\Name;
11 use PhpParser\Node\Param;
12 use PhpParser\Node\Scalar\LNumber;
13 use PhpParser\Node\Scalar\String_;
14 use PhpParser\Node\Stmt\Class_;
15 use PhpParser\Node\Stmt\ClassConst;
16 use PhpParser\Node\Stmt\ClassMethod;
17 use PhpParser\Node\Stmt\Interface_;
18 use PhpParser\Node\Stmt\Namespace_;
19 use PhpParser\Node\Stmt\Property;
20 use PhpParser\Node\Stmt\TryCatch;
21 use PhpParser\Node\Stmt\UseUse;
22 use PhpParser\Node\VarLikeIdentifier;
24 abstract class ParserAbstract implements Parser
26 const SYMBOL_NONE = -1;
29 * The following members will be filled with generated parsing data:
32 /** @var int Size of $tokenToSymbol map */
33 protected $tokenToSymbolMapSize;
34 /** @var int Size of $action table */
35 protected $actionTableSize;
36 /** @var int Size of $goto table */
37 protected $gotoTableSize;
39 /** @var int Symbol number signifying an invalid token */
40 protected $invalidSymbol;
41 /** @var int Symbol number of error recovery token */
42 protected $errorSymbol;
43 /** @var int Action number signifying default action */
44 protected $defaultAction;
45 /** @var int Rule number signifying that an unexpected token was encountered */
46 protected $unexpectedTokenRule;
48 protected $YY2TBLSTATE;
49 /** @var int Number of non-leaf states */
50 protected $numNonLeafStates;
52 /** @var int[] Map of lexer tokens to internal symbols */
53 protected $tokenToSymbol;
54 /** @var string[] Map of symbols to their names */
55 protected $symbolToName;
56 /** @var array Names of the production rules (only necessary for debugging) */
57 protected $productions;
59 /** @var int[] Map of states to a displacement into the $action table. The corresponding action for this
60 * state/symbol pair is $action[$actionBase[$state] + $symbol]. If $actionBase[$state] is 0, the
61 action is defaulted, i.e. $actionDefault[$state] should be used instead. */
62 protected $actionBase;
63 /** @var int[] Table of actions. Indexed according to $actionBase comment. */
65 /** @var int[] Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol
66 * then the action is defaulted, i.e. $actionDefault[$state] should be used instead. */
67 protected $actionCheck;
68 /** @var int[] Map of states to their default action */
69 protected $actionDefault;
70 /** @var callable[] Semantic action callbacks */
71 protected $reduceCallbacks;
73 /** @var int[] Map of non-terminals to a displacement into the $goto table. The corresponding goto state for this
74 * non-terminal/state pair is $goto[$gotoBase[$nonTerminal] + $state] (unless defaulted) */
76 /** @var int[] Table of states to goto after reduction. Indexed according to $gotoBase comment. */
78 /** @var int[] Table indexed analogously to $goto. If $gotoCheck[$gotoBase[$nonTerminal] + $state] != $nonTerminal
79 * then the goto state is defaulted, i.e. $gotoDefault[$nonTerminal] should be used. */
81 /** @var int[] Map of non-terminals to the default state to goto after their reduction */
82 protected $gotoDefault;
84 /** @var int[] Map of rules to the non-terminal on their left-hand side, i.e. the non-terminal to use for
85 * determining the state to goto after reduction. */
86 protected $ruleToNonTerminal;
87 /** @var int[] Map of rules to the length of their right-hand side, which is the number of elements that have to
88 * be popped from the stack(s) on reduction. */
89 protected $ruleToLength;
92 * The following members are part of the parser state:
95 /** @var Lexer Lexer that is used when parsing */
97 /** @var mixed Temporary value containing the result of last semantic action (reduction) */
99 /** @var array Semantic value stack (contains values of tokens and semantic action results) */
101 /** @var array[] Start attribute stack */
102 protected $startAttributeStack;
103 /** @var array[] End attribute stack */
104 protected $endAttributeStack;
105 /** @var array End attributes of last *shifted* token */
106 protected $endAttributes;
107 /** @var array Start attributes of last *read* token */
108 protected $lookaheadStartAttributes;
110 /** @var ErrorHandler Error handler */
111 protected $errorHandler;
112 /** @var Error[] Errors collected during last parse */
114 /** @var int Error state, used to avoid error floods */
115 protected $errorState;
118 * Initialize $reduceCallbacks map.
120 abstract protected function initReduceCallbacks();
123 * Creates a parser instance.
125 * Options: Currently none.
127 * @param Lexer $lexer A lexer
128 * @param array $options Options array.
130 public function __construct(Lexer $lexer, array $options = []) {
131 $this->lexer = $lexer;
134 if (isset($options['throwOnError'])) {
135 throw new \LogicException(
136 '"throwOnError" is no longer supported, use "errorHandler" instead');
139 $this->initReduceCallbacks();
143 * Parses PHP code into a node tree.
145 * If a non-throwing error handler is used, the parser will continue parsing after an error
146 * occurred and attempt to build a partial AST.
148 * @param string $code The source code to parse
149 * @param ErrorHandler|null $errorHandler Error handler to use for lexer/parser errors, defaults
150 * to ErrorHandler\Throwing.
152 * @return Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and
153 * the parser was unable to recover from an error).
155 public function parse(string $code, ErrorHandler $errorHandler = null) {
156 $this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing;
158 $this->lexer->startLexing($code, $this->errorHandler);
159 $result = $this->doParse();
161 // Clear out some of the interior state, so we don't hold onto unnecessary
162 // memory between uses of the parser
163 $this->startAttributeStack = [];
164 $this->endAttributeStack = [];
165 $this->semStack = [];
166 $this->semValue = null;
171 protected function doParse() {
172 // We start off with no lookahead-token
173 $symbol = self::SYMBOL_NONE;
175 // The attributes for a node are taken from the first and last token of the node.
176 // From the first token only the startAttributes are taken and from the last only
177 // the endAttributes. Both are merged using the array union operator (+).
178 $startAttributes = [];
180 $this->endAttributes = $endAttributes;
182 // Keep stack of start and end attributes
183 $this->startAttributeStack = [];
184 $this->endAttributeStack = [$endAttributes];
186 // Start off in the initial state and keep a stack of previous states
188 $stateStack = [$state];
190 // Semantic value stack (contains values of tokens and semantic action results)
191 $this->semStack = [];
193 // Current position in the stack(s)
196 $this->errorState = 0;
199 //$this->traceNewState($state, $symbol);
201 if ($this->actionBase[$state] === 0) {
202 $rule = $this->actionDefault[$state];
204 if ($symbol === self::SYMBOL_NONE) {
205 // Fetch the next token id from the lexer and fetch additional info by-ref.
206 // The end attributes are fetched into a temporary variable and only set once the token is really
207 // shifted (not during read). Otherwise you would sometimes get off-by-one errors, when a rule is
208 // reduced after a token was read but not yet shifted.
209 $tokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $endAttributes);
211 // map the lexer token id to the internally used symbols
212 $symbol = $tokenId >= 0 && $tokenId < $this->tokenToSymbolMapSize
213 ? $this->tokenToSymbol[$tokenId]
214 : $this->invalidSymbol;
216 if ($symbol === $this->invalidSymbol) {
217 throw new \RangeException(sprintf(
218 'The lexer returned an invalid token (id=%d, value=%s)',
219 $tokenId, $tokenValue
223 // This is necessary to assign some meaningful attributes to /* empty */ productions. They'll get
224 // the attributes of the next token, even though they don't contain it themselves.
225 $this->startAttributeStack[$stackPos+1] = $startAttributes;
226 $this->endAttributeStack[$stackPos+1] = $endAttributes;
227 $this->lookaheadStartAttributes = $startAttributes;
229 //$this->traceRead($symbol);
232 $idx = $this->actionBase[$state] + $symbol;
233 if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol)
234 || ($state < $this->YY2TBLSTATE
235 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0
236 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol))
237 && ($action = $this->action[$idx]) !== $this->defaultAction) {
239 * >= numNonLeafStates: shift and reduce
243 * = -YYUNEXPECTED: error
247 //$this->traceShift($symbol);
250 $stateStack[$stackPos] = $state = $action;
251 $this->semStack[$stackPos] = $tokenValue;
252 $this->startAttributeStack[$stackPos] = $startAttributes;
253 $this->endAttributeStack[$stackPos] = $endAttributes;
254 $this->endAttributes = $endAttributes;
255 $symbol = self::SYMBOL_NONE;
257 if ($this->errorState) {
261 if ($action < $this->numNonLeafStates) {
265 /* $yyn >= numNonLeafStates means shift-and-reduce */
266 $rule = $action - $this->numNonLeafStates;
271 $rule = $this->actionDefault[$state];
278 //$this->traceAccept();
279 return $this->semValue;
280 } elseif ($rule !== $this->unexpectedTokenRule) {
282 //$this->traceReduce($rule);
285 $this->reduceCallbacks[$rule]($stackPos);
287 if (-1 === $e->getStartLine() && isset($startAttributes['startLine'])) {
288 $e->setStartLine($startAttributes['startLine']);
291 $this->emitError($e);
292 // Can't recover from this type of error
296 /* Goto - shift nonterminal */
297 $lastEndAttributes = $this->endAttributeStack[$stackPos];
298 $stackPos -= $this->ruleToLength[$rule];
299 $nonTerminal = $this->ruleToNonTerminal[$rule];
300 $idx = $this->gotoBase[$nonTerminal] + $stateStack[$stackPos];
301 if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] === $nonTerminal) {
302 $state = $this->goto[$idx];
304 $state = $this->gotoDefault[$nonTerminal];
308 $stateStack[$stackPos] = $state;
309 $this->semStack[$stackPos] = $this->semValue;
310 $this->endAttributeStack[$stackPos] = $lastEndAttributes;
313 switch ($this->errorState) {
315 $msg = $this->getErrorMessage($symbol, $state);
316 $this->emitError(new Error($msg, $startAttributes + $endAttributes));
317 // Break missing intentionally
320 $this->errorState = 3;
322 // Pop until error-expecting state uncovered
324 (($idx = $this->actionBase[$state] + $this->errorSymbol) >= 0
325 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol)
326 || ($state < $this->YY2TBLSTATE
327 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $this->errorSymbol) >= 0
328 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol)
329 ) || ($action = $this->action[$idx]) === $this->defaultAction) { // Not totally sure about this
330 if ($stackPos <= 0) {
331 // Could not recover from error
334 $state = $stateStack[--$stackPos];
335 //$this->tracePop($state);
338 //$this->traceShift($this->errorSymbol);
340 $stateStack[$stackPos] = $state = $action;
342 // We treat the error symbol as being empty, so we reset the end attributes
343 // to the end attributes of the last non-error symbol
344 $this->endAttributeStack[$stackPos] = $this->endAttributeStack[$stackPos - 1];
345 $this->endAttributes = $this->endAttributeStack[$stackPos - 1];
350 // Reached EOF without recovering from error
354 //$this->traceDiscard($symbol);
355 $symbol = self::SYMBOL_NONE;
360 if ($state < $this->numNonLeafStates) {
364 /* >= numNonLeafStates means shift-and-reduce */
365 $rule = $state - $this->numNonLeafStates;
369 throw new \RuntimeException('Reached end of parser loop');
372 protected function emitError(Error $error) {
373 $this->errorHandler->handleError($error);
377 * Format error message including expected tokens.
379 * @param int $symbol Unexpected symbol
380 * @param int $state State at time of error
382 * @return string Formatted error message
384 protected function getErrorMessage(int $symbol, int $state) : string {
385 $expectedString = '';
386 if ($expected = $this->getExpectedTokens($state)) {
387 $expectedString = ', expecting ' . implode(' or ', $expected);
390 return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString;
394 * Get limited number of expected tokens in given state.
396 * @param int $state State
398 * @return string[] Expected tokens. If too many, an empty array is returned.
400 protected function getExpectedTokens(int $state) : array {
403 $base = $this->actionBase[$state];
404 foreach ($this->symbolToName as $symbol => $name) {
405 $idx = $base + $symbol;
406 if ($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
407 || $state < $this->YY2TBLSTATE
408 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0
409 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
411 if ($this->action[$idx] !== $this->unexpectedTokenRule
412 && $this->action[$idx] !== $this->defaultAction
413 && $symbol !== $this->errorSymbol
415 if (count($expected) === 4) {
416 /* Too many expected tokens */
429 * Tracing functions used for debugging the parser.
433 protected function traceNewState($state, $symbol) {
434 echo '% State ' . $state
435 . ', Lookahead ' . ($symbol == self::SYMBOL_NONE ? '--none--' : $this->symbolToName[$symbol]) . "\n";
438 protected function traceRead($symbol) {
439 echo '% Reading ' . $this->symbolToName[$symbol] . "\n";
442 protected function traceShift($symbol) {
443 echo '% Shift ' . $this->symbolToName[$symbol] . "\n";
446 protected function traceAccept() {
447 echo "% Accepted.\n";
450 protected function traceReduce($n) {
451 echo '% Reduce by (' . $n . ') ' . $this->productions[$n] . "\n";
454 protected function tracePop($state) {
455 echo '% Recovering, uncovered state ' . $state . "\n";
458 protected function traceDiscard($symbol) {
459 echo '% Discard ' . $this->symbolToName[$symbol] . "\n";
464 * Helper functions invoked by semantic actions
468 * Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.
470 * @param Node\Stmt[] $stmts
471 * @return Node\Stmt[]
473 protected function handleNamespaces(array $stmts) : array {
475 $style = $this->getNamespacingStyle($stmts);
476 if (null === $style) {
477 // not namespaced, nothing to do
479 } elseif ('brace' === $style) {
480 // For braced namespaces we only have to check that there are no invalid statements between the namespaces
481 $afterFirstNamespace = false;
482 foreach ($stmts as $stmt) {
483 if ($stmt instanceof Node\Stmt\Namespace_) {
484 $afterFirstNamespace = true;
485 } elseif (!$stmt instanceof Node\Stmt\HaltCompiler
486 && !$stmt instanceof Node\Stmt\Nop
487 && $afterFirstNamespace && !$hasErrored) {
488 $this->emitError(new Error(
489 'No code may exist outside of namespace {}', $stmt->getAttributes()));
490 $hasErrored = true; // Avoid one error for every statement
495 // For semicolon namespaces we have to move the statements after a namespace declaration into ->stmts
497 $targetStmts =& $resultStmts;
499 foreach ($stmts as $stmt) {
500 if ($stmt instanceof Node\Stmt\Namespace_) {
501 if ($lastNs !== null) {
502 $this->fixupNamespaceAttributes($lastNs);
504 if ($stmt->stmts === null) {
506 $targetStmts =& $stmt->stmts;
507 $resultStmts[] = $stmt;
509 // This handles the invalid case of mixed style namespaces
510 $resultStmts[] = $stmt;
511 $targetStmts =& $resultStmts;
514 } elseif ($stmt instanceof Node\Stmt\HaltCompiler) {
515 // __halt_compiler() is not moved into the namespace
516 $resultStmts[] = $stmt;
518 $targetStmts[] = $stmt;
521 if ($lastNs !== null) {
522 $this->fixupNamespaceAttributes($lastNs);
528 private function fixupNamespaceAttributes(Node\Stmt\Namespace_ $stmt) {
529 // We moved the statements into the namespace node, as such the end of the namespace node
530 // needs to be extended to the end of the statements.
531 if (empty($stmt->stmts)) {
535 // We only move the builtin end attributes here. This is the best we can do with the
536 // knowledge we have.
537 $endAttributes = ['endLine', 'endFilePos', 'endTokenPos'];
538 $lastStmt = $stmt->stmts[count($stmt->stmts) - 1];
539 foreach ($endAttributes as $endAttribute) {
540 if ($lastStmt->hasAttribute($endAttribute)) {
541 $stmt->setAttribute($endAttribute, $lastStmt->getAttribute($endAttribute));
547 * Determine namespacing style (semicolon or brace)
549 * @param Node[] $stmts Top-level statements.
551 * @return null|string One of "semicolon", "brace" or null (no namespaces)
553 private function getNamespacingStyle(array $stmts) {
555 $hasNotAllowedStmts = false;
556 foreach ($stmts as $i => $stmt) {
557 if ($stmt instanceof Node\Stmt\Namespace_) {
558 $currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace';
559 if (null === $style) {
560 $style = $currentStyle;
561 if ($hasNotAllowedStmts) {
562 $this->emitError(new Error(
563 'Namespace declaration statement has to be the very first statement in the script',
564 $stmt->getLine() // Avoid marking the entire namespace as an error
567 } elseif ($style !== $currentStyle) {
568 $this->emitError(new Error(
569 'Cannot mix bracketed namespace declarations with unbracketed namespace declarations',
570 $stmt->getLine() // Avoid marking the entire namespace as an error
572 // Treat like semicolon style for namespace normalization
578 /* declare(), __halt_compiler() and nops can be used before a namespace declaration */
579 if ($stmt instanceof Node\Stmt\Declare_
580 || $stmt instanceof Node\Stmt\HaltCompiler
581 || $stmt instanceof Node\Stmt\Nop) {
585 /* There may be a hashbang line at the very start of the file */
586 if ($i === 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) {
590 /* Everything else if forbidden before namespace declarations */
591 $hasNotAllowedStmts = true;
597 * Fix up parsing of static property calls in PHP 5.
599 * In PHP 5 A::$b[c][d] and A::$b[c][d]() have very different interpretation. The former is
600 * interpreted as (A::$b)[c][d], while the latter is the same as A::{$b[c][d]}(). We parse the
601 * latter as the former initially and this method fixes the AST into the correct form when we
602 * encounter the "()".
604 * @param Node\Expr\StaticPropertyFetch|Node\Expr\ArrayDimFetch $prop
605 * @param Node\Arg[] $args
606 * @param array $attributes
608 * @return Expr\StaticCall
610 protected function fixupPhp5StaticPropCall($prop, array $args, array $attributes) : Expr\StaticCall {
611 if ($prop instanceof Node\Expr\StaticPropertyFetch) {
612 $name = $prop->name instanceof VarLikeIdentifier
613 ? $prop->name->toString() : $prop->name;
614 $var = new Expr\Variable($name, $prop->name->getAttributes());
615 return new Expr\StaticCall($prop->class, $var, $args, $attributes);
616 } elseif ($prop instanceof Node\Expr\ArrayDimFetch) {
618 while ($tmp->var instanceof Node\Expr\ArrayDimFetch) {
622 /** @var Expr\StaticPropertyFetch $staticProp */
623 $staticProp = $tmp->var;
625 // Set start attributes to attributes of innermost node
627 $this->fixupStartAttributes($tmp, $staticProp->name);
628 while ($tmp->var instanceof Node\Expr\ArrayDimFetch) {
630 $this->fixupStartAttributes($tmp, $staticProp->name);
633 $name = $staticProp->name instanceof VarLikeIdentifier
634 ? $staticProp->name->toString() : $staticProp->name;
635 $tmp->var = new Expr\Variable($name, $staticProp->name->getAttributes());
636 return new Expr\StaticCall($staticProp->class, $prop, $args, $attributes);
638 throw new \Exception;
642 protected function fixupStartAttributes(Node $to, Node $from) {
643 $startAttributes = ['startLine', 'startFilePos', 'startTokenPos'];
644 foreach ($startAttributes as $startAttribute) {
645 if ($from->hasAttribute($startAttribute)) {
646 $to->setAttribute($startAttribute, $from->getAttribute($startAttribute));
651 protected function handleBuiltinTypes(Name $name) {
662 if (!$name->isUnqualified()) {
666 $lowerName = $name->toLowerString();
667 if (!isset($scalarTypes[$lowerName])) {
671 return new Node\Identifier($lowerName, $name->getAttributes());
675 * Get combined start and end attributes at a stack location
677 * @param int $pos Stack location
679 * @return array Combined start and end attributes
681 protected function getAttributesAt(int $pos) : array {
682 return $this->startAttributeStack[$pos] + $this->endAttributeStack[$pos];
685 protected function parseLNumber($str, $attributes, $allowInvalidOctal = false) {
687 return LNumber::fromString($str, $attributes, $allowInvalidOctal);
688 } catch (Error $error) {
689 $this->emitError($error);
691 return new LNumber(0, $attributes);
696 * Parse a T_NUM_STRING token into either an integer or string node.
698 * @param string $str Number string
699 * @param array $attributes Attributes
701 * @return LNumber|String_ Integer or string node.
703 protected function parseNumString(string $str, array $attributes) {
704 if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) {
705 return new String_($str, $attributes);
710 return new String_($str, $attributes);
713 return new LNumber($num, $attributes);
716 protected function checkModifier($a, $b, $modifierPos) {
717 // Jumping through some hoops here because verifyModifier() is also used elsewhere
719 Class_::verifyModifier($a, $b);
720 } catch (Error $error) {
721 $error->setAttributes($this->getAttributesAt($modifierPos));
722 $this->emitError($error);
726 protected function checkParam(Param $node) {
727 if ($node->variadic && null !== $node->default) {
728 $this->emitError(new Error(
729 'Variadic parameter cannot have a default value',
730 $node->default->getAttributes()
735 protected function checkTryCatch(TryCatch $node) {
736 if (empty($node->catches) && null === $node->finally) {
737 $this->emitError(new Error(
738 'Cannot use try without catch or finally', $node->getAttributes()
743 protected function checkNamespace(Namespace_ $node) {
744 if ($node->name && $node->name->isSpecialClassName()) {
745 $this->emitError(new Error(
746 sprintf('Cannot use \'%s\' as namespace name', $node->name),
747 $node->name->getAttributes()
751 if (null !== $node->stmts) {
752 foreach ($node->stmts as $stmt) {
753 if ($stmt instanceof Namespace_) {
754 $this->emitError(new Error(
755 'Namespace declarations cannot be nested', $stmt->getAttributes()
762 protected function checkClass(Class_ $node, $namePos) {
763 if (null !== $node->name && $node->name->isSpecialClassName()) {
764 $this->emitError(new Error(
765 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name),
766 $this->getAttributesAt($namePos)
770 if ($node->extends && $node->extends->isSpecialClassName()) {
771 $this->emitError(new Error(
772 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->extends),
773 $node->extends->getAttributes()
777 foreach ($node->implements as $interface) {
778 if ($interface->isSpecialClassName()) {
779 $this->emitError(new Error(
780 sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface),
781 $interface->getAttributes()
787 protected function checkInterface(Interface_ $node, $namePos) {
788 if (null !== $node->name && $node->name->isSpecialClassName()) {
789 $this->emitError(new Error(
790 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name),
791 $this->getAttributesAt($namePos)
795 foreach ($node->extends as $interface) {
796 if ($interface->isSpecialClassName()) {
797 $this->emitError(new Error(
798 sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface),
799 $interface->getAttributes()
805 protected function checkClassMethod(ClassMethod $node, $modifierPos) {
806 if ($node->flags & Class_::MODIFIER_STATIC) {
807 switch ($node->name->toLowerString()) {
809 $this->emitError(new Error(
810 sprintf('Constructor %s() cannot be static', $node->name),
811 $this->getAttributesAt($modifierPos)));
814 $this->emitError(new Error(
815 sprintf('Destructor %s() cannot be static', $node->name),
816 $this->getAttributesAt($modifierPos)));
819 $this->emitError(new Error(
820 sprintf('Clone method %s() cannot be static', $node->name),
821 $this->getAttributesAt($modifierPos)));
827 protected function checkClassConst(ClassConst $node, $modifierPos) {
828 if ($node->flags & Class_::MODIFIER_STATIC) {
829 $this->emitError(new Error(
830 "Cannot use 'static' as constant modifier",
831 $this->getAttributesAt($modifierPos)));
833 if ($node->flags & Class_::MODIFIER_ABSTRACT) {
834 $this->emitError(new Error(
835 "Cannot use 'abstract' as constant modifier",
836 $this->getAttributesAt($modifierPos)));
838 if ($node->flags & Class_::MODIFIER_FINAL) {
839 $this->emitError(new Error(
840 "Cannot use 'final' as constant modifier",
841 $this->getAttributesAt($modifierPos)));
845 protected function checkProperty(Property $node, $modifierPos) {
846 if ($node->flags & Class_::MODIFIER_ABSTRACT) {
847 $this->emitError(new Error('Properties cannot be declared abstract',
848 $this->getAttributesAt($modifierPos)));
851 if ($node->flags & Class_::MODIFIER_FINAL) {
852 $this->emitError(new Error('Properties cannot be declared final',
853 $this->getAttributesAt($modifierPos)));
857 protected function checkUseUse(UseUse $node, $namePos) {
858 if ($node->alias && $node->alias->isSpecialClassName()) {
859 $this->emitError(new Error(
861 'Cannot use %s as %s because \'%2$s\' is a special class name',
862 $node->name, $node->alias
864 $this->getAttributesAt($namePos)