Skip to content

Commit

Permalink
Create new Util method for counting global artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 19, 2024
1 parent 4a519fe commit bc27d8e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
58 changes: 58 additions & 0 deletions moodle/Tests/Util/TokenUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,62 @@ public static function objectPropertiesProvider(): array {

return $cases;
}

/**
* @dataProvider countGlobalScopesInFileProvider
*/
public function testCountGlobalScopesInFile(
string $content,
int $expectedCount
): void {
$config = new Config([]);
$ruleset = new Ruleset($config);

$phpcsFile = new DummyFile($content, $ruleset, $config);
$phpcsFile->process();

$this->assertEquals($expectedCount, TokenUtil::countGlobalScopesInFile($phpcsFile));
}

public static function countGlobalScopesInFileProvider(): array {
$cases = [
'No global scopes' => [
'<?php $a = 1;',
0,
],
'One global scope' => [
'<?php class Example {}',
1,
],
'Two global scopes' => [
'<?php class Example {} class AnotherExample {}',
2,
],
'Class method is not global' => [
'<?php class Example { public function exampleMethod() {} }',
1,
],
'Global method counts' => [
'<?php function exampleFunction() {}',
1,
],
'Interfaces count' => [
'<?php interface ExampleInterface {}',
1,
],
'Traits count' => [
'<?php trait ExampleTrait {}',
1,
],
];

if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
$cases['Enums count'] = [
'<?php enum ExampleEnum {}',
1,
];
}

return $cases;
}
}
5 changes: 0 additions & 5 deletions moodle/Util/Docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@

namespace MoodleHQ\MoodleCS\moodle\Util;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Util\Tokens;

/**
* Utilities related to PHP DocBlocks.
Expand Down
29 changes: 29 additions & 0 deletions moodle/Util/TokenUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace MoodleHQ\MoodleCS\moodle\Util;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\ObjectDeclarations;

class TokenUtil
Expand Down Expand Up @@ -58,4 +59,32 @@ public static function getObjectName(

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

/**
* Count the number of global scopes in a file.
*
* @param File $phpcsFile
* @return int
*/
public static function countGlobalScopesInFile(
File $phpcsFile
): int {
$tokens = $phpcsFile->getTokens();
$artifactCount = 0;
$find = Tokens::$ooScopeTokens;
$find[] = T_FUNCTION;

$typePtr = 0;
while ($typePtr = $phpcsFile->findNext($find, $typePtr + 1)) {
$token = $tokens[$typePtr];
if ($token['code'] === T_FUNCTION && !empty($token['conditions'])) {
// Skip methods of classes, traits and interfaces.
continue;
}

$artifactCount++;
}

return $artifactCount;
}
}

0 comments on commit bc27d8e

Please sign in to comment.