Skip to content

Commit d20a197

Browse files
ondrejmirtesnikic
authored andcommittedDec 27, 2024
Emit error - Multiple properties cannot share the same hooks
Closes GH-1052.
1 parent 62dee28 commit d20a197

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
 

‎grammar/php.y

+1
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ class_statement:
840840
#if PHP8
841841
| optional_attributes variable_modifiers optional_type_without_static property_declaration_list '{' property_hook_list '}'
842842
{ $$ = new Stmt\Property($2, $4, attributes(), $3, $1, $6);
843+
$this->checkPropertyHooksForMultiProperty($$, #5);
843844
$this->checkEmptyPropertyHookList($6, #5); }
844845
#endif
845846
| optional_attributes method_modifiers T_CONST class_const_list semi

‎lib/PhpParser/Parser/Php8.php

+1
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ protected function initReduceCallbacks(): void {
19931993
},
19941994
348 => static function ($self, $stackPos) {
19951995
$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)]);
1996+
$self->checkPropertyHooksForMultiProperty($self->semValue, $stackPos-(7-5));
19961997
$self->checkEmptyPropertyHookList($self->semStack[$stackPos-(7-6)], $stackPos-(7-5));
19971998
},
19981999
349 => static function ($self, $stackPos) {

‎lib/PhpParser/ParserAbstract.php

+7
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,13 @@ protected function checkUseUse(UseItem $node, int $namePos): void {
11581158
}
11591159
}
11601160

1161+
protected function checkPropertyHooksForMultiProperty(Property $property, int $hookPos): void {
1162+
if (count($property->props) > 1) {
1163+
$this->emitError(new Error(
1164+
'Cannot use hooks when declaring multiple properties', $this->getAttributesAt($hookPos)));
1165+
}
1166+
}
1167+
11611168
/** @param PropertyHook[] $hooks */
11621169
protected function checkEmptyPropertyHookList(array $hooks, int $hookPos): void {
11631170
if (empty($hooks)) {

‎test/code/parser/stmt/class/property_hooks.test

+113
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,116 @@ array(
523523
)
524524
)
525525
)
526+
-----
527+
<?php
528+
class Test
529+
{
530+
531+
public $foo, $bar { get { return 42; } }
532+
533+
}
534+
-----
535+
Cannot use hooks when declaring multiple properties from 5:23 to 5:23
536+
array(
537+
0: Stmt_Class(
538+
attrGroups: array(
539+
)
540+
flags: 0
541+
name: Identifier(
542+
name: Test
543+
)
544+
extends: null
545+
implements: array(
546+
)
547+
stmts: array(
548+
0: Stmt_Property(
549+
attrGroups: array(
550+
)
551+
flags: PUBLIC (1)
552+
type: null
553+
props: array(
554+
0: PropertyItem(
555+
name: VarLikeIdentifier(
556+
name: foo
557+
)
558+
default: null
559+
)
560+
1: PropertyItem(
561+
name: VarLikeIdentifier(
562+
name: bar
563+
)
564+
default: null
565+
)
566+
)
567+
hooks: array(
568+
0: PropertyHook(
569+
attrGroups: array(
570+
)
571+
flags: 0
572+
byRef: false
573+
name: Identifier(
574+
name: get
575+
)
576+
params: array(
577+
)
578+
body: array(
579+
0: Stmt_Return(
580+
expr: Scalar_Int(
581+
value: 42
582+
)
583+
)
584+
)
585+
)
586+
)
587+
)
588+
)
589+
)
590+
)
591+
-----
592+
<?php
593+
class Test
594+
{
595+
596+
public $foo, $bar { }
597+
598+
}
599+
-----
600+
Cannot use hooks when declaring multiple properties from 5:23 to 5:23
601+
Property hook list cannot be empty from 5:23 to 5:23
602+
array(
603+
0: Stmt_Class(
604+
attrGroups: array(
605+
)
606+
flags: 0
607+
name: Identifier(
608+
name: Test
609+
)
610+
extends: null
611+
implements: array(
612+
)
613+
stmts: array(
614+
0: Stmt_Property(
615+
attrGroups: array(
616+
)
617+
flags: PUBLIC (1)
618+
type: null
619+
props: array(
620+
0: PropertyItem(
621+
name: VarLikeIdentifier(
622+
name: foo
623+
)
624+
default: null
625+
)
626+
1: PropertyItem(
627+
name: VarLikeIdentifier(
628+
name: bar
629+
)
630+
default: null
631+
)
632+
)
633+
hooks: array(
634+
)
635+
)
636+
)
637+
)
638+
)

0 commit comments

Comments
 (0)