Skip to content

Commit

Permalink
fix(FunctionComment): Fix support for PHP 8 intersection types (#3303…
Browse files Browse the repository at this point in the history
…625)
  • Loading branch information
klausi authored Apr 28, 2023
1 parent 6689a21 commit 5a1779e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
11 changes: 7 additions & 4 deletions coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$commentLines = [];
if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
$matches = [];
preg_match('/([^$&]*)(?:((?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);
preg_match('/((?:(?![$.]|&(?=\$)).)*)(?:((?:\.\.\.)?(?:\$|&)[^\s]+)(?:(\s+)(.*))?)?/', $tokens[($tag + 2)]['content'], $matches);

$typeLen = strlen($matches[1]);
$type = trim($matches[1]);
Expand Down Expand Up @@ -618,7 +618,7 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
$variableArguments = true;
}

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

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

return $type;

Expand Down
13 changes: 13 additions & 0 deletions tests/Drupal/Commenting/FunctionCommentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,16 @@ function test_return_non_falsy_string(): string {
function test_return_literal_string(): string {
return '';
}

/**
* PHP 8 intersection types are ok.
*
* @param Foo&Bar $a
* Intersection type parameter.
*
* @return Foo&Bar
* Intersection type return declaration.
*/
function test_intersection_types(Foo&Bar $a): Foo&Bar {
return new Xyz();
}
13 changes: 13 additions & 0 deletions tests/Drupal/Commenting/FunctionCommentUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,16 @@ function test_return_non_falsy_string(): string {
function test_return_literal_string(): string {
return '';
}

/**
* PHP 8 intersection types are ok.
*
* @param Foo&Bar $a
* Intersection type parameter.
*
* @return Foo&Bar
* Intersection type return declaration.
*/
function test_intersection_types(Foo&Bar $a): Foo&Bar {
return new Xyz();
}

0 comments on commit 5a1779e

Please sign in to comment.