Skip to content
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
115 changes: 74 additions & 41 deletions PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
* // 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:
* ```php
* 'set_visibility' => string, // The property set-visibility as declared.
* 'set_visibility_token' => integer, // The stack pointer to the set-visibility modifier token.
* ```
*
* PHPCS cross-version compatible version of the `File::getMethodParameters()` method.
*
* Changelog for the PHPCS native function:
Expand All @@ -196,6 +202,7 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
*
* @since 1.0.0
* @since 1.0.6 Sync with PHPCS 3.8.0, support for readonly properties without explicit visibility. PHPCS#3801.
* @since 1.1.0 Sync with PHPCS 3.13.1, support for asymmetric properties. PHPCS(new)#851
*
* @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 @@ -242,23 +249,24 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr)

$closer = $tokens[$opener]['parenthesis_closer'];

$vars = [];
$currVar = null;
$paramStart = ($opener + 1);
$defaultStart = null;
$equalToken = null;
$paramCount = 0;
$hasAttributes = false;
$passByReference = false;
$referenceToken = false;
$variableLength = false;
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$readonlyToken = null;
$vars = [];
$currVar = null;
$paramStart = ($opener + 1);
$defaultStart = null;
$equalToken = null;
$paramCount = 0;
$hasAttributes = false;
$passByReference = false;
$referenceToken = false;
$variableLength = false;
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$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 @@ -392,6 +400,13 @@ public static function getMethodParameters(File $phpcsFile, $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 @@ -426,16 +441,21 @@ public static function getMethodParameters(File $phpcsFile, $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'] = $tokens[$visibilityToken]['content'];
$vars[$paramCount]['visibility_token'] = $visibilityToken;
}

if ($setVisibilityToken !== null) {
$vars[$paramCount]['set_visibility'] = $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 @@ -449,21 +469,22 @@ public static function getMethodParameters(File $phpcsFile, $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 @@ -532,6 +553,9 @@ public static function getMethodProperties(File $phpcsFile, $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 @@ -598,19 +622,18 @@ public static function getMemberProperties(File $phpcsFile, $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 @@ -643,6 +666,15 @@ public static function getMemberProperties(File $phpcsFile, $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 @@ -692,6 +724,7 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
return [
'scope' => $scope,
'scope_specified' => $scopeSpecified,
'set_scope' => $setScope,
'is_static' => $isStatic,
'is_readonly' => $isReadonly,
'is_final' => $isFinal,
Expand Down
87 changes: 53 additions & 34 deletions PHPCSUtils/Utils/FunctionDeclarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ public static function getProperties(File $phpcsFile, $stackPtr)
* // 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:
* ```php
* 'set_visibility' => string, // The property set-visibility as declared.
* 'set_visibility_token' => int, // The stack pointer to the set-visibility modifier token.
* ```
*
* Main differences with the PHPCS version:
* - Defensive coding against incorrect calls to this method.
* - More efficient and more stable checking whether a `T_USE` token is a closure use.
Expand Down Expand Up @@ -457,23 +463,24 @@ public static function getParameters(File $phpcsFile, $stackPtr)

$closer = $tokens[$opener]['parenthesis_closer'];

$vars = [];
$currVar = null;
$paramStart = ($opener + 1);
$defaultStart = null;
$equalToken = null;
$paramCount = 0;
$hasAttributes = false;
$passByReference = false;
$referenceToken = false;
$variableLength = false;
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$readonlyToken = null;
$vars = [];
$currVar = null;
$paramStart = ($opener + 1);
$defaultStart = null;
$equalToken = null;
$paramCount = 0;
$hasAttributes = false;
$passByReference = false;
$referenceToken = false;
$variableLength = false;
$variadicToken = false;
$typeHint = '';
$typeHintToken = false;
$typeHintEndToken = false;
$nullableType = false;
$visibilityToken = null;
$setVisibilityToken = null;
$readonlyToken = null;

$parameterTypeTokens = Collections::parameterTypeTokens();

Expand Down Expand Up @@ -529,6 +536,12 @@ public static function getParameters(File $phpcsFile, $stackPtr)
$visibilityToken = $i;
break;

case \T_PUBLIC_SET:
case \T_PROTECTED_SET:
case \T_PRIVATE_SET:
$setVisibilityToken = $i;
break;

case \T_READONLY:
$readonlyToken = $i;
break;
Expand Down Expand Up @@ -566,16 +579,21 @@ public static function getParameters(File $phpcsFile, $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'] = $tokens[$visibilityToken]['content'];
$vars[$paramCount]['visibility_token'] = $visibilityToken;
}

if ($setVisibilityToken !== null) {
$vars[$paramCount]['set_visibility'] = $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 @@ -589,21 +607,22 @@ public static function getParameters(File $phpcsFile, $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
23 changes: 23 additions & 0 deletions PHPCSUtils/Utils/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ final class Variables
* 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 @@ -148,6 +151,7 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)

$scope = 'public';
$scopeSpecified = false;
$setScope = false;
$isStatic = false;
$isReadonly = false;
$isFinal = false;
Expand Down Expand Up @@ -180,6 +184,24 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
$scope = 'protected';
$scopeSpecified = true;
break;
case \T_PUBLIC_SET:
$setScope = 'public';
if ($scopeSpecified === false) {
$scope = 'public';
}
break;
case \T_PROTECTED_SET:
$setScope = 'protected';
if ($scopeSpecified === false) {
$scope = 'public';
}
break;
case \T_PRIVATE_SET:
$setScope = 'private';
if ($scopeSpecified === false) {
$scope = 'public';
}
break;
case \T_STATIC:
$isStatic = true;
break;
Expand Down Expand Up @@ -228,6 +250,7 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
$returnValue = [
'scope' => $scope,
'scope_specified' => $scopeSpecified,
'set_scope' => $setScope,
'is_static' => $isStatic,
'is_readonly' => $isReadonly,
'is_final' => $isFinal,
Expand Down
Loading