Skip to content

BCFile::getMemberProperties(): sync with upstream #646

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

Merged
merged 4 commits into from
Mar 12, 2025
Merged
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
143 changes: 141 additions & 2 deletions PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ public static function getMethodProperties(File $phpcsFile, $stackPtr)
* 'scope_specified' => boolean, // TRUE if the scope was explicitly 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.
* 'type' => string, // The type of the var (empty if no type specified).
* 'type_token' => integer|false, // The stack pointer to the start of the type
* // or FALSE if there is no type.
Expand All @@ -553,7 +554,7 @@ public static function getMethodProperties(File $phpcsFile, $stackPtr)
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - The upstream method has received no significant updates since PHPCS 3.10.1.
* - PHPCS 3.12.0: report final properties
*
* @see \PHP_CodeSniffer\Files\File::getMemberProperties() Original source.
* @see \PHPCSUtils\Utils\Variables::getMemberProperties() PHPCSUtils native improved version.
Expand All @@ -572,7 +573,145 @@ public static function getMethodProperties(File $phpcsFile, $stackPtr)
*/
public static function getMemberProperties(File $phpcsFile, $stackPtr)
{
return $phpcsFile->getMemberProperties($stackPtr);
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] !== T_VARIABLE) {
throw new RuntimeException('$stackPtr must be of type T_VARIABLE');
}

$conditions = array_keys($tokens[$stackPtr]['conditions']);
$ptr = array_pop($conditions);
if (isset($tokens[$ptr]) === false
|| ($tokens[$ptr]['code'] !== T_CLASS
&& $tokens[$ptr]['code'] !== T_ANON_CLASS
&& $tokens[$ptr]['code'] !== T_TRAIT)
) {
if (isset($tokens[$ptr]) === true
&& ($tokens[$ptr]['code'] === T_INTERFACE
|| $tokens[$ptr]['code'] === T_ENUM)
) {
// T_VARIABLEs in interfaces/enums can actually be method arguments
// but they won't be seen as being inside the method because there
// are no scope openers and closers for abstract methods. If it is in
// parentheses, we can be pretty sure it is a method argument.
if (isset($tokens[$stackPtr]['nested_parenthesis']) === false
|| empty($tokens[$stackPtr]['nested_parenthesis']) === true
) {
$error = 'Possible parse error: %ss may not include member vars';
$code = sprintf('Internal.ParseError.%sHasMemberVar', ucfirst($tokens[$ptr]['content']));
$data = [strtolower($tokens[$ptr]['content'])];
$phpcsFile->addWarning($error, $stackPtr, $code, $data);
return [];
}
} else {
throw new RuntimeException('$stackPtr is not a class member var');
}
}

// Make sure it's not a method parameter.
if (empty($tokens[$stackPtr]['nested_parenthesis']) === false) {
$parenthesis = array_keys($tokens[$stackPtr]['nested_parenthesis']);
$deepestOpen = array_pop($parenthesis);
if ($deepestOpen > $ptr
&& isset($tokens[$deepestOpen]['parenthesis_owner']) === true
&& $tokens[$tokens[$deepestOpen]['parenthesis_owner']]['code'] === T_FUNCTION
) {
throw new RuntimeException('$stackPtr is not a class member var');
}
}

$valid = Collections::propertyModifierKeywords();
$valid += Tokens::$emptyTokens;

$scope = 'public';
$scopeSpecified = false;
$isStatic = false;
$isReadonly = false;
$isFinal = false;

$startOfStatement = $phpcsFile->findPrevious(
[
T_SEMICOLON,
T_OPEN_CURLY_BRACKET,
T_CLOSE_CURLY_BRACKET,
T_ATTRIBUTE_END,
],
($stackPtr - 1)
);

