Skip to content
Closed
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
18 changes: 17 additions & 1 deletion coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Drupal\Sniffs\Commenting;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
Expand All @@ -24,6 +25,13 @@
class FunctionCommentSniff implements Sniff
{

/**
* The current PHP version.
*
* @var integer
*/
private $phpVersion = null;

/**
* A map of invalid data types to valid ones for param and return documentation.
*
Expand Down Expand Up @@ -461,6 +469,13 @@ protected function processThrows(File $phpcsFile, $stackPtr, $commentStart)
*/
protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
{
if ($this->phpVersion === null) {
$this->phpVersion = Config::getConfigData('php_version');
if ($this->phpVersion === null) {
$this->phpVersion = PHP_VERSION_ID;
}
}

$tokens = $phpcsFile->getTokens();

$params = [];
Expand Down Expand Up @@ -735,7 +750,8 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
&& isset($realParams[$checkPos]) === true
) {
$typeHint = $realParams[$checkPos]['type_hint'];
if ($typeHint !== '' && $typeHint !== 'stdClass' && $typeHint !== '\stdClass') {
// As of PHP 7.2, object is a valid type hint.
if ($typeHint !== '' && $typeHint !== 'stdClass' && $typeHint !== '\stdClass' && ($this->phpVersion < 70200 || $typeHint !== 'object')) {
$error = 'Unknown type hint "%s" found for %s';
$data = [
$typeHint,
Expand Down
16 changes: 16 additions & 0 deletions coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @file
* Test module.
*/

/**
* @param \stdClass $arg
* Something here.
* @param object $blarg
* Another thing.
*/
function module_handler_test_all2_hook(object $arg, object $blarg) {
return $arg;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @file
* Test module.
*/

/**
* @param object $arg
* Something here.
* @param object $blarg
* Another thing.
*/
function module_handler_test_all2_hook(object $arg, object $blarg) {
return $arg;
}
10 changes: 10 additions & 0 deletions coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ public function getErrorList($testFile=null)

case 'FunctionCommentUnitTest.2.inc':
return [8 => 1];

case 'FunctionCommentUnitTest.3.inc':
if (PHP_VERSION_ID < 70200) {
return [
9 => 1,
14 => 1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not a good idea to have different test results per PHP version. Line 14 should never be flagged as error if the PHP type hint is object. We should just make "object" an acceptable type hint. People might run PHPCS on a lower PHP version and then get different results than on their CI or prod enviorinment. It is just an invitation for confusion.

];
} else {
return [9 => 1];
}//end if
}//end switch

}//end getErrorList()
Expand Down