Skip to content

Commit 1e96963

Browse files
committed
Added new MoodleUtil::isLangFile() method
We are going to use it with the LangFilesOrdering Sniff that needs to check when a given file is a lang one or no.
1 parent 3966e9f commit 1e96963

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

moodle/Tests/Util/MoodleUtilTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,55 @@ protected function cleanMoodleUtilCaches() {
480480
$apiCache->setValue(null, []);
481481
}
482482

483+
/**
484+
* Data provider for testIsLangFile.
485+
*
486+
* @return array
487+
*/
488+
public static function isLangFileProvider(): array
489+
{
490+
return [
491+
'Not in lang directory' => [
492+
'value' => '/path/to/standard/file.php',
493+
'return' => false,
494+
],
495+
'In lang/en directory' => [
496+
'value' => '/path/to/standard/lang/en/file.php',
497+
'return' => true,
498+
],
499+
'In lang directory but missing en' => [
500+
'value' => '/path/to/standard/lang/file.php',
501+
'return' => false,
502+
],
503+
'In lang/en directory but missing .php' => [
504+
'value' => '/path/to/standard/lang/en/file',
505+
'return' => false,
506+
],
507+
'In lang/en directory but another extension' => [
508+
'value' => '/path/to/standard/lang/en/file.md',
509+
'return' => false,
510+
],
511+
'In lang sub-directory with not allowed chars' => [
512+
'value' => '/path/to/standard/lang/@@@/file.php',
513+
'return' => false,
514+
],
515+
];
516+
}
517+
518+
/**
519+
* @dataProvider isLangFileProvider
520+
*/
521+
public function testIsLangFile(
522+
string $filepath,
523+
bool $expected
524+
): void {
525+
$phpcsConfig = new Config();
526+
$phpcsRuleset = new Ruleset($phpcsConfig);
527+
$file = new File($filepath, $phpcsRuleset, $phpcsConfig);
528+
529+
$this->assertEquals($expected, MoodleUtil::isLangFile($file));
530+
}
531+
483532
/**
484533
* Data provider for testIsUnitTest.
485534
*

moodle/Util/MoodleUtil.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,28 @@ public static function getMoodleRoot(?File $file = null, bool $selfPath = true):
442442
return self::$moodleRoot;
443443
}
444444

445+
/**
446+
* Whether this file is a lang file.
447+
*
448+
* @param File $phpcsFile
449+
* @return bool
450+
*/
451+
public static function isLangFile(File $phpcsFile): bool
452+
{
453+
// If the file is not under a /lang/[a-zA-Z0-9_-]+/ directory, nothing to check.
454+
// (note that we are using that regex because it's what PARAM_LANG does).
455+
if (preg_match('~/lang/[a-zA-Z0-9_-]+/~', $phpcsFile->getFilename()) === 0) {
456+
return false;
457+
}
458+
459+
// If the file is not a PHP file, nothing to check.
460+
if (substr($phpcsFile->getFilename(), -4) !== '.php') {
461+
return false;
462+
}
463+
464+
return true;
465+
}
466+
445467
/**
446468
* Whether this file is a unit test file.
447469
*

0 commit comments

Comments
 (0)