for ($i = ($startOfStatement + 1); $i < $stackPtr; $i++) {
if (isset($valid[$tokens[$i]['code']]) === false) {
break;
}

switch ($tokens[$i]['code']) {
case T_PUBLIC:
$scope = 'public';
$scopeSpecified = true;
break;
case T_PRIVATE:
$scope = 'private';
$scopeSpecified = true;
break;
case T_PROTECTED:
$scope = 'protected';
$scopeSpecified = true;
break;
case T_STATIC:
$isStatic = true;
break;
case T_READONLY:
$isReadonly = true;
break;
case T_FINAL:
$isFinal = true;
break;
}
}

$type = '';
$typeToken = false;
$typeEndToken = false;
$nullableType = false;
$propertyTypeTokens = Collections::propertyTypeTokens();

if ($i < $stackPtr) {
// We've found a type.
for ($i; $i < $stackPtr; $i++) {
if ($tokens[$i]['code'] === T_VARIABLE) {
// Hit another variable in a group definition.
break;
}

if ($tokens[$i]['code'] === T_NULLABLE) {
$nullableType = true;
}

if (isset($propertyTypeTokens[$tokens[$i]['code']]) === true) {
$typeEndToken = $i;
if ($typeToken === false) {
$typeToken = $i;
}

$type .= $tokens[$i]['content'];
}
}

if ($type !== '' && $nullableType === true) {
$type = '?' . $type;
}
}

return [
'scope' => $scope,
'scope_specified' => $scopeSpecified,
'is_static' => $isStatic,
'is_readonly' => $isReadonly,
'is_final' => $isFinal,
'type' => $type,
'type_token' => $typeToken,
'type_end_token' => $typeEndToken,
'nullable_type' => $nullableType,
];
}

/**
Expand Down
1 change: 1 addition & 0 deletions PHPCSUtils/Tokens/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ final class Collections
\T_STATIC => \T_STATIC,
\T_VAR => \T_VAR,
\T_READONLY => \T_READONLY,
\T_FINAL => \T_FINAL,
];

/**
Expand Down
6 changes: 6 additions & 0 deletions PHPCSUtils/Utils/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ final class Variables
* 'scope_specified' => boolean, // TRUE if the scope was explicitly 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.
* 'type' => string, // The type of the var (empty if no type specified).
* 'type_token' => integer|false, // The stack pointer to the start of the type
* // or FALSE if there is no type.
Expand Down Expand Up @@ -149,6 +150,7 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
$scopeSpecified = false;
$isStatic = false;
$isReadonly = false;
$isFinal = false;

$startOfStatement = $phpcsFile->findPrevious(
[
Expand Down Expand Up @@ -184,6 +186,9 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
case \T_READONLY:
$isReadonly = true;
break;
case \T_FINAL:
$isFinal = true;
break;
}
}

Expand Down Expand Up @@ -225,6 +230,7 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
'scope_specified' => $scopeSpecified,
'is_static' => $isStatic,
'is_readonly' => $isReadonly,
'is_final' => $isFinal,
'type' => $type,
'type_token' => $typeToken,
'type_end_token' => $typeEndToken,
Expand Down
21 changes: 21 additions & 0 deletions Tests/BackCompat/BCFile/GetMemberPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,24 @@ trait DNFTypes {
// Intentional fatal error - nullable operator cannot be combined with DNF.
var ?(A&\Pck\B)|bool $propD;
}

class WithFinalProperties {
/* testPHP84FinalPublicTypedProp */
final public string $val1;
/* testPHP84FinalProtectedTypedProp */
final protected string $val2;
/* testPHP84FinalMiddleTypedProp */
public final string $val3;
/* testPHP84FinalMiddleStaticTypedProp */
public final static string $val4;
/* testPHP84FinalLastTypedProp */
public readonly final string $val5;
/* testPHP84FinalImplicitVisibilityTypedProp */
final string $val6;
/* testPHP84FinalImplicitVisibilityProp */
final $val7;
/* testPHP84FinalNullableTypedProp */
final public ?string $val8;
/* testPHP84FinalComplexTypedProp */
final public (Foo&\Bar)|bool $val9;
}
Loading