8000 BCFile::getMethodParameters(): sync with upstream / readonly without visibility by jrfnl · Pull Request #472 · PHPCSStandards/PHPCSUtils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

BCFile::getMethodParameters(): sync with upstream / readonly without visibility #472

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 1 commit into from
May 19, 2023
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
33 changes: 20 additions & 13 deletions PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,25 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
*
* Parameters declared using PHP 8 constructor property promotion, have these additional array indexes:
* ```php
* 'property_visibility' => string, // The property visibility as declared.
* 'visibility_token' => integer, // The stack pointer to the visibility modifier token.
* 'property_readonly' => bool, // TRUE if the readonly keyword was found.
* 'readonly_token' => integer, // The stack pointer to the readonly modifier token.
* // This index will only be set if the property is readonly.
* 'property_visibility' => string, // The property visibility as declared.
* 'visibility_token' => integer,|false // The stack pointer to the visibility modifier token.
* // or FALSE if the visibility is not explicitly declared.
* 'property_readonly' => bool, // TRUE if the readonly keyword was found.
* 'readonly_token' => integer, // The stack pointer to the readonly modifier token.
* // This index will only be set if the property is readonly.
* ```
*
* PHPCS cross-version compatible version of the `File::getMethodParameters()` method.
*
* Changelog for the PHPCS native function:
* - Introduced in PHPCS 0.0.5.
* - The upstream method has received no significant updates since PHPCS 3.7.1.
* - PHPCS 3.8.0: Added support for constructor property promotion with readonly without explicit visibility.
*
* @see \PHP_CodeSniffer\Files\File::getMethodParameters() Original source.
* @see \PHPCSUtils\Utils\FunctionDeclarations::getParameters() PHPCSUtils native improved version.
*
* @since 1.0.0
* @since 1.0.6 Sync with PHPCS 3.8.0, support for readonly properties without explicit visibility. PHPCS#3801.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position in the stack of the function token
Expand Down Expand Up @@ -380,15 +382,20 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr)
$vars[$paramCount]['type_hint_end_token'] = $typeHintEndToken;
$vars[$paramCount]['nullable_type'] = $nullableType;

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

if ($readonlyToken !== null) {
$vars[$paramCount]['property_readonly'] = true;
$vars[$paramCount]['readonly_token'] = $readonlyToken;
if ($visibilityToken !== null) {
$vars[$paramCount]['property_visibility'] = $tokens[$visibilityToken]['content'];
$vars[$paramCount]['visibility_token'] = $visibilityToken;
}

if ($readonlyToken !== null) {
$vars[$paramCount]['property_readonly'] = true;
$vars[$paramCount]['readonly_token'] = $readonlyToken;
}
}

if ($tokens[$i]['code'] === T_COMMA) {
Expand Down
1 change: 0 additions & 1 deletion PHPCSUtils/Utils/FunctionDeclarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public static function getName(File $phpcsFile, $stackPtr)
* - Defensive coding against incorrect calls to this method.
* - More efficient checking whether a function has a body.
* - Support for PHP 8.0 identifier name tokens in return types, cross-version PHP & PHPCS.
* - Support for constructor property promotion with the PHP 8.1 readonly keyword without explicit visibility.
* - Support for the PHP 8.2 `true` type.
* - The results of this function call are cached during a PHPCS run for faster response times.
*
Expand Down
5 changes: 5 additions & 0 deletions Tests/BackCompat/BCFile/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ class ConstructorPropertyPromotionWithReadOnlyNoTypeDeclaration {
public function __construct(public readonly $promotedProp, ReadOnly private &$promotedToo) {}
}

class ConstructorPropertyPromotionWithOnlyReadOnly {
/* testPHP81ConstructorPropertyPromotionWithOnlyReadOnly */
public function __construct(readonly Foo&Bar $promotedProp, readonly ?bool $promotedToo,) {}
}

/* testPHP8ConstructorPropertyPromotionGlobalFunction */
// Intentional fatal error. Property promotion not allowed in non-constructor, but that's not the concern of this method.
function globalFunction(private $x) {}
Expand Down
53 changes: 52 additions & 1 deletion Tests/BackCompat/BCFile/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
8000 Expand Up @@ -2012,6 +2012,57 @@ public function testPHP81ConstructorPropertyPromotionWithReadOnlyNoTypeDeclarati
$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Verify recognition of PHP8 constructor with property promotion using PHP 8.1 readonly
* keyword without explicit visibility.
*
* @return void
*/
public function testPHP81ConstructorPropertyPromotionWithOnlyReadOnly()
{
$expected = [];
$expected[0] = [
'token' => 10, // Offset from the T_FUNCTION token.
'name' => '$promotedProp',
'content' => 'readonly Foo&Bar $promotedProp',
'has_attributes' => false,
'pass_by_reference' => false,
'reference_token' => false,
'variable_length' => false,
'variadic_token' => false,
'type_hint' => 'Foo&Bar',
'type_hint_token' => 6, // Offset from the T_FUNCTION token.
'type_hint_end_token' => 8, // Offset from the T_FUNCTION token.
'nullable_type' => false,
'property_visibility' => 'public',
'visibility_token' => false,
'property_readonly' => true,
'readonly_token' => 4, // Offset from the T_FUNCTION token.
'comma_token' => 11,
];
$expected[1] = [
'token' => 18, // Offset from the T_FUNCTION token.
'name' => '$promotedToo',
'content' => 'readonly ?bool $promotedToo',
'has_attributes' => false,
'pass_by_reference' => false,
'reference_token' => false,
'variable_length' => false,
'variadic_token' => false,
'type_hint' => '?bool',
'type_hint_token' => 16, // Offset from the T_FUNCTION token.
'type_hint_end_token' => 16, // Offset from the T_FUNCTION token.
'nullable_type' => true,
'property_visibility' => 'public',
'visibility_token' => false,
'property_readonly' => true,
'readonly_token' => 13, // Offset from the T_FUNCTION token.
'comma_token' => 19,
];

$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Verify behaviour when a non-constructor function uses PHP 8 property promotion syntax.
*
Expand Down Expand Up @@ -2717,7 +2768,7 @@ protected function getMethodParametersTestHelper($marker, $expected, $targetType
if (isset($param['default_equal_token'])) {
$expected[$key]['default_equal_token'] += $target;
}
if (isset($param['visibility_token'])) {
if (isset($param['visibility_token']) && $param['visibility_token'] !== false) {
$expected[$key]['visibility_token'] += $target;
}
if (isset($param['readonly_token'])) {
Expand Down
5 changes: 0 additions & 5 deletions Tests/Utils/FunctionDeclarations/GetParametersDiffTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,3 @@ function pseudoTypeTrue(?true $var = true) {}
/* testPHP82PseudoTypeFalseAndTrue */
// Intentional fatal error - Type contains both true and false, bool should be used instead, but that's not the concern of the method.
function pseudoTypeFalseAndTrue(true|false $var = true) {}

class ConstructorPropertyPromotionWithOnlyReadOnly {
/* testPHP81ConstructorPropertyPromotionWithOnlyReadOnly */
public function __construct(readonly Foo&Bar $promotedProp, readonly ?bool $promotedToo,) {}
}
51 changes: 0 additions & 51 deletions Tests/Utils/FunctionDeclarations/GetParametersDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,57 +102,6 @@ public function testPHP82PseudoTypeFalseAndTrue()
$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Verify recognition of PHP8 constructor with property promotion using PHP 8.1 readonly
* keyword without explicit visibility.
*
* @return void
*/
public function testPHP81ConstructorPropertyPromotionWithOnlyReadOnly()
{
$expected = [];
$expected[0] = [
'token' => 10, // Offset from the T_FUNCTION token.
'name' => '$promotedProp',
'content' => 'readonly Foo&Bar $promotedProp',
'has_attributes' => false,
'pass_by_reference' => false,
'reference_token' => false,
'variable_length' => false,
'variadic_token' => false,
'type_hint' => 'Foo&Bar',
'type_hint_token' => 6, // Offset from the T_FUNCTION token.
'type_hint_end_token' => 8, // Offset from the T_FUNCTION token.
'nullable_type' => false,
'property_visibility' => 'public',
'visibility_token' => false,
'property_readonly' => true,
'readonly_token' => 4, // Offset from the T_FUNCTION token.
'comma_token' => 11,
];
$expected[1] = [
'token' => 18, // Offset from the T_FUNCTION token.
'name' => '$promotedToo',
'content' => 'readonly ?bool $promotedToo',
'has_attributes' => false,
'pass_by_reference' => false,
'reference_token' => false,
'variable_length' => false,
'variadic_token' => false,
'type_hint' => '?bool',
'type_hint_token' => 16, // Offset from the T_FUNCTION token.
'type_hint_end_token' => 16, // Offset from the T_FUNCTION token.
'nullable_type' => true,
'property_visibility' => 'public',
'visibility_token' => false,
'property_readonly' => true,
'readonly_token' => 13, // Offset from the T_FUNCTION token.
'comma_token' => 19,
];

$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Test helper.
*
Expand Down
2 changes: 1 addition & 1 deletion Tests/Utils/FunctionDeclarations/GetParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function getMethodParametersTestHelper($marker, $expected, $targetType
if (isset($param['default_equal_token'])) {
$expected[$key]['default_equal_token'] += $target;
}
if (isset($param['visibility_token'])) {
if (isset($param['visibility_token']) && $param['visibility_token'] !== false) {
$expected[$key]['visibility_token'] += $target;
}
if (isset($param['readonly_token'])) {
Expand Down
0