From 3f5e5cafdaa1a622327d549be2698ecc5c4cf46a Mon Sep 17 00:00:00 2001 From: pwolanin Date: Thu, 6 Jun 2019 10:39:22 -0400 Subject: [PATCH 1/3] fix(FunctionCommentSniff): Support object type hint for php 7.2 and greater (#3059698) --- .../Sniffs/Commenting/FunctionCommentSniff.php | 18 +++++++++++++++++- .../Commenting/FunctionCommentUnitTest.3.inc | 14 ++++++++++++++ .../FunctionCommentUnitTest.3.inc.fixed | 14 ++++++++++++++ .../Commenting/FunctionCommentUnitTest.php | 10 ++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc create mode 100644 coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc.fixed diff --git a/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php b/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php index dd4978e8..5303f14f 100644 --- a/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php +++ b/coder_sniffer/Drupal/Sniffs/Commenting/FunctionCommentSniff.php @@ -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; @@ -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. * @@ -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 = []; @@ -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, diff --git a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc new file mode 100644 index 00000000..d6a6a700 --- /dev/null +++ b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc @@ -0,0 +1,14 @@ + 1]; + + case 'FunctionCommentUnitTest.3.inc': + if (PHP_VERSION_ID < 70200) { + return [ + 9 => 1, + 12 => 1, + ]; + } else { + return [9 => 1]; + }//end if }//end switch }//end getErrorList() From 9c418ee339d14d3560f43c3219b13acdf9505d05 Mon Sep 17 00:00:00 2001 From: pwolanin Date: Thu, 6 Jun 2019 11:54:13 -0400 Subject: [PATCH 2/3] fix whitespace --- .../Drupal/Test/Commenting/FunctionCommentUnitTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php index 04f4bb48..73d84d14 100644 --- a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php +++ b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php @@ -82,12 +82,12 @@ public function getErrorList($testFile=null) case 'FunctionCommentUnitTest.3.inc': if (PHP_VERSION_ID < 70200) { - return [ - 9 => 1, - 12 => 1, - ]; + return [ + 9 => 1, + 12 => 1, + ]; } else { - return [9 => 1]; + return [9 => 1]; }//end if }//end switch From 70265f53cfc89720ae765a8d3cb9e8fdd5b1d8e1 Mon Sep 17 00:00:00 2001 From: pwolanin Date: Thu, 6 Jun 2019 12:34:20 -0400 Subject: [PATCH 3/3] test fix --- .../Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc | 4 +++- .../Test/Commenting/FunctionCommentUnitTest.3.inc.fixed | 4 +++- .../Drupal/Test/Commenting/FunctionCommentUnitTest.php | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc index d6a6a700..dd4b4d22 100644 --- a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc +++ b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc @@ -8,7 +8,9 @@ /** * @param \stdClass $arg * Something here. + * @param object $blarg + * Another thing. */ -function module_handler_test_all2_hook(object $arg) { +function module_handler_test_all2_hook(object $arg, object $blarg) { return $arg; } diff --git a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc.fixed b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc.fixed index 33f8399e..fa2791c5 100644 --- a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc.fixed +++ b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.3.inc.fixed @@ -8,7 +8,9 @@ /** * @param object $arg * Something here. + * @param object $blarg + * Another thing. */ -function module_handler_test_all2_hook(object $arg) { +function module_handler_test_all2_hook(object $arg, object $blarg) { return $arg; } diff --git a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php index 73d84d14..02103462 100644 --- a/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php +++ b/coder_sniffer/Drupal/Test/Commenting/FunctionCommentUnitTest.php @@ -84,7 +84,7 @@ public function getErrorList($testFile=null) if (PHP_VERSION_ID < 70200) { return [ 9 => 1, - 12 => 1, + 14 => 1, ]; } else { return [9 => 1];