From 3639ade8670fbe2708d6f739402d5591987dc0cf Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 1 Apr 2024 22:04:42 +0800 Subject: [PATCH] Standardise filepath separators to / (Fixes #145 #146) --- moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php | 5 ++-- moodle/Tests/MoodleCSBaseTestCase.php | 6 +++- moodle/Util/Docblocks.php | 2 +- moodle/Util/MoodleUtil.php | 29 +++++++++++++++----- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php index 5e81c37..05f7575 100644 --- a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php +++ b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php @@ -233,9 +233,10 @@ public function process(File $file, $pointer) { if ($bspos !== false) { // Only if there are level2 and down namespace. $relns = str_replace('\\', '/', substr(trim($namespace, ' \\'), $bspos + 1)); + $filename = MoodleUtil::getStandardisedFilename($file); // Calculate the relative path under tests directory. - $dirpos = strripos(trim(dirname($file->getFilename()), ' /') . '/', '/tests/'); - $reldir = str_replace('\\', '/', substr(trim(dirname($file->getFilename()), ' /'), $dirpos + 7)); + $dirpos = strripos(trim(dirname($filename), ' /') . '/', '/tests/'); + $reldir = str_replace('\\', '/', substr(trim(dirname($filename), ' /'), $dirpos + 7)); // Warning if the relative namespace does not match the relative directory. if ($reldir !== $relns) { diff --git a/moodle/Tests/MoodleCSBaseTestCase.php b/moodle/Tests/MoodleCSBaseTestCase.php index 88325eb..baaf1fc 100644 --- a/moodle/Tests/MoodleCSBaseTestCase.php +++ b/moodle/Tests/MoodleCSBaseTestCase.php @@ -221,9 +221,13 @@ protected function verifyCsResults() { // Let's process the fixture. try { if ($this->fixtureFileName !== null) { + $fixtureFilename = $this->fixtureFileName; + if (DIRECTORY_SEPARATOR !== '/') { + $fixtureFilename = str_replace('/', DIRECTORY_SEPARATOR, $fixtureFilename); + } $fixtureSource = file_get_contents($this->fixture); $fixtureContent = <<fixtureFileName} + phpcs_input_file: {$fixtureFilename} {$fixtureSource} EOF; $phpcsfile = new \PHP_CodeSniffer\Files\DummyFile($fixtureContent, $ruleset, $config); diff --git a/moodle/Util/Docblocks.php b/moodle/Util/Docblocks.php index bd4fe0b..6b86cb7 100644 --- a/moodle/Util/Docblocks.php +++ b/moodle/Util/Docblocks.php @@ -324,7 +324,7 @@ public static function isValidTag( $tag = ltrim($tokens[$tagPtr]['content'], '@'); if (array_key_exists($tag, self::$validTags)) { if (array_key_exists($tag, self::$pathRestrictedTags)) { - $file = $phpcsFile->getFilename(); + $file = MoodleUtil::getStandardisedFilename($phpcsFile); foreach (self::$pathRestrictedTags[$tag] as $path) { if (preg_match($path, $file)) { return true; diff --git a/moodle/Util/MoodleUtil.php b/moodle/Util/MoodleUtil.php index 045d3ed..4af8457 100644 --- a/moodle/Util/MoodleUtil.php +++ b/moodle/Util/MoodleUtil.php @@ -242,14 +242,17 @@ public static function getMoodleComponent(File $file, $selfPath = true): ?string } } + $filepath = MoodleUtil::getStandardisedFilename($file); // Let's find the first component that matches the file path. foreach ($components as $component => $componentPath) { // Only components with path. if (empty($componentPath)) { continue; } + // Look for component paths matching the file path. - if (strpos($file->path, $componentPath . '/') === 0) { + $componentPath = str_replace('\\', '/', $componentPath . DIRECTORY_SEPARATOR); + if (strpos($filepath, $componentPath) === 0) { // First match found should be the better one always. We are done. return $component; } @@ -450,14 +453,15 @@ public static function getMoodleRoot(?File $file = null, bool $selfPath = true): */ public static function isLangFile(File $phpcsFile): bool { + $filename = MoodleUtil::getStandardisedFilename($phpcsFile); // If the file is not under a /lang/[a-zA-Z0-9_-]+/ directory, nothing to check. // (note that we are using that regex because it's what PARAM_LANG does). - if (preg_match('~/lang/[a-zA-Z0-9_-]+/~', $phpcsFile->getFilename()) === 0) { + if (preg_match('~/lang/[a-zA-Z0-9_-]+/~', $filename) === 0) { return false; } // If the file is not a PHP file, nothing to check. - if (substr($phpcsFile->getFilename(), -4) !== '.php') { + if (substr($filename, -4) !== '.php') { return false; } @@ -476,28 +480,29 @@ public static function isLangFile(File $phpcsFile): bool */ public static function isUnitTest(File $phpcsFile): bool { + $filename = MoodleUtil::getStandardisedFilename($phpcsFile); // If the file isn't called, _test.php, nothing to check. if (stripos(basename($phpcsFile->getFilename()), '_test.php') === false) { return false; } // If the file isn't under tests directory, nothing to check. - if (stripos($phpcsFile->getFilename(), '/tests/') === false) { + if (stripos($filename, '/tests/') === false) { return false; } // If the file is in a fixture directory, ignore it. - if (stripos($phpcsFile->getFilename(), '/tests/fixtures/') !== false) { + if (stripos($filename, '/tests/fixtures/') !== false) { return false; } // If the file is in a generator directory, ignore it. - if (stripos($phpcsFile->getFilename(), '/tests/generator/') !== false) { + if (stripos($filename, '/tests/generator/') !== false) { return false; } // If the file is in a behat directory, ignore it. - if (stripos($phpcsFile->getFilename(), '/tests/behat/') !== false) { + if (stripos($filename, '/tests/behat/') !== false) { return false; } @@ -609,4 +614,14 @@ public static function getTokensOnLine( ARRAY_FILTER_USE_BOTH ); } + + /** + * Get the standardised filename for the file. + * + * @param File @phpcsFile + * @return string + */ + public static function getStandardisedFilename(File $phpcsFile): string { + return str_replace('\\', '/', $phpcsFile->getFilename()); + } }