Skip to content

Commit 582195e

Browse files
committed
Properties should not be tested for regular constructors (Fixes #142)
Only properties which define a visibility and therefore are promoted constructor properties are required to define a variable docblock.
1 parent 9b3b3cd commit 582195e

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

moodle/Sniffs/Commenting/VariableCommentSniff.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
179179

180180
$method = $phpcsFile->getTokens()[$methodPtr];
181181
if ($method['parenthesis_opener'] < $stackPtr && $method['parenthesis_closer'] > $stackPtr) {
182+
$lookBackTo = max(
183+
$method['parenthesis_opener'],
184+
$phpcsFile->findPrevious(T_COMMA, $stackPtr)
185+
);
186+
// Only apply to properties declared in the constructor.
187+
$modifierPtr = $phpcsFile->findPrevious(
188+
Collections::propertyModifierKeywords(),
189+
$stackPtr,
190+
$lookBackTo
191+
);
192+
if ($modifierPtr === false) {
193+
// No modifier found.
194+
return;
195+
}
196+
182197
$this->processMemberVar($phpcsFile, $stackPtr);
183198
return;
184199
}

moodle/Tests/Sniffs/Commenting/VariableCommentSniffTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public function testSniffWithFixtures(
4242
$this->setFixture(sprintf("%s/fixtures/VariableComment/%s.php", __DIR__, $fixture));
4343
$this->setErrors($errors);
4444
$this->setWarnings($warnings);
45-
4645
$this->verifyCsResults();
4746
}
4847

@@ -90,6 +89,14 @@ public static function fixtureProvider(): array {
9089
],
9190
'warnings' => [],
9291
],
92+
'Constructor with mixed CPP' => [
93+
'fixture' => 'constructor_with_mixed_property_promotion',
94+
'errors' => [
95+
21 => 'Missing member variable doc comment',
96+
],
97+
'warnings' => [
98+
],
99+
],
93100
];
94101

95102
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;
4+
5+
class constructor_with_mixed_property_promotion {
6+
/**
7+
* Constructor
8+
*
9+
* @param string $serverurl a Moodle URL
10+
* @param string $token the token used to do the web service call
11+
* @param string $example
12+
* @param string $example2
13+
* @param string $example3
14+
*/
15+
public function __construct(
16+
$serverurl,
17+
string $token,
18+
/** @var string The example */
19+
protected string $example,
20+
string $example2,
21+
protected string $example3
22+
) {
23+
}
24+
}

0 commit comments

Comments
 (0)