forked from moodlehq/moodle-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not warn about missing blocks if a method has the Override attribute
Fixes moodlehq#155
- Loading branch information
1 parent
c9b7667
commit fa1c22b
Showing
6 changed files
with
383 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
moodle/Tests/Sniffs/Commenting/fixtures/MissingDocblock/with_and_without_overrides.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit; | ||
|
||
|
||
/** | ||
* Class level docblock. | ||
*/ | ||
class base_class { | ||
#[\Override] | ||
public function has_override(): void {} | ||
|
||
public function no_override(): void {} | ||
} | ||
|
||
/** | ||
* Base interface. | ||
*/ | ||
interface base_interface { | ||
#[\Override] | ||
public function has_override(): void {} | ||
|
||
public function no_override(): void {} | ||
} | ||
|
||
/** | ||
* Class which extends another class. | ||
*/ | ||
class child_class extends base_class { | ||
#[\Override] | ||
public function has_override(): void {} | ||
|
||
public function no_override(): void {} | ||
} | ||
|
||
/** | ||
* Interface which extends another interface. | ||
*/ | ||
interface child_interface extends base_interface { | ||
#[\Override] | ||
public function has_override(): void {} | ||
|
||
public function no_override(): void {} | ||
} | ||
|
||
/** | ||
* Class which implements an interface. | ||
*/ | ||
class child_class_implements_interface implements base_interface { | ||
#[\Override] | ||
public function has_override(): void {} | ||
|
||
public function no_override(): void {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
<?php | ||
|
||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Util; | ||
|
||
use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase; | ||
use MoodleHQ\MoodleCS\moodle\Util\Attributes; | ||
use PHP_CodeSniffer\Config; | ||
use PHP_CodeSniffer\Files\DummyFile; | ||
use PHP_CodeSniffer\Ruleset; | ||
|
||
/** | ||
* Test the Attributes specific moodle utilities class | ||
* | ||
* @copyright 2024 onwards Andrew Lyons <andrew@nicols.co.uk> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* | ||
* @covers \MoodleHQ\MoodleCS\moodle\Util\Attributes | ||
*/ | ||
class AttributesTest extends MoodleCSBaseTestCase | ||
{ | ||
/** | ||
* @dataProvider validTagsProvider | ||
*/ | ||
public function testGetAttributePointers( | ||
string $content, | ||
$stackPtrSearch, | ||
?array $expectations | ||
): void { | ||
$config = new Config([]); | ||
$ruleset = new Ruleset($config); | ||
|
||
$phpcsFile = new DummyFile($content, $ruleset, $config); | ||
$phpcsFile->process(); | ||
|
||
$searchPtr = $phpcsFile->findNext($stackPtrSearch, 0); | ||
|
||
$pointers = Attributes::getAttributePointers($phpcsFile, $searchPtr); | ||
if (count($expectations)) { | ||
foreach ($expectations as $expectation) { | ||
$this->assertCount( | ||
$expectation['count'], | ||
array_filter($pointers, function ($pointer) use ($expectation, $phpcsFile) { | ||
$properties = Attributes::getAttributeProperties($phpcsFile, $pointer); | ||
|
||
return $properties['attribute_name'] === $expectation['name']; | ||
}) | ||
); | ||
} | ||
} else { | ||
$this->assertEmpty($pointers); | ||
} | ||
} | ||
|
||
public static function validTagsProvider(): array { | ||
return [ | ||
'No attributes' => [ | ||
'<?php | ||
/** | ||
* @param string $param | ||
*/ | ||
function exampleFunction(string $param): void {}', | ||
T_FUNCTION, | ||
[], | ||
], | ||
'Attribute matches' => [ | ||
'<?php | ||
/** | ||
* @param string $param | ||
*/ | ||
#[\Override] | ||
function exampleFunction(string $param): void {}', | ||
T_FUNCTION, | ||
[ | ||
['name' => '\\Override', 'count' => 1], | ||
], | ||
], | ||
'Multiple Attribute matches (same)' => [ | ||
'<?php | ||
/** | ||
* @param string $param | ||
*/ | ||
#[\Override] | ||
#[\Override] | ||
function exampleFunction(string $param): void {}', | ||
T_FUNCTION, | ||
[ | ||
['name' => '\\Override', 'count' => 2], | ||
], | ||
], | ||
'Multiple Attribute matches (different)' => [ | ||
'<?php | ||
/** | ||
* @param string $param | ||
*/ | ||
#[\Override] | ||
#[\Other] | ||
#[\Override] | ||
function exampleFunction(string $param): void {}', | ||
T_FUNCTION, | ||
[ | ||
['name' => '\\Override', 'count' => 2], | ||
['name' => '\\Other', 'count' => 1], | ||
], | ||
], | ||
'Attribute on other funciton' => [ | ||
'<?php | ||
function otherFunction(): void {} | ||
#[\Override] | ||
#[\Override] | ||
function exampleFunction(string $param): void {}', | ||
T_FUNCTION, | ||
[], | ||
], | ||
'Attributes with arguments' => [ | ||
'<?php | ||
#[\Route("Example")] | ||
#[\Override] | ||
#[\Route] | ||
#[\core\attribute\deprecated("thing")] | ||
function exampleFunction(string $param): void {}', | ||
T_FUNCTION, | ||
[ | ||
['name' => '\\Override', 'count' => 1], | ||
['name' => '\\Route', 'count' => 2], | ||
['name' => '\\core\\attribute\\deprecated', 'count' => 1], | ||
], | ||
], | ||
'Attribute start will only get that attribute' => [ | ||
'<?php | ||
#[\Route("Example")] | ||
#[\Override] | ||
#[\Route] | ||
#[\core\attribute\deprecated("thing")] | ||
function exampleFunction(string $param): void {}', | ||
T_ATTRIBUTE, | ||
[ | ||
['name' => '\\Route', 'count' => 1], | ||
], | ||
], | ||
'Attribute End will only get that attribute' => [ | ||
'<?php | ||
#[\Route("Example")] | ||
#[\Override] | ||
#[\Route] | ||
#[\core\attribute\deprecated("thing")] | ||
function exampleFunction(string $param): void {}', | ||
T_ATTRIBUTE_END, | ||
[ | ||
['name' => '\\Route', 'count' => 1], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
public function testGetAttributePropertiesNotAnAttribute(): void { | ||
$config = new Config([]); | ||
$ruleset = new Ruleset($config); | ||
|
||
$content = <<<EOF | ||
<?php | ||
#[\Example()] | ||
function foo() {} | ||
EOF; | ||
|
||
$phpcsFile = new DummyFile($content, $ruleset, $config); | ||
$phpcsFile->process(); | ||
|
||
$searchPtr = $phpcsFile->findNext(T_FUNCTION, 0); | ||
|
||
$this->assertNull(Attributes::getAttributeProperties($phpcsFile, $searchPtr)); | ||
} | ||
} |
Oops, something went wrong.