Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties should not be tested for regular constructors (Fixes #142) #144

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
) {
}
}