From 2478c0f3ff29d8450a33d7472359eab3c9894f69 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Tue, 17 May 2022 13:44:01 +0200 Subject: [PATCH] Do not crash if comment is missing completely --- .../Sniffs/Commenting/VariableCommentSniff.php | 11 +++++++++-- tests/DigiComp/Sniffs/Commenting/data/comments.php | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php b/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php index d181735..53f7620 100644 --- a/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php +++ b/src/DigiComp/Sniffs/Commenting/VariableCommentSniff.php @@ -22,8 +22,15 @@ class VariableCommentSniff extends \PHP_CodeSniffer\Standards\Squiz\Sniffs\Comme ]; $commentEnd = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true); - - + // this error is copied from above, as it comes with a needed early return + if ( + $commentEnd === false + || ($tokens[$commentEnd]['code'] !== \T_DOC_COMMENT_CLOSE_TAG + && $tokens[$commentEnd]['code'] !== \T_COMMENT) + ) { + $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing'); + return; + } $commentStart = $tokens[$commentEnd]['comment_opener']; $foundVar = null; diff --git a/tests/DigiComp/Sniffs/Commenting/data/comments.php b/tests/DigiComp/Sniffs/Commenting/data/comments.php index 4452582..7ddd7fa 100644 --- a/tests/DigiComp/Sniffs/Commenting/data/comments.php +++ b/tests/DigiComp/Sniffs/Commenting/data/comments.php @@ -20,3 +20,7 @@ class C extends A { */ protected $var; } + +class D extends a { + protected $var; +}