Skip to content

fix(FunctionComment): Fix support for PHP 8 intersection types #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2023
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
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();
}