Skip to content

Commit

Permalink
add checkClassModifier() to separate from validation of class members
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 15, 2022
1 parent da55227 commit ccf3d46
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion grammar/php7.y
Expand Up @@ -387,7 +387,7 @@ class_entry_type:

class_modifiers:
class_modifier { $$ = $1; }
| class_modifiers class_modifier { $this->checkModifier($1, $2, #2); $$ = $1 | $2; }
| class_modifiers class_modifier { $this->checkClassModifier($1, $2, #2); $$ = $1 | $2; }
;

class_modifier:
Expand Down
21 changes: 21 additions & 0 deletions lib/PhpParser/Node/Stmt/Class_.php
Expand Up @@ -81,6 +81,27 @@ public function isAnonymous() : bool {
return null === $this->name;
}

/**
* @internal
*/
public static function verifyClassModifier($a, $b) {
if ($a & self::MODIFIER_ABSTRACT && $b & self::MODIFIER_ABSTRACT) {
throw new Error('Multiple abstract modifiers are not allowed');
}

if ($a & self::MODIFIER_FINAL && $b & self::MODIFIER_FINAL) {
throw new Error('Multiple final modifiers are not allowed');
}

if ($a & self::MODIFIER_READONLY && $b & self::MODIFIER_READONLY) {
throw new Error('Multiple readonly modifiers are not allowed');
}

if ($a & 48 && $b & 48) {
throw new Error('Cannot use the final modifier on an abstract class');
}
}

/**
* @internal
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/PhpParser/ParserAbstract.php
Expand Up @@ -875,6 +875,10 @@ protected function createCommentNopAttributes(array $comments) {
return $attributes;
}

protected function checkClassModifier($a, $b, $modifierPos) {
Class_::verifyClassModifier($a, $b);
}

protected function checkModifier($a, $b, $modifierPos) {
// Jumping through some hoops here because verifyModifier() is also used elsewhere
try {
Expand Down

0 comments on commit ccf3d46

Please sign in to comment.