From bc27d8ea90a55a9195bfd1c4c0be28f089e2dbbe Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 19 Mar 2024 15:54:03 +0800 Subject: [PATCH] Create new Util method for counting global artifacts --- moodle/Tests/Util/TokenUtilTest.php | 58 +++++++++++++++++++++++++++++ moodle/Util/Docblocks.php | 5 --- moodle/Util/TokenUtil.php | 29 +++++++++++++++ 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/moodle/Tests/Util/TokenUtilTest.php b/moodle/Tests/Util/TokenUtilTest.php index d32e537..9f44094 100644 --- a/moodle/Tests/Util/TokenUtilTest.php +++ b/moodle/Tests/Util/TokenUtilTest.php @@ -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' => [ + ' [ + ' [ + ' [ + ' [ + ' [ + ' [ + '= 0) { + $cases['Enums count'] = [ + '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; + } }