-
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.
Move getObject* helpers to their own Util
- Loading branch information
1 parent
88f207d
commit 237cf60
Showing
3 changed files
with
170 additions
and
40 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
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\Tests\Util; | ||
|
||
use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase; | ||
use MoodleHQ\MoodleCS\moodle\Util\TokenUtil; | ||
use PHP_CodeSniffer\Config; | ||
use PHP_CodeSniffer\Ruleset; | ||
use PHP_CodeSniffer\Files\DummyFile; | ||
|
||
/** | ||
* Test the Tokens specific utilities class | ||
* | ||
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com} | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* | ||
* @covers \MoodleHQ\MoodleCS\moodle\Util\TokenUtil | ||
*/ | ||
class TokenUtilTest extends MoodleCSBaseTestCase | ||
{ | ||
/** | ||
* @dataProvider objectPropertiesProvider | ||
*/ | ||
public function testGetObjectProperties( | ||
string $content, | ||
int $type, | ||
string $expectedType, | ||
string $expectedName | ||
): void { | ||
$config = new Config([]); | ||
$ruleset = new Ruleset($config); | ||
|
||
$phpcsFile = new DummyFile($content, $ruleset, $config); | ||
$phpcsFile->process(); | ||
|
||
$stackPtr = $phpcsFile->findNext($type, 0); | ||
|
||
$this->assertEquals($expectedType, TokenUtil::getObjectType($phpcsFile, $stackPtr)); | ||
$this->assertEquals($expectedName, TokenUtil::getObjectName($phpcsFile, $stackPtr)); | ||
} | ||
|
||
public static function objectPropertiesProvider(): array { | ||
$cases = [ | ||
'Class name' => [ | ||
'<?php class Example {}', | ||
T_CLASS, | ||
'class', | ||
'Example', | ||
], | ||
'File name' => [ | ||
// Setting the first line of the file to phpcs_input_file: pathname will set the file name of the dummy file. | ||
<<<EOF | ||
phpcs_input_file: /path/to/file/example.php | ||
<?php class Example {} | ||
EOF, | ||
T_OPEN_TAG, | ||
'file', | ||
'example.php', | ||
], | ||
'Trait name' => [ | ||
'<?php trait ExampleTrait {}', | ||
T_TRAIT, | ||
'trait', | ||
'ExampleTrait', | ||
], | ||
'Interface name' => [ | ||
'<?php interface ExampleInterface {}', | ||
T_INTERFACE, | ||
'interface', | ||
'ExampleInterface', | ||
], | ||
'Function name' => [ | ||
'<?php function exampleFunction(): void {}', | ||
T_FUNCTION, | ||
'function', | ||
'exampleFunction', | ||
], | ||
]; | ||
|
||
if (version_compare(PHP_VERSION, '8.1.0') >= 0) { | ||
$cases['Enum name'] = [ | ||
'<?php enum ExampleEnum {}', | ||
T_ENUM, | ||
'enum', | ||
'ExampleEnum', | ||
]; | ||
} | ||
|
||
return $cases; | ||
} | ||
} |
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,61 @@ | ||
<?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\ObjectDeclarations; | ||
|
||
class TokenUtil | ||
{ | ||
/** | ||
* Get the human-readable object type. | ||
* | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @return string | ||
*/ | ||
public static function getObjectType( | ||
File $phpcsFile, | ||
int $stackPtr | ||
): string { | ||
$tokens = $phpcsFile->getTokens(); | ||
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { | ||
return 'file'; | ||
} | ||
return $tokens[$stackPtr]['content']; | ||
} | ||
|
||
/** | ||
* Get the human readable object name. | ||
* | ||
* @param File $phpcsFile | ||
* @param int $stackPtr | ||
* @return string | ||
*/ | ||
public static function getObjectName( | ||
File $phpcsFile, | ||
int $stackPtr | ||
): string { | ||
$tokens = $phpcsFile->getTokens(); | ||
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { | ||
return basename($phpcsFile->getFilename()); | ||
} | ||
|
||
return ObjectDeclarations::getName($phpcsFile, $stackPtr); | ||
} | ||
} |
237cf60
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function ObjectDeclarations::getName can return null:
public static getName(File $phpcsFile, int $stackPtr) : string|null
So line 60 of moodle/Util/TokenUtil.php also can return null, which throws a PHP Fatal error: Uncaught TypeError: MoodleHQ\MoodleCS\moodle\Util\TokenUtil::getObjectName(): Return value must be of type string, null returned in phar:///moodle-plugin-ci.phar/vendor/moodlehq/moodle-cs/moodle/Util/TokenUtil.php:60
To get rid of the error, I changed line 59 into
return ObjectDeclarations::getName($phpcsFile, $stackPtr) ?? '';
237cf60
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error now also pops up in GitHub actions.
Adding a question mark in line 53 also fixes the problem.
237cf60
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a testcase I can use to identify and fix this?
237cf60
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is related to the coverage.php file.
Sample => error
Sample => no error
237cf60
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect thanks - #147