-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Missing docblocks for #[\Override]
Fixes #155 POC
- Loading branch information
1 parent
02a279e
commit 9d2c0dc
Showing
6 changed files
with
267 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,71 @@ | ||
<?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 <[email protected]> | ||
* @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, | ||
string|int $stackPtrSearch, | ||
?array $expected | ||
): void { | ||
$config = new Config([]); | ||
$ruleset = new Ruleset($config); | ||
|
||
$phpcsFile = new DummyFile($content, $ruleset, $config); | ||
$phpcsFile->process(); | ||
|
||
$tokens = $phpcsFile->getTokens(); | ||
$docPtr = $phpcsFile->findNext($stackPtrSearch, 0); | ||
$docblock = $tokens[$docPtr]; | ||
$testPtr = reset($docblock['comment_tags']); | ||
|
||
$this->assertEquals($expected, Docblocks::isValidTag($phpcsFile, $testPtr)); | ||
} | ||
|
||
public static function validTagsProvider(): array { | ||
return [ | ||
'No attrinbutes' => [ | ||
'<?php | ||
/** | ||
* @param string $param | ||
*/ | ||
function exampleFunction(string $param): void {}', | ||
T_FUNCTION, | ||
true, | ||
], | ||
]; | ||
} | ||
} |
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,106 @@ | ||
<?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\Util; | ||
|
||
use PHP_CodeSniffer\Files\File; | ||
use PHPCSUtils\Utils\Context; | ||
use PHPCSUtils\Utils\Namespaces; | ||
|
||
/** | ||
* Utilities related to PHP Attributes. | ||
* | ||
* @copyright 2024 Andrew Lyons <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
abstract class Attributes | ||
{ | ||
/** | ||
* Get the pointer for an Attribute on an Attributable object. | ||
* | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @return array | ||
*/ | ||
public static function getAttributePointers( | ||
File $phpcsFile, | ||
int $stackPtr | ||
): array { | ||
$tokens = $phpcsFile->getTokens(); | ||
$attributes = []; | ||
|
||
$stopAt = [ | ||
T_DOC_COMMENT_CLOSE_TAG, | ||
T_CLOSE_CURLY_BRACKET, | ||
T_OPEN_CURLY_BRACKET, | ||
T_SEMICOLON, | ||
]; | ||
|
||
for ($attributePtr = $stackPtr; $attributePtr > 0; $attributePtr--) { | ||
$token = $tokens[$attributePtr]; | ||
if (isset($token['attribute_opener'])) { | ||
$attributePtr = $token['attribute_opener']; | ||
$attributes[] = $attributePtr; | ||
} elseif (isset($token['attribute_closer'])) { | ||
$attributes[] = $attributePtr; | ||
} elseif (Context::inAttribute($phpcsFile, $attributePtr)) { | ||
$attributePtr = $token['attribute_opener']; | ||
$attributes[] = $attributePtr; | ||
} | ||
|
||
if (in_array($token['code'], $stopAt)) { | ||
break; | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
public static function getAttributeProperties( | ||
File $phpcsFile, | ||
int $stackPtr | ||
): ?array { | ||
$tokens = $phpcsFile->getTokens(); | ||
if (!isset($tokens[$stackPtr]['attribute_opener'])) { | ||
return null; | ||
} | ||
|
||
$opener = $tokens[$stackPtr]['attribute_opener']; | ||
$closer = $tokens[$stackPtr]['attribute_closer']; | ||
|
||
$properties = [ | ||
'attribute_opener' => $opener, | ||
'attribute_closer' => $closer, | ||
'attribute_name' => null, | ||
]; | ||
|
||
$stopAt = [ | ||
T_OPEN_PARENTHESIS, | ||
]; | ||
|
||
for ($i = $opener + 1; $i < $closer; $i++) { | ||
if (in_array($tokens[$i]['code'], $stopAt)) { | ||
break; | ||
} | ||
$properties['attribute_name'] .= $tokens[$i]['content']; | ||
} | ||
|
||
// TODO Get the qualified name. | ||
|
||
return $properties; | ||
} | ||
} |