Skip to content

Commit

Permalink
Merge pull request #144 from andrewnicols/variableCommentOnConstructor
Browse files Browse the repository at this point in the history
Properties should not be tested for regular constructors (Fixes #142)
  • Loading branch information
stronk7 authored Mar 31, 2024
2 parents 9b3b3cd + e7506b1 commit c09a624
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
19 changes: 19 additions & 0 deletions moodle/Sniffs/Commenting/VariableCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ protected function processVariable(File $phpcsFile, $stackPtr) {

$method = $phpcsFile->getTokens()[$methodPtr];
if ($method['parenthesis_opener'] < $stackPtr && $method['parenthesis_closer'] > $stackPtr) {
// Only apply to properties declared in the constructor.
// Constructor Promoted Properties canbe detected by a visbility keyword.
// These can be found, amongst others like READONLY in Collections::propertyModifierKeywords().
// When searching, only look back to the previous arg (comma), or the opening parenthesis.
$lookBackTo = max(
$method['parenthesis_opener'],
$phpcsFile->findPrevious(T_COMMA, $stackPtr)
);
$modifierPtr = $phpcsFile->findPrevious(
Collections::propertyModifierKeywords(),
$stackPtr,
$lookBackTo
);
if ($modifierPtr === false) {
// No modifier found, so not a promoted property.
return;
}

// This is a promoted property. Handle it in the same way as other properties.
$this->processMemberVar($phpcsFile, $stackPtr);
return;
}
Expand Down
8 changes: 8 additions & 0 deletions moodle/Tests/Sniffs/Commenting/VariableCommentSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,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 c09a624

Please sign in to comment.