Skip to content

Commit

Permalink
Merge pull request #168 from PHPCSStandards/feature/php8-union-types
Browse files Browse the repository at this point in the history
PHP 8.0 | Add support for union types
  • Loading branch information
jrfnl committed Jul 17, 2020
2 parents 568b6c9 + 38ad2b0 commit 4d02dd8
Show file tree
Hide file tree
Showing 18 changed files with 1,335 additions and 94 deletions.
9 changes: 6 additions & 3 deletions PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
* // 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 nullable.
* '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.
* )
Expand Down Expand Up @@ -503,7 +504,8 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr)
* '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.
* 'nullable_return_type' => false, // TRUE if the return type is nullable.
* '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.
Expand Down Expand Up @@ -712,7 +714,8 @@ public static function getMethodProperties(File $phpcsFile, $stackPtr)
* // 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 nullable.
* 'nullable_type' => boolean, // TRUE if the type is preceded by the
* // nullability operator.
* );
* ```
*
Expand Down
238 changes: 160 additions & 78 deletions PHPCSUtils/Tokens/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

namespace PHPCSUtils\Tokens;

use PHPCSUtils\BackCompat\Helper;

/**
* Collections of related tokens as often used and needed for sniffs.
*
Expand Down Expand Up @@ -333,23 +331,14 @@ class Collections
];

/**
* Token types which can be encountered in a parameter type declaration.
*
* Sister-property to the {@see Collections::parameterTypeTokensBC()} method.
* The property supports PHPCS 3.3.0 and up.
* The method supports PHPCS 2.6.0 and up.
*
* Notable difference:
* - The method will include the `T_ARRAY_HINT` token when used with PHPCS 2.x and 3.x.
* This token constant will no longer exist in PHPCS 4.x.
*
* It is recommended to use the property instead of the method if a standard supports does
* not need to support PHPCS < 3.3.0.
*
* @see \PHPCSUtils\Tokens\Collections::parameterTypeTokensBC() Related method (cross-version).
* DEPRECATED: Token types which can be encountered in a parameter type declaration.
*
* @since 1.0.0
*
* @deprecated 1.0.0-alpha4 Use the {@see \PHPCSUtils\Tokens\Collections::parameterTypeTokens()}
* or {@see \PHPCSUtils\Tokens\Collections::parameterTypeTokensBC()}
* method instead.
*
* @var array <int|string> => <int|string>
*/
public static $parameterTypeTokens = [
Expand All @@ -376,23 +365,14 @@ class Collections
];

/**
* Token types which can be encountered in a property type declaration.
*
* Sister-property to the {@see Collections::propertyTypeTokensBC()} method.
* The property supports PHPCS 3.3.0 and up.
* The method supports PHPCS 2.6.0 and up.
*
* Notable difference:
* - The method will include the `T_ARRAY_HINT` token when used with PHPCS 2.x and 3.x.
* This token constant will no longer exist in PHPCS 4.x.
*
* It is recommended to use the property instead of the method if a standard supports does
* not need to support PHPCS < 3.3.0.
*
* @see \PHPCSUtils\Tokens\Collections::propertyTypeTokensBC() Related method (cross-version).
* DEPRECATED: Token types which can be encountered in a property type declaration.
*
* @since 1.0.0
*
* @deprecated 1.0.0-alpha4 Use the {@see \PHPCSUtils\Tokens\Collections::propertyTypeTokens()}
* or {@see \PHPCSUtils\Tokens\Collections::propertyTypeTokensBC()}
* method instead.
*
* @var array <int|string> => <int|string>
*/
public static $propertyTypeTokens = [
Expand All @@ -404,24 +384,14 @@ class Collections
];

/**
* Token types which can be encountered in a return type declaration.
*
* Sister-property to the {@see Collections::returnTypeTokensBC()} method.
* The property supports PHPCS 3.5.4 and up.
* The method supports PHPCS 2.6.0 and up.
*
* Notable differences:
* - The method will include the `T_ARRAY_HINT` and the `T_RETURN_TYPE` tokens when used with PHPCS 2.x and 3.x.
* These token constants will no longer exist in PHPCS 4.x.
* - The method will include the `T_ARRAY` token which is needed for select arrow functions in PHPCS < 3.5.4.
*
* It is recommended to use the property instead of the method if a standard supports does
* not need to support PHPCS < 3.5.4.
*
* @see \PHPCSUtils\Tokens\Collections::returnTypeTokensBC() Related method (cross-version).
* DEPRECATED: Token types which can be encountered in a return type declaration.
*
* @since 1.0.0
*
* @deprecated 1.0.0-alpha4 Use the {@see \PHPCSUtils\Tokens\Collections::returnTypeTokens()}
* or {@see \PHPCSUtils\Tokens\Collections::returnTypeTokensBC()}
* method instead.
*
* @var array <int|string> => <int|string>
*/
public static $returnTypeTokens = [
Expand Down Expand Up @@ -606,29 +576,66 @@ public static function functionDeclarationTokensBC()
return $tokens;
}

/**
* Token types which can be encountered in a parameter type declaration.
*
* Sister-method to the {@see Collections::parameterTypeTokensBC()} method.
* This method supports PHPCS 3.3.0 and up.
* The {@see Collections::parameterTypeTokensBC()} method supports PHPCS 2.6.0 and up.
*
* Notable difference:
* - The {@see Collections::parameterTypeTokensBC()} method will include the `T_ARRAY_HINT` token
* when used with PHPCS 2.x and 3.x.
* This token constant will no longer exist in PHPCS 4.x.
*
* It is recommended to use this method instead of the {@see Collections::parameterTypeTokensBC()}
* method if a standard does not need to support PHPCS < 3.3.0.
*
* @see \PHPCSUtils\Tokens\Collections::parameterTypeTokensBC() Related method (cross-version).
*
* @since 1.0.0-alpha4 This method replaces the {@see Collections::$parameterTypeTokens} property.
* @since 1.0.0-alpha4 Added support for PHP 8.0 union types.
*
* @return array <int|string> => <int|string>
*/
public static function parameterTypeTokens()
{
return [
\T_CALLABLE => \T_CALLABLE,
\T_SELF => \T_SELF,
\T_PARENT => \T_PARENT,
\T_FALSE => \T_FALSE, // Union types only.
\T_NULL => \T_NULL, // Union types only.
\T_STRING => \T_STRING,
\T_NS_SEPARATOR => \T_NS_SEPARATOR,
\T_BITWISE_OR => \T_BITWISE_OR, // Union types.
];
}

/**
* Token types which can be encountered in a parameter type declaration (cross-version).
*
* Sister-method to the {@see Collections::$parameterTypeTokens} property.
* The property supports PHPCS 3.3.0 and up.
* The method supports PHPCS 2.6.0 and up.
* Sister-method to the {@see Collections::parameterTypeTokens()} method.
* The {@see Collections::parameterTypeTokens()} method supports PHPCS 3.3.0 and up.
* This method supports PHPCS 2.6.0 and up.
*
* Notable difference:
* - The method will include the `T_ARRAY_HINT` token when used with PHPCS 2.x and 3.x.
* - This method will include the `T_ARRAY_HINT` token when used with PHPCS 2.x and 3.x.
* This token constant will no longer exist in PHPCS 4.x.
*
* It is recommended to use the property instead of the method if a standard supports does
* not need to support PHPCS < 3.3.0.
* It is recommended to use {@see Collections::parameterTypeTokens()} method instead of
* this method if a standard does not need to support PHPCS < 3.3.0.
*
* @see \PHPCSUtils\Tokens\Collections::$parameterTypeTokens Related property (PHPCS 3.3.0+).
* @see \PHPCSUtils\Tokens\Collections::parameterTypeTokens() Related method (PHPCS 3.3.0+).
*
* @since 1.0.0-alpha3
* @since 1.0.0-alpha4 Added support for PHP 8.0 union types.
*
* @return array <int|string> => <int|string>
*/
public static function parameterTypeTokensBC()
{
$tokens = self::$parameterTypeTokens;
$tokens = self::parameterTypeTokens();

// PHPCS < 4.0; Needed for support of PHPCS < 3.3.0. For PHPCS 3.3.0+ the constant is no longer used.
if (\defined('T_ARRAY_HINT') === true) {
Expand All @@ -638,55 +645,138 @@ public static function parameterTypeTokensBC()
return $tokens;
}

/**
* Token types which can be encountered in a property type declaration.
*
* Sister-method to the {@see Collections::propertyTypeTokensBC()} method.
* This method supports PHPCS 3.3.0 and up.
* The {@see Collections::propertyTypeTokensBC()} method supports PHPCS 2.6.0 and up.
*
* Notable difference:
* - The {@see Collections::propertyTypeTokensBC()} method will include the `T_ARRAY_HINT` token
* when used with PHPCS 2.x and 3.x.
* This token constant will no longer exist in PHPCS 4.x.
*
* It is recommended to use this method instead of the {@see Collections::propertyTypeTokensBC()}
* method if a standard does not need to support PHPCS < 3.3.0.
*
* @see \PHPCSUtils\Tokens\Collections::propertyTypeTokensBC() Related method (cross-version).
*
* @since 1.0.0-alpha4 This method replaces the {@see Collections::$propertyTypeTokens} property.
* @since 1.0.0-alpha4 Added support for PHP 8.0 union types.
*
* @return array <int|string> => <int|string>
*/
public static function propertyTypeTokens()
{
return [
\T_CALLABLE => \T_CALLABLE,
\T_SELF => \T_SELF,
\T_PARENT => \T_PARENT,
\T_FALSE => \T_FALSE, // Union types only.
\T_NULL => \T_NULL, // Union types only.
\T_STRING => \T_STRING,
\T_NS_SEPARATOR => \T_NS_SEPARATOR,
\T_BITWISE_OR => \T_BITWISE_OR, // Union types.
];
}

/**
* Token types which can be encountered in a property type declaration (cross-version).
*
* Sister-method to the {@see Collections::$propertyTypeTokens} property.
* The property supports PHPCS 3.3.0 and up.
* The method supports PHPCS 2.6.0 and up.
* Sister-method to the {@see Collections::propertyTypeTokens()} method.
* The {@see Collections::propertyTypeTokens()} method supports PHPCS 3.3.0 and up.
* This method supports PHPCS 2.6.0 and up.
*
* Notable difference:
* - The method will include the `T_ARRAY_HINT` token when used with PHPCS 2.x and 3.x.
* - This method will include the `T_ARRAY_HINT` token when used with PHPCS 2.x and 3.x.
* This token constant will no longer exist in PHPCS 4.x.
*
* It is recommended to use the property instead of the method if a standard supports does
* not need to support PHPCS < 3.3.0.
* It is recommended to use the {@see Collections::propertyTypeTokens()} method instead of
* this method if a standard does not need to support PHPCS < 3.3.0.
*
* @see \PHPCSUtils\Tokens\Collections::$propertyTypeTokens Related property (PHPCS 3.3.0+).
* @see \PHPCSUtils\Tokens\Collections::propertyTypeTokens() Related method (PHPCS 3.3.0+).
*
* @since 1.0.0-alpha3
* @since 1.0.0-alpha4 Added support for PHP 8.0 union types.
*
* @return array <int|string> => <int|string>
*/
public static function propertyTypeTokensBC()
{
return self::parameterTypeTokensBC();
$tokens = self::propertyTypeTokens();

// PHPCS < 4.0; Needed for support of PHPCS < 3.3.0. For PHPCS 3.3.0+ the constant is no longer used.
if (\defined('T_ARRAY_HINT') === true) {
$tokens[\T_ARRAY_HINT] = \T_ARRAY_HINT;
}

return $tokens;
}

/**
* Token types which can be encountered in a return type declaration.
*
* Sister-method to the {@see Collections::returnTypeTokensBC()} method.
* This method supports PHPCS 3.3.0 and up.
* The {@see Collections::returnTypeTokensBC()} method supports PHPCS 2.6.0 and up.
*
* Notable differences:
* - The {@see Collections::returnTypeTokensBC()} method will include the `T_ARRAY_HINT`
* and the `T_RETURN_TYPE` tokens when used with PHPCS 2.x and 3.x.
* These token constants will no longer exist in PHPCS 4.x.
*
* It is recommended to use this method instead of the {@see Collections::returnTypeTokensBC()}
* method if a standard does not need to support PHPCS < 3.3.0.
*
* @see \PHPCSUtils\Tokens\Collections::returnTypeTokensBC() Related method (cross-version).
*
* @since 1.0.0-alpha4 This method replaces the {@see Collections::$returnTypeTokens} property.
* @since 1.0.0-alpha4 Added support for PHP 8.0 union types.
*
* @return array <int|string> => <int|string>
*/
public static function returnTypeTokens()
{
return [
\T_STRING => \T_STRING,
\T_CALLABLE => \T_CALLABLE,
\T_SELF => \T_SELF,
\T_PARENT => \T_PARENT,
\T_STATIC => \T_STATIC,
\T_FALSE => \T_FALSE, // Union types only.
\T_NULL => \T_NULL, // Union types only.
\T_ARRAY => \T_ARRAY, // Arrow functions PHPCS < 3.5.4 + union types.
\T_NS_SEPARATOR => \T_NS_SEPARATOR,
\T_BITWISE_OR => \T_BITWISE_OR, // Union types.
];
}

/**
* Token types which can be encountered in a return type declaration (cross-version).
*
* Sister-property to the {@see Collections::returnTypeTokensBC()} method.
* The property supports PHPCS 3.5.4 and up.
* The method supports PHPCS 2.6.0 and up.
* Sister-method to the {@see Collections::returnTypeTokens()} method.
* The {@see Collections::returnTypeTokens()} method supports PHPCS 3.3.0 and up.
* This method supports PHPCS 2.6.0 and up.
*
* Notable differences:
* - The method will include the `T_ARRAY_HINT` and the `T_RETURN_TYPE` tokens when used with PHPCS 2.x and 3.x.
* - This method will include the `T_ARRAY_HINT` and the `T_RETURN_TYPE` tokens when
* used with PHPCS 2.x and 3.x.
* These token constants will no longer exist in PHPCS 4.x.
* - The method will include the `T_ARRAY` token which is needed for select arrow functions in PHPCS < 3.5.4.
*
* It is recommended to use the property instead of the method if a standard supports does
* not need to support PHPCS < 3.5.4.
* It is recommended to use the {@see Collections::returnTypeTokens()} method instead of
* this method if a standard does not need to support PHPCS < 3.3.0.
*
* @see \PHPCSUtils\Tokens\Collections::$returnTypeTokens Related property (PHPCS 3.5.4+).
* @see \PHPCSUtils\Tokens\Collections::returnTypeTokens() Related method (PHPCS 3.3.0+).
*
* @since 1.0.0-alpha3
* @since 1.0.0-alpha4 Added support for PHP 8.0 union types.
*
* @return array <int|string> => <int|string>
*/
public static function returnTypeTokensBC()
{
$tokens = self::$returnTypeTokens;
$tokens = self::returnTypeTokens();

/*
* PHPCS < 4.0. Needed for support of PHPCS 2.4.0 < 3.3.0.
Expand All @@ -704,14 +794,6 @@ public static function returnTypeTokensBC()
$tokens[\T_ARRAY_HINT] = \T_ARRAY_HINT;
}

/*
* PHPCS < 3.5.4. Needed for support of PHPCS < 3.5.4 for select arrow functions.
* For PHPCS 3.5.4+ the constant is no longer used in return type tokenization.
*/
if (\version_compare(Helper::getVersion(), '3.5.4', '<')) {
$tokens[\T_ARRAY] = \T_ARRAY;
}

return $tokens;
}
}
Loading

0 comments on commit 4d02dd8

Please sign in to comment.