From dab874139e3b7a5491ab6640bcd69896045652ae Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Fri, 10 Feb 2023 16:41:58 +0100 Subject: [PATCH] Squiz/VariableComment: allow union types to be detected If one uses a union type for an object property, the doc-type-hint could not be related to the variable type. Fixed now. Includes unit test. --- .../Squiz/Sniffs/Commenting/VariableCommentSniff.php | 1 + .../Squiz/Tests/Commenting/VariableCommentUnitTest.inc | 8 ++++++++ .../Tests/Commenting/VariableCommentUnitTest.inc.fixed | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php index 32e89789a7..c40fbf951e 100644 --- a/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php +++ b/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php @@ -40,6 +40,7 @@ public function processMemberVar(File $phpcsFile, $stackPtr) T_STRING => T_STRING, T_NS_SEPARATOR => T_NS_SEPARATOR, T_NULLABLE => T_NULLABLE, + T_TYPE_UNION => T_TYPE_UNION, ]; for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) { diff --git a/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc b/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc index 36efc443bf..f9d5f7b497 100644 --- a/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc +++ b/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc @@ -402,3 +402,11 @@ class ReadOnlyProps private readonly string $variable; } + +class UnionTypes +{ + /** + * @var array|boolean + */ + private array|bool $variableName = array(); +} diff --git a/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc.fixed index 5c652f5402..f96998f682 100644 --- a/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Commenting/VariableCommentUnitTest.inc.fixed @@ -402,3 +402,11 @@ class ReadOnlyProps private readonly string $variable; } + +class UnionTypes +{ + /** + * @var array|boolean + */ + private array|bool $variableName = array(); +}