Skip to content

Commit 626e90f

Browse files
committed
Create new Util method for counting global artifacts
1 parent 979a494 commit 626e90f

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

moodle/Tests/Util/TokenUtilTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,62 @@ public static function objectPropertiesProvider(): array {
103103

104104
return $cases;
105105
}
106+
107+
/**
108+
* @dataProvider countGlobalScopesInFileProvider
109+
*/
110+
public function testCountGlobalScopesInFile(
111+
string $content,
112+
int $expectedCount
113+
): void {
114+
$config = new Config([]);
115+
$ruleset = new Ruleset($config);
116+
117+
$phpcsFile = new DummyFile($content, $ruleset, $config);
118+
$phpcsFile->process();
119+
120+
$this->assertEquals($expectedCount, TokenUtil::countGlobalScopesInFile($phpcsFile));
121+
}
122+
123+
public static function countGlobalScopesInFileProvider(): array {
124+
$cases = [
125+
'No global scopes' => [
126+
'<?php $a = 1;',
127+
0,
128+
],
129+
'One global scope' => [
130+
'<?php class Example {}',
131+
1,
132+
],
133+
'Two global scopes' => [
134+
'<?php class Example {} class AnotherExample {}',
135+
2,
136+
],
137+
'Class method is not global' => [
138+
'<?php class Example { public function exampleMethod() {} }',
139+
1,
140+
],
141+
'Global method counts' => [
142+
'<?php function exampleFunction() {}',
143+
1,
144+
],
145+
'Interfaces count' => [
146+
'<?php interface ExampleInterface {}',
147+
1,
148+
],
149+
'Traits count' => [
150+
'<?php trait ExampleTrait {}',
151+
1,
152+
],
153+
];
154+
155+
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
156+
$cases['Enums count'] = [
157+
'<?php enum ExampleEnum {}',
158+
1,
159+
];
160+
}
161+
162+
return $cases;
163+
}
106164
}

moodle/Util/TokenUtil.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MoodleHQ\MoodleCS\moodle\Util;
1919

2020
use PHP_CodeSniffer\Files\File;
21+
use PHP_CodeSniffer\Util\Tokens;
2122
use PHPCSUtils\Utils\ObjectDeclarations;
2223

2324
class TokenUtil
@@ -58,4 +59,32 @@ public static function getObjectName(
5859

5960
return ObjectDeclarations::getName($phpcsFile, $stackPtr);
6061
}
62+
63+
/**
64+
* Count the number of global scopes in a file.
65+
*
66+
* @param File $phpcsFile
67+
* @return int
68+
*/
69+
public static function countGlobalScopesInFile(
70+
File $phpcsFile
71+
): int {
72+
$tokens = $phpcsFile->getTokens();
73+
$artifactCount = 0;
74+
$find = Tokens::$ooScopeTokens;
75+
$find[] = T_FUNCTION;
76+
77+
$typePtr = 0;
78+
while ($typePtr = $phpcsFile->findNext($find, $typePtr + 1)) {
79+
$token = $tokens[$typePtr];
80+
if ($token['code'] === T_FUNCTION && !empty($token['conditions'])) {
81+
// Skip methods of classes, traits and interfaces.
82+
continue;
83+
}
84+
85+
$artifactCount++;
86+
}
87+
88+
return $artifactCount;
89+
}
6190
}

0 commit comments

Comments
 (0)