Skip to content

Commit

Permalink
Merge pull request #458 from PHPCSStandards/develop
Browse files Browse the repository at this point in the history
Release 1.0.4
  • Loading branch information
jrfnl committed Apr 15, 2023
2 parents 73d7b07 + 5070cee commit 6b0c204
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 87 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ This projects adheres to [Keep a CHANGELOG](https://keepachangelog.com/) and use

_Nothing yet._

## [1.0.4] - 2023-04-15

### Changed

#### Other

* Minor documentation improvements.

### Fixed

#### Utils

* The `FunctionDeclarations::getParameters()` method will now correctly handle constructor promoted properties with `readonly`, but without explicit visibility set. [#456]

[#456]: https://github.com/PHPCSStandards/PHPCSUtils/pull/456


## [1.0.3] - 2023-04-13

### Changed
Expand Down Expand Up @@ -822,6 +839,7 @@ This initial alpha release contains the following utility classes:


[Unreleased]: https://github.com/PHPCSStandards/PHPCSUtils/compare/stable...HEAD
[1.0.4]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.0.3...1.0.4
[1.0.3]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.0.2...1.0.3
[1.0.2]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.0.1...1.0.2
[1.0.1]: https://github.com/PHPCSStandards/PHPCSUtils/compare/1.0.0...1.0.1
Expand Down
91 changes: 46 additions & 45 deletions PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,25 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
* Each parameter is in the following format:
* ```php
* 0 => array(
* 'name' => '$var', // The variable name.
* 'token' => integer, // The stack pointer to the variable name.
* 'content' => string, // The full content of the variable definition.
* 'has_attributes' => boolean, // Does the parameter have one or more attributes attached ?
* 'pass_by_reference' => boolean, // Is the variable passed by reference?
* 'reference_token' => integer, // The stack pointer to the reference operator
* // or FALSE if the param is not passed by reference.
* 'variable_length' => boolean, // Is the param of variable length through use of `...` ?
* 'variadic_token' => integer, // The stack pointer to the ... operator
* // or FALSE if the param is not variable length.
* 'type_hint' => string, // The type hint for the variable.
* 'type_hint_token' => integer, // The stack pointer to the start of the type hint
* // or FALSE if there is no type hint.
* 'type_hint_end_token' => integer, // The stack pointer to the end of the type hint
* // or FALSE if there is no type hint.
* 'nullable_type' => boolean, // TRUE if the var type is preceded by the nullability
* // operator.
* 'comma_token' => integer, // The stack pointer to the comma after the param
* // or FALSE if this is the last param.
* 'name' => string, // The variable name.
* 'token' => integer, // The stack pointer to the variable name.
* 'content' => string, // The full content of the variable definition.
* 'has_attributes' => boolean, // Does the parameter have one or more attributes attached ?
* 'pass_by_reference' => boolean, // Is the variable passed by reference?
* 'reference_token' => integer|false, // The stack pointer to the reference operator
* // or FALSE if the param is not passed by reference.
* 'variable_length' => boolean, // Is the param of variable length through use of `...` ?
* 'variadic_token' => integer|false, // The stack pointer to the ... operator
* // or FALSE if the param is not variable length.
* 'type_hint' => string, // The type hint for the variable.
* 'type_hint_token' => integer|false, // The stack pointer to the start of the type hint
* // or FALSE if there is no type hint.
* 'type_hint_end_token' => integer|false, // The stack pointer to the end of the type hint
* // or FALSE if there is no type hint.
* 'nullable_type' => boolean, // TRUE if the var type is preceded by the nullability
* // operator.
* 'comma_token' => integer|false, // The stack pointer to the comma after the param
* // or FALSE if this is the last param.
* )
* ```
*
Expand All @@ -142,6 +142,7 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
* '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.
* ```
*
* PHPCS cross-version compatible version of the `File::getMethodParameters()` method.
Expand Down Expand Up @@ -431,19 +432,19 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr)
* The format of the return value is:
* ```php
* array(
* 'scope' => 'public', // Public, private, or protected
* 'scope_specified' => true, // TRUE if the scope keyword was found.
* 'return_type' => '', // The return type of the method.
* 'return_type_token' => integer, // The stack pointer to the start of the return type
* // or FALSE if there is no return type.
* 'return_type_end_token' => integer, // The stack pointer to the end of the return type
* // or FALSE if there is no return type.
* 'nullable_return_type' => false, // TRUE if the return type is preceded by
* // the nullability operator.
* 'is_abstract' => false, // TRUE if the abstract keyword was found.
* 'is_final' => false, // TRUE if the final keyword was found.
* 'is_static' => false, // TRUE if the static keyword was found.
* 'has_body' => false, // TRUE if the method has a body
* 'scope' => string, // Public, private, or protected
* 'scope_specified' => boolean, // TRUE if the scope keyword was found.
* 'return_type' => string, // The return type of the method.
* 'return_type_token' => integer|false, // The stack pointer to the start of the return type
* // or FALSE if there is no return type.
* 'return_type_end_token' => integer|false, // The stack pointer to the end of the return type
* // or FALSE if there is no return type.
* 'nullable_return_type' => boolean, // TRUE if the return type is preceded by
* // the nullability operator.
* 'is_abstract' => boolean, // TRUE if the abstract keyword was found.
* 'is_final' => boolean, // TRUE if the final keyword was found.
* 'is_static' => boolean, // TRUE if the static keyword was found.
* 'has_body' => boolean, // TRUE if the method has a body
* );
* ```
*
Expand Down Expand Up @@ -478,17 +479,17 @@ public static function getMethodProperties(File $phpcsFile, $stackPtr)
* The format of the return value is:
* ```php
* array(
* 'scope' => string, // Public, private, or protected.
* '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.
* 'type' => string, // The type of the var (empty if no type specified).
* 'type_token' => integer, // The stack pointer to the start of the type
* // or FALSE if there is no type.
* 'type_end_token' => integer, // The stack pointer to the end of the type
* // or FALSE if there is no type.
* 'nullable_type' => boolean, // TRUE if the type is preceded by the
* // nullability operator.
* 'scope' => string, // Public, private, or protected.
* '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.
* '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.
* 'type_end_token' => integer|false, // The stack pointer to the end of the type
* // or FALSE if there is no type.
* 'nullable_type' => boolean, // TRUE if the type is preceded by the
* // nullability operator.
* );
* ```
*
Expand Down Expand Up @@ -524,8 +525,8 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
* The format of the return value is:
* ```php
* array(
* 'is_abstract' => false, // TRUE if the abstract keyword was found.
* 'is_final' => false, // TRUE if the final keyword was found.
* 'is_abstract' => boolean, // TRUE if the abstract keyword was found.
* 'is_final' => boolean, // TRUE if the final keyword was found.
* );
* ```
*
Expand Down
68 changes: 38 additions & 30 deletions PHPCSUtils/Utils/FunctionDeclarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ 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.
*
* @see \PHP_CodeSniffer\Files\File::getMethodProperties() Original source.
Expand Down Expand Up @@ -314,25 +315,25 @@ public static function getProperties(File $phpcsFile, $stackPtr)
*
* ```php
* 0 => array(
* 'name' => string, // The variable name.
* 'token' => int, // The stack pointer to the variable name.
* 'content' => string, // The full content of the variable definition.
* 'has_attributes' => bool, // Does the parameter have one or more attributes attached ?
* 'pass_by_reference' => bool, // Is the variable passed by reference?
* 'reference_token' => int, // The stack pointer to the reference operator
* // or FALSE if the param is not passed by reference.
* 'variable_length' => bool, // Is the param of variable length through use of `...` ?
* 'variadic_token' => int, // The stack pointer to the ... operator
* // or FALSE if the param is not variable length.
* 'type_hint' => string, // The type hint for the variable.
* 'type_hint_token' => int, // The stack pointer to the start of the type hint
* // or FALSE if there is no type hint.
* 'type_hint_end_token' => int, // The stack pointer to the end of the type hint
* // or FALSE if there is no type hint.
* 'nullable_type' => bool, // TRUE if the var type is preceded by the nullability
* // operator.
* 'comma_token' => int, // The stack pointer to the comma after the param
* // or FALSE if this is the last param.
* 'name' => string, // The variable name.
* 'token' => int, // The stack pointer to the variable name.
* 'content' => string, // The full content of the variable definition.
* 'has_attributes' => bool, // Does the parameter have one or more attributes attached ?
* 'pass_by_reference' => bool, // Is the variable passed by reference?
* 'reference_token' => int|false, // The stack pointer to the reference operator
* // or FALSE if the param is not passed by reference.
* 'variable_length' => bool, // Is the param of variable length through use of `...` ?
* 'variadic_token' => int|false, // The stack pointer to the ... operator
* // or FALSE if the param is not variable length.
* 'type_hint' => string, // The type hint for the variable.
* 'type_hint_token' => int|false, // The stack pointer to the start of the type hint
* // or FALSE if there is no type hint.
* 'type_hint_end_token' => int|false, // The stack pointer to the end of the type hint
* // or FALSE if there is no type hint.
* 'nullable_type' => bool, // TRUE if the var type is preceded by the nullability
* // operator.
* 'comma_token' => int|false, // The stack pointer to the comma after the param
* // or FALSE if this is the last param.
* )
* ```
*
Expand All @@ -345,10 +346,12 @@ public static function getProperties(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' => int, // The stack pointer to the visibility modifier token.
* 'property_readonly' => bool, // TRUE if the readonly keyword was found.
* 'readonly_token' => int, // The stack pointer to the readonly modifier token.
* 'property_visibility' => string, // The property visibility as declared.
* 'visibility_token' => int|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' => int, // The stack pointer to the readonly modifier token.
* // This index will only be set if the property is readonly.
* ```
*
* Main differences with the PHPCS version:
Expand Down Expand Up @@ -532,15 +535,20 @@ public static function getParameters(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
22 changes: 11 additions & 11 deletions PHPCSUtils/Utils/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ final class Variables
* The format of the return value is:
* ```php
* array(
* 'scope' => string, // Public, private, or protected.
* '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.
* 'type' => string, // The type of the var (empty if no type specified).
* 'type_token' => integer, // The stack pointer to the start of the type
* // or FALSE if there is no type.
* 'type_end_token' => integer, // The stack pointer to the end of the type
* // or FALSE if there is no type.
* 'nullable_type' => boolean, // TRUE if the type is preceded by the
* // nullability operator.
* 'scope' => string, // Public, private, or protected.
* '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.
* '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.
* 'type_end_token' => integer|false, // The stack pointer to the end of the type
* // or FALSE if there is no type.
* 'nullable_type' => boolean, // TRUE if the type is preceded by the
* // nullability operator.
* );
* ```
*
Expand Down
5 changes: 5 additions & 0 deletions Tests/Utils/FunctionDeclarations/GetParametersDiffTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ 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,) {}
}

0 comments on commit 6b0c204

Please sign in to comment.