Skip to content

Commit

Permalink
Properties should not be tested for regular constructors (Fixes #142)
Browse files Browse the repository at this point in the history
Only properties which define a visibility and therefore are promoted
constructor properties are required to define a variable docblock.
  • Loading branch information
andrewnicols committed Mar 31, 2024
1 parent 9b3b3cd commit 582195e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
15 changes: 15 additions & 0 deletions moodle/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ protected function processVariable(File $phpcsFile, $stackPtr) {

$method = $phpcsFile->getTokens()[$methodPtr];
if ($method['parenthesis_opener'] < $stackPtr && $method['parenthesis_closer'] > $stackPtr) {
$lookBackTo = max(
$method['parenthesis_opener'],
$phpcsFile->findPrevious(T_COMMA, $stackPtr)
);
// Only apply to properties declared in the constructor.
$modifierPtr = $phpcsFile->findPrevious(
Collections::propertyModifierKeywords(),
$stackPtr,
$lookBackTo
);
if ($modifierPtr === false) {
// No modifier found.
return;
}

$this->processMemberVar($phpcsFile, $stackPtr);
return;
}
Expand Down
9 changes: 8 additions & 1 deletion moodle/Tests/Sniffs/Commenting/VariableCommentSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function testSniffWithFixtures(
$this->setFixture(sprintf("%s/fixtures/VariableComment/%s.php", __DIR__, $fixture));
$this->setErrors($errors);
$this->setWarnings($warnings);

$this->verifyCsResults();
}

Expand Down Expand Up @@ -90,6 +89,14 @@ public static function fixtureProvider(): array {
],
'warnings' => [],
],
'Constructor with mixed CPP' => [
'fixture' => 'constructor_with_mixed_property_promotion',
'errors' => [
21 => 'Missing member variable doc comment',
],
'warnings' => [
],
],
];

if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;

class constructor_with_mixed_property_promotion {
/**
* Constructor
*
* @param string $serverurl a Moodle URL
* @param string $token the token used to do the web service call
* @param string $example
* @param string $example2
* @param string $example3
*/
public function __construct(
$serverurl,
string $token,
/** @var string The example */
protected string $example,
string $example2,
protected string $example3
) {
}
}

0 comments on commit 582195e

Please sign in to comment.