Skip to content

Commit

Permalink
Object names may not be present for all objects
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Apr 2, 2024
1 parent 95d2abd commit de8408f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
19 changes: 16 additions & 3 deletions moodle/Tests/Util/TokenUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class TokenUtilTest extends MoodleCSBaseTestCase
*/
public function testGetObjectProperties(
string $content,
int $type,
string $expectedType,
string $expectedName
$type,
?string $expectedType,
?string $expectedName
): void {
$config = new Config([]);
$ruleset = new Ruleset($config);
Expand Down Expand Up @@ -90,6 +90,19 @@ public static function objectPropertiesProvider(): array {
'function',
'exampleFunction',
],
'Unnamed anonymous class' => [
<<<EOF
<?php
return new class extends phpunit_coverage_info {
/** @var array The list of folders relative to the plugin root to include in coverage generation. */
protected \$includelistfolders = ['classes'];
};
EOF,
T_ANON_CLASS,
'class',
'anonymous class',
],
];

if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
Expand Down
16 changes: 14 additions & 2 deletions moodle/Util/TokenUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,29 @@ public static function getObjectType(
*
* @param File $phpcsFile
* @param int $stackPtr
* @return string
* @return null|string
*/
public static function getObjectName(
File $phpcsFile,
int $stackPtr
): string {
): ?string {
$tokens = $phpcsFile->getTokens();
if (!isset($tokens[$stackPtr])) {
return '';

Check warning on line 57 in moodle/Util/TokenUtil.php

View check run for this annotation

Codecov / codecov/patch

moodle/Util/TokenUtil.php#L57

Added line #L57 was not covered by tests
}

if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
return basename($phpcsFile->getFilename());
}

if ($tokens[$stackPtr]['code'] === T_ANON_CLASS) {
return 'anonymous class';
}

if ($tokens[$stackPtr]['code'] === T_CLOSURE) {
return 'closure';

Check warning on line 69 in moodle/Util/TokenUtil.php

View check run for this annotation

Codecov / codecov/patch

moodle/Util/TokenUtil.php#L69

Added line #L69 was not covered by tests
}

return ObjectDeclarations::getName($phpcsFile, $stackPtr);
}

Expand Down

0 comments on commit de8408f

Please sign in to comment.