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
4 changes: 4 additions & 0 deletions PHPCSUtils/BackCompat/BCFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public static function getDeclarationName(File $phpcsFile, $stackPtr)
* - PHPCS 3.5.3: Fixed a bug where the `"type_hint_end_token"` array index for a type hinted
* parameter would bleed through to the next (non-type hinted) parameter.
* - PHPCS 3.5.3: Added support for PHP 7.4 `T_FN` arrow functions.
* - PHPCS 3.5.7: Added support for namespace operators in type declarations. PHPCS#3066.
*
* @see \PHP_CodeSniffer\Files\File::getMethodParameters() Original source.
* @see \PHPCSUtils\Utils\FunctionDeclarations::getParameters() PHPCSUtils native improved version.
Expand Down Expand Up @@ -418,6 +419,7 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr)
$typeHintEndToken = $i;
}
break;
case 'T_NAMESPACE':
case 'T_NS_SEPARATOR':
// Part of a type hint or default value.
if ($defaultStart === null) {
Expand Down Expand Up @@ -538,6 +540,7 @@ public static function getMethodParameters(File $phpcsFile, $stackPtr)
* - PHPCS 3.5.0: The Exception thrown changed from a `\PHP_CodeSniffer\Exceptions\TokenizerException`
* to `\PHP_CodeSniffer\Exceptions\RuntimeException`.
* - PHPCS 3.5.3: Added support for PHP 7.4 `T_FN` arrow functions.
* - PHPCS 3.5.7: Added support for namespace operators in type declarations. PHPCS#3066.
*
* @see \PHP_CodeSniffer\Files\File::getMethodProperties() Original source.
* @see \PHPCSUtils\Utils\FunctionDeclarations::getProperties() PHPCSUtils native improved version.
Expand Down Expand Up @@ -743,6 +746,7 @@ public static function getMethodProperties(File $phpcsFile, $stackPtr)
* and comments in the `"type"` value.
* - PHPCS 3.5.0: The Exception thrown changed from a `\PHP_CodeSniffer\Exceptions\TokenizerException`
* to `\PHP_CodeSniffer\Exceptions\RuntimeException`.
* - PHPCS 3.5.7: Added support for namespace operators in type declarations. PHPCS#3066.
*
* @see \PHP_CodeSniffer\Files\File::getMemberProperties() Original source.
* @see \PHPCSUtils\Utils\Variables::getMemberProperties() PHPCSUtils native improved version.
Expand Down
2 changes: 0 additions & 2 deletions PHPCSUtils/Utils/FunctionDeclarations.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ public static function getName(File $phpcsFile, $stackPtr)
* - To allow for backward compatible handling of arrow functions, this method will also accept
* `T_STRING` tokens and examine them to check if these are arrow functions.
* - Support for PHP 8.0 union types.
* - Support for namespace operator in type declarations.
* - Support for PHP 8.0 identifier name tokens in return types, cross-version PHP & PHPCS.
*
* @see \PHP_CodeSniffer\Files\File::getMethodProperties() Original source.
Expand Down Expand Up @@ -403,7 +402,6 @@ public static function getProperties(File $phpcsFile, $stackPtr)
* `T_STRING` tokens and examine them to check if these are arrow functions.
* - Support for PHP 8.0 union types.
* - Support for PHP 8.0 constructor property promotion.
* - Support for namespace operator in type declarations.
* - Support for PHP 8.0 identifier name tokens in parameter types, cross-version PHP & PHPCS.
*
* @see \PHP_CodeSniffer\Files\File::getMethodParameters() Original source.
Expand Down
1 change: 0 additions & 1 deletion PHPCSUtils/Utils/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class Variables
* other non-property variables passed to the method.
* - Defensive coding against incorrect calls to this method.
* - Support for PHP 8.0 union types.
* - Support for namespace operator in type declarations.
* - Support PHP 8.0 identifier name tokens in property types, cross-version PHP & PHPCS.
*
* @see \PHP_CodeSniffer\Files\File::getMemberProperties() Original source.
Expand Down
5 changes: 5 additions & 0 deletions Tests/BackCompat/BCFile/GetMemberPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,8 @@ class PHP8Mixed {
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
private ?mixed $nullableMixed;
}

class NSOperatorInType {
/* testNamespaceOperatorTypeHint */
public ?namespace\Name $prop;
}
12 changes: 12 additions & 0 deletions Tests/BackCompat/BCFile/GetMemberPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,18 @@ public function dataGetMemberProperties()
'nullable_type' => true,
],
],
[
'/* testNamespaceOperatorTypeHint */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => '?namespace\Name',
'type_token' => ($php8Names === true) ? -2 : -4, // Offset from the T_VARIABLE token.
'type_end_token' => -2, // Offset from the T_VARIABLE token.
'nullable_type' => true,
],
],
];
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/BackCompat/BCFile/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ function mixedTypeHint(mixed &...$var1) {}
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
function mixedTypeHintNullable(?Mixed $var1) {}

/* testNamespaceOperatorTypeHint */
function namespaceOperatorTypeHint(?namespace\Name $var1) {}

/* testFunctionCallFnPHPCS353-354 */
$value = $obj->fn(true);

Expand Down
28 changes: 28 additions & 0 deletions Tests/BackCompat/BCFile/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,34 @@ public function testPHP8MixedTypeHintNullable()
$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Verify recognition of type declarations using the namespace operator.
*
* @return void
*/
public function testNamespaceOperatorTypeHint()
{
$php8Names = parent::usesPhp8NameTokens();

$expected = [];
$expected[0] = [
'token' => ($php8Names === true) ? 7 : 9, // Offset from the T_FUNCTION token.
'name' => '$var1',
'content' => '?namespace\Name $var1',
'pass_by_reference' => false,
'reference_token' => false,
'variable_length' => false,
'variadic_token' => false,
'type_hint' => '?namespace\Name',
'type_hint_token' => 5, // Offset from the T_FUNCTION token.
'type_hint_end_token' => ($php8Names === true) ? 5 : 7, // Offset from the T_FUNCTION token.
'nullable_type' => true,
'comma_token' => false,
];

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

/**
* Verify handling of a closure.
*
Expand Down
3 changes: 3 additions & 0 deletions Tests/BackCompat/BCFile/GetMethodPropertiesTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function mixedTypeHint() :mixed {}
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
function mixedTypeHintNullable(): ?mixed {}

/* testNamespaceOperatorTypeHint */
function namespaceOperatorTypeHint() : ?namespace\Name {}

/* testNotAFunction */
return true;

Expand Down
22 changes: 22 additions & 0 deletions Tests/BackCompat/BCFile/GetMethodPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,28 @@ public function testPHP8MixedTypeHintNullable()
$this->getMethodPropertiesTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Test a function with return type using the namespace operator.
*
* @return void
*/
public function testNamespaceOperatorTypeHint()
{
$expected = [
'scope' => 'public',
'scope_specified' => false,
'return_type' => '?namespace\Name',
'return_type_token' => 9, // Offset from the T_FUNCTION token.
'nullable_return_type' => true,
'is_abstract' => false,
'is_final' => false,
'is_static' => false,
'has_body' => true,
];

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

/**
* Test for incorrect tokenization of array return type declarations in PHPCS < 2.8.0.
*
Expand Down
3 changes: 0 additions & 3 deletions Tests/Utils/FunctionDeclarations/GetParametersDiffTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,3 @@ abstract class ConstructorPropertyPromotionAbstractMethod {
// 3. The callable type is not supported for properties, but that's not the concern of this method.
abstract public function __construct(public callable $y, private ...$x);
}

/* testNamespaceOperatorTypeHint */
function namespaceOperatorTypeHint(?namespace\Name $var1) {}
28 changes: 0 additions & 28 deletions Tests/Utils/FunctionDeclarations/GetParametersDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,34 +631,6 @@ public function testPHP8ConstructorPropertyPromotionAbstractMethod()
$this->getParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Verify recognition of type declarations using the namespace operator.
*
* @return void
*/
public function testNamespaceOperatorTypeHint()
{
$php8Names = parent::usesPhp8NameTokens();

$expected = [];
$expected[0] = [
'token' => ($php8Names === true) ? 7 : 9, // Offset from the T_FUNCTION token.
'name' => '$var1',
'content' => '?namespace\Name $var1',
'pass_by_reference' => false,
'reference_token' => false,
'variable_length' => false,
'variadic_token' => false,
'type_hint' => '?namespace\Name',
'type_hint_token' => 5, // Offset from the T_FUNCTION token.
'type_hint_end_token' => ($php8Names === true) ? 5 : 7, // Offset from the T_FUNCTION token.
'nullable_type' => true,
'comma_token' => false,
];

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

/**
* Test helper.
*
Expand Down
3 changes: 0 additions & 3 deletions Tests/Utils/FunctionDeclarations/GetPropertiesDiffTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,3 @@ interface FooBar {
/* testPHP8DuplicateTypeInUnionWhitespaceAndComment */
// Intentional fatal error - duplicate types are not allowed in union types, but that's not the concern of the method.
function duplicateTypeInUnion(): int | /*comment*/ string | INT {}

/* testNamespaceOperatorTypeHint */
function namespaceOperatorTypeHint() : ?namespace\Name {}
25 changes: 0 additions & 25 deletions Tests/Utils/FunctionDeclarations/GetPropertiesDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,31 +371,6 @@ public function testPHP8DuplicateTypeInUnionWhitespaceAndComment()
$this->getPropertiesTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
}

/**
* Test a function with return type using the namespace operator.
*
* @return void
*/
public function testNamespaceOperatorTypeHint()
{
$php8Names = parent::usesPhp8NameTokens();

$expected = [
'scope' => 'public',
'scope_specified' => false,
'return_type' => '?namespace\Name',
'return_type_token' => 9, // Offset from the T_FUNCTION token.
'return_type_end_token' => ($php8Names === true) ? 9 : 11, // Offset from the T_FUNCTION token.
'nullable_return_type' => true,
'is_abstract' => false,
'is_final' => false,
'is_static' => false,
'has_body' => true,
];

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

/**
* Test helper.
*
Expand Down
5 changes: 0 additions & 5 deletions Tests/Utils/Variables/GetMemberPropertiesDiffTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,3 @@ $anon = new class() {
// Intentional fatal error - duplicate types are not allowed in union types, but that's not the concern of the method.
public int |string| /*comment*/ INT $duplicateTypeInUnion;
};

class NSOperatorInType {
/* testNamespaceOperatorTypeHint */
public ?namespace\Name $prop;
}
12 changes: 0 additions & 12 deletions Tests/Utils/Variables/GetMemberPropertiesDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,6 @@ public function dataGetMemberProperties()
'nullable_type' => false,
],
],
'namespace-operator-type-declaration' => [
'/* testNamespaceOperatorTypeHint */',
[
'scope' => 'public',
'scope_specified' => true,
'is_static' => false,
'type' => '?namespace\Name',
'type_token' => ($php8Names === true) ? -2 : -4, // Offset from the T_VARIABLE token.
'type_end_token' => -2, // Offset from the T_VARIABLE token.
'nullable_type' => true,
],
],
];
}
}