Skip to content

Commit 5a1779e

Browse files
authored
fix(FunctionComment): Fix support for PHP 8 intersection types (#3303625)
1 parent 6689a21 commit 5a1779e

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
482482
$commentLines = [];
483483
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
484484
$matches = [];
485-
preg_match('/([^$&]*)(?:((?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
485+
preg_match('/((?:(?![$.]|&(?=\$)).)*)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
486486

487487
$typeLen = strlen($matches[1]);
488488
$type = trim($matches[1]);
@@ -618,7 +618,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
618618
$variableArguments = true;
619619
}
620620

621-
if ($typeLen === 0) {
621+
if ($typeLen === 0 && $variableArguments === false) {
622622
$error = 'Missing parameter type';
623623
// If there is just one word as comment at the end of the line
624624
// then this is probably the data type. Move it before the
@@ -677,8 +677,11 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
677677
while (isset($realParams[($checkPos)]) === true) {
678678
$realName = $realParams[$checkPos]['name'];
679679

680-
if ($realName === $param['var'] || ($realParams[$checkPos]['pass_by_reference'] === true
680+
if ($realName === $param['var']
681+
|| ($realParams[$checkPos]['pass_by_reference'] === true
681682
&& ('&'.$realName) === $param['var'])
683+
|| ($realParams[$checkPos]['variable_length'] === true
684+
&& ('...'.$realName) === $param['var'])
682685
) {
683686
$matched = true;
684687
break;
@@ -923,7 +926,7 @@ public static function suggestType($type)
923926
// Also allow some more characters for special type hints supported by
924927
// PHPStan:
925928
// https://phpstan.org/writing-php-code/phpdoc-types#basic-types .
926-
$type = preg_replace('/[^a-zA-Z0-9_\\\[\]\-<> ,"\{\}\?\':\*]/', '', $type);
929+
$type = preg_replace('/[^a-zA-Z0-9_\\\[\]\-<> ,"\{\}\?\':\*\|\&]/', '', $type);
927930

928931
return $type;
929932

tests/Drupal/Commenting/FunctionCommentUnitTest.inc

+13
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,16 @@ function test_return_non_falsy_string(): string {
903903
function test_return_literal_string(): string {
904904
return '';
905905
}
906+
907+
/**
908+
* PHP 8 intersection types are ok.
909+
*
910+
* @param Foo&Bar $a
911+
* Intersection type parameter.
912+
*
913+
* @return Foo&Bar
914+
* Intersection type return declaration.
915+
*/
916+
function test_intersection_types(Foo&Bar $a): Foo&Bar {
917+
return new Xyz();
918+
}

tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -929,3 +929,16 @@ function test_return_non_falsy_string(): string {
929929
function test_return_literal_string(): string {
930930
return '';
931931
}
932+
933+
/**
934+
* PHP 8 intersection types are ok.
935+
*
936+
* @param Foo&Bar $a
937+
* Intersection type parameter.
938+
*
939+
* @return Foo&Bar
940+
* Intersection type return declaration.
941+
*/
942+
function test_intersection_types(Foo&Bar $a): Foo&Bar {
943+
return new Xyz();
944+
}

0 commit comments

Comments
 (0)