Skip to content

PHP 8.4 | File::getMemberProperties() + File::getMethodParameters(): add support for asymmetric visibility #1116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 58 additions & 28 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,10 @@ public function getDeclarationName($stackPtr)
* 'readonly_token' => integer, // The stack pointer to the readonly modifier token.
* // This index will only be set if the property is readonly.
*
* ... and if the promoted property uses asymmetric visibility, these additional array indexes will also be available:
* 'set_visibility' => string, // The property set-visibility as declared.
* 'set_visibility_token' => integer, // The stack pointer to the set-visibility modifier token.
*
* @param int $stackPtr The position in the stack of the function token
* to acquire the parameters for.
*
Expand Down Expand Up @@ -1406,10 +1410,11 @@ public function getMethodParameters($stackPtr)
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$readonlyToken = null;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$setVisibilityToken = null;
$readonlyToken = null;

for ($i = $paramStart; $i <= $closer; $i++) {
// Check to see if this token has a parenthesis or bracket opener. If it does
Expand Down Expand Up @@ -1541,6 +1546,13 @@ public function getMethodParameters($stackPtr)
$visibilityToken = $i;
}
break;
case T_PUBLIC_SET:
case T_PROTECTED_SET:
case T_PRIVATE_SET:
if ($defaultStart === null) {
$setVisibilityToken = $i;
}
break;
case T_READONLY:
if ($defaultStart === null) {
$readonlyToken = $i;
Expand Down Expand Up @@ -1575,16 +1587,21 @@ public function getMethodParameters($stackPtr)
$vars[$paramCount]['type_hint_end_token'] = $typeHintEndToken;
$vars[$paramCount]['nullable_type'] = $nullableType;

if ($visibilityToken !== null || $readonlyToken !== null) {
if ($visibilityToken !== null || $setVisibilityToken !== null || $readonlyToken !== null) {
$vars[$paramCount]['property_visibility'] = 'public';
$vars[$paramCount]['visibility_token'] = false;
$vars[$paramCount]['property_readonly'] = false;

if ($visibilityToken !== null) {
$vars[$paramCount]['property_visibility'] = $this->tokens[$visibilityToken]['content'];
$vars[$paramCount]['visibility_token'] = $visibilityToken;
}

if ($setVisibilityToken !== null) {
$vars[$paramCount]['set_visibility'] = $this->tokens[$setVisibilityToken]['content'];
$vars[$paramCount]['set_visibility_token'] = $setVisibilityToken;
}

$vars[$paramCount]['property_readonly'] = false;
if ($readonlyToken !== null) {
$vars[$paramCount]['property_readonly'] = true;
$vars[$paramCount]['readonly_token'] = $readonlyToken;
Expand All @@ -1598,21 +1615,22 @@ public function getMethodParameters($stackPtr)
}

// Reset the vars, as we are about to process the next parameter.
$currVar = null;
$paramStart = ($i + 1);
$defaultStart = null;
$equalToken = null;
$hasAttributes = false;
$passByReference = false;
$referenceToken = false;
$variableLength = false;
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$readonlyToken = null;
$currVar = null;
$paramStart = ($i + 1);
$defaultStart = null;
$equalToken = null;
$hasAttributes = false;
$passByReference = false;
$referenceToken = false;
$variableLength = false;
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$setVisibilityToken = null;
$readonlyToken = null;

$paramCount++;
break;
Expand Down Expand Up @@ -1827,6 +1845,9 @@ public function getMethodProperties($stackPtr)
* array(
* 'scope' => string, // Public, private, or protected.
* 'scope_specified' => boolean, // TRUE if the scope was explicitly specified.
* 'set_scope' => string|false, // Scope for asymmetric visibility.
* // Either public, private, or protected or
* // FALSE if no set scope is specified.
* 'is_static' => boolean, // TRUE if the static keyword was found.
* 'is_readonly' => boolean, // TRUE if the readonly keyword was found.
* 'is_final' => boolean, // TRUE if the final keyword was found.
Expand Down Expand Up @@ -1896,19 +1917,18 @@ public function getMemberProperties($stackPtr)
}

$valid = [
T_PUBLIC => T_PUBLIC,
T_PRIVATE => T_PRIVATE,
T_PROTECTED => T_PROTECTED,
T_STATIC => T_STATIC,
T_VAR => T_VAR,
T_READONLY => T_READONLY,
T_FINAL => T_FINAL,
T_STATIC => T_STATIC,
T_VAR => T_VAR,
T_READONLY => T_READONLY,
T_FINAL => T_FINAL,
];

$valid += Tokens::$scopeModifiers;
$valid += Tokens::$emptyTokens;

$scope = 'public';
$scopeSpecified = false;
$setScope = false;
$isStatic = false;
$isReadonly = false;
$isFinal = false;
Expand Down Expand Up @@ -1941,6 +1961,15 @@ public function getMemberProperties($stackPtr)
$scope = 'protected';
$scopeSpecified = true;
break;
case T_PUBLIC_SET:
$setScope = 'public';
break;
case T_PROTECTED_SET:
$setScope = 'protected';
break;
case T_PRIVATE_SET:
$setScope = 'private';
break;
case T_STATIC:
$isStatic = true;
break;
Expand Down Expand Up @@ -2004,6 +2033,7 @@ public function getMemberProperties($stackPtr)
return [
'scope' => $scope,
'scope_specified' => $scopeSpecified,
'set_scope' => $setScope,
'is_static' => $isStatic,
'is_readonly' => $isReadonly,
'is_final' => $isFinal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public function getErrorList()
101 => 2,
105 => 1,
107 => 1,
109 => 1,
111 => 1,
];

}//end getErrorList()
Expand Down
26 changes: 26 additions & 0 deletions tests/Core/Files/File/GetMemberPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,29 @@ class WithFinalProperties {
/* testPHP84FinalComplexTypedProp */
final public (Foo&\Bar)|bool $val9;
}

class AsymVisibility {
/* testPHP84AsymPublicSetProperty */
public(set) mixed $prop1;
/* testPHP84AsymPublicPublicSetProperty */
public public(set) (A&B)|null $prop2;
/* testPHP84AsymPublicSetPublicProperty */
public(set) public bool $prop3;

/* testPHP84AsymProtectedSetProperty */
protected(set) readonly mixed $prop4;
/* testPHP84AsymPublicProtectedSetProperty */
public protected(set) string $prop5;
/* testPHP84AsymProtectedSetPublicProperty */
protected(set) public ?float $prop6;

/* testPHP84AsymPrivateSetProperty */
private(set) string|int $prop7;
/* testPHP84AsymProtectedPrivateSetProperty */
final protected private(set) $prop8;
/* testPHP84AsymPrivateSetPublicProperty */
private(set) public mixed $prop9;

/* testPHP84IllegalAsymPublicProtectedSetStaticProperty */
public protected(set) static mixed $prop10;
}
Loading