mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-17 07:08:14 +01:00
Emit error - Multiple properties cannot share the same hooks
Closes GH-1052.
This commit is contained in:
parent
62dee28027
commit
d20a197ca7
@ -840,6 +840,7 @@ class_statement:
|
|||||||
#if PHP8
|
#if PHP8
|
||||||
| optional_attributes variable_modifiers optional_type_without_static property_declaration_list '{' property_hook_list '}'
|
| optional_attributes variable_modifiers optional_type_without_static property_declaration_list '{' property_hook_list '}'
|
||||||
{ $$ = new Stmt\Property($2, $4, attributes(), $3, $1, $6);
|
{ $$ = new Stmt\Property($2, $4, attributes(), $3, $1, $6);
|
||||||
|
$this->checkPropertyHooksForMultiProperty($$, #5);
|
||||||
$this->checkEmptyPropertyHookList($6, #5); }
|
$this->checkEmptyPropertyHookList($6, #5); }
|
||||||
#endif
|
#endif
|
||||||
| optional_attributes method_modifiers T_CONST class_const_list semi
|
| optional_attributes method_modifiers T_CONST class_const_list semi
|
||||||
|
@ -1993,6 +1993,7 @@ class Php8 extends \PhpParser\ParserAbstract
|
|||||||
},
|
},
|
||||||
348 => static function ($self, $stackPos) {
|
348 => static function ($self, $stackPos) {
|
||||||
$self->semValue = new Stmt\Property($self->semStack[$stackPos-(7-2)], $self->semStack[$stackPos-(7-4)], $self->getAttributes($self->tokenStartStack[$stackPos-(7-1)], $self->tokenEndStack[$stackPos]), $self->semStack[$stackPos-(7-3)], $self->semStack[$stackPos-(7-1)], $self->semStack[$stackPos-(7-6)]);
|
$self->semValue = new Stmt\Property($self->semStack[$stackPos-(7-2)], $self->semStack[$stackPos-(7-4)], $self->getAttributes($self->tokenStartStack[$stackPos-(7-1)], $self->tokenEndStack[$stackPos]), $self->semStack[$stackPos-(7-3)], $self->semStack[$stackPos-(7-1)], $self->semStack[$stackPos-(7-6)]);
|
||||||
|
$self->checkPropertyHooksForMultiProperty($self->semValue, $stackPos-(7-5));
|
||||||
$self->checkEmptyPropertyHookList($self->semStack[$stackPos-(7-6)], $stackPos-(7-5));
|
$self->checkEmptyPropertyHookList($self->semStack[$stackPos-(7-6)], $stackPos-(7-5));
|
||||||
},
|
},
|
||||||
349 => static function ($self, $stackPos) {
|
349 => static function ($self, $stackPos) {
|
||||||
|
@ -1158,6 +1158,13 @@ abstract class ParserAbstract implements Parser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function checkPropertyHooksForMultiProperty(Property $property, int $hookPos): void {
|
||||||
|
if (count($property->props) > 1) {
|
||||||
|
$this->emitError(new Error(
|
||||||
|
'Cannot use hooks when declaring multiple properties', $this->getAttributesAt($hookPos)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @param PropertyHook[] $hooks */
|
/** @param PropertyHook[] $hooks */
|
||||||
protected function checkEmptyPropertyHookList(array $hooks, int $hookPos): void {
|
protected function checkEmptyPropertyHookList(array $hooks, int $hookPos): void {
|
||||||
if (empty($hooks)) {
|
if (empty($hooks)) {
|
||||||
|
@ -523,3 +523,116 @@ array(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
class Test
|
||||||
|
{
|
||||||
|
|
||||||
|
public $foo, $bar { get { return 42; } }
|
||||||
|
|
||||||
|
}
|
||||||
|
-----
|
||||||
|
Cannot use hooks when declaring multiple properties from 5:23 to 5:23
|
||||||
|
array(
|
||||||
|
0: Stmt_Class(
|
||||||
|
attrGroups: array(
|
||||||
|
)
|
||||||
|
flags: 0
|
||||||
|
name: Identifier(
|
||||||
|
name: Test
|
||||||
|
)
|
||||||
|
extends: null
|
||||||
|
implements: array(
|
||||||
|
)
|
||||||
|
stmts: array(
|
||||||
|
0: Stmt_Property(
|
||||||
|
attrGroups: array(
|
||||||
|
)
|
||||||
|
flags: PUBLIC (1)
|
||||||
|
type: null
|
||||||
|
props: array(
|
||||||
|
0: PropertyItem(
|
||||||
|
name: VarLikeIdentifier(
|
||||||
|
name: foo
|
||||||
|
)
|
||||||
|
default: null
|
||||||
|
)
|
||||||
|
1: PropertyItem(
|
||||||
|
name: VarLikeIdentifier(
|
||||||
|
name: bar
|
||||||
|
)
|
||||||
|
default: null
|
||||||
|
)
|
||||||
|
)
|
||||||
|
hooks: array(
|
||||||
|
0: PropertyHook(
|
||||||
|
attrGroups: array(
|
||||||
|
)
|
||||||
|
flags: 0
|
||||||
|
byRef: false
|
||||||
|
name: Identifier(
|
||||||
|
name: get
|
||||||
|
)
|
||||||
|
params: array(
|
||||||
|
)
|
||||||
|
body: array(
|
||||||
|
0: Stmt_Return(
|
||||||
|
expr: Scalar_Int(
|
||||||
|
value: 42
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
class Test
|
||||||
|
{
|
||||||
|
|
||||||
|
public $foo, $bar { }
|
||||||
|
|
||||||
|
}
|
||||||
|
-----
|
||||||
|
Cannot use hooks when declaring multiple properties from 5:23 to 5:23
|
||||||
|
Property hook list cannot be empty from 5:23 to 5:23
|
||||||
|
array(
|
||||||
|
0: Stmt_Class(
|
||||||
|
attrGroups: array(
|
||||||
|
)
|
||||||
|
flags: 0
|
||||||
|
name: Identifier(
|
||||||
|
name: Test
|
||||||
|
)
|
||||||
|
extends: null
|
||||||
|
implements: array(
|
||||||
|
)
|
||||||
|
stmts: array(
|
||||||
|
0: Stmt_Property(
|
||||||
|
attrGroups: array(
|
||||||
|
)
|
||||||
|
flags: PUBLIC (1)
|
||||||
|
type: null
|
||||||
|
props: array(
|
||||||
|
0: PropertyItem(
|
||||||
|
name: VarLikeIdentifier(
|
||||||
|
name: foo
|
||||||
|
)
|
||||||
|
default: null
|
||||||
|
)
|
||||||
|
1: PropertyItem(
|
||||||
|
name: VarLikeIdentifier(
|
||||||
|
name: bar
|
||||||
|
)
|
||||||
|
default: null
|
||||||
|
)
|
||||||
|
)
|
||||||
|
hooks: array(
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user