diff --git a/moodle/Sniffs/Files/LineLengthSniff.php b/moodle/Sniffs/Files/LineLengthSniff.php index f12e8c9..e6b385b 100644 --- a/moodle/Sniffs/Files/LineLengthSniff.php +++ b/moodle/Sniffs/Files/LineLengthSniff.php @@ -24,6 +24,7 @@ namespace MoodleHQ\MoodleCS\moodle\Sniffs\Files; +use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil; use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff as GenericLineLengthSniff; use PHP_CodeSniffer\Sniffs\Sniff; use PHP_CodeSniffer\Files\File; @@ -37,7 +38,8 @@ public function __construct() { public function process(File $file, $stackptr) { // Lang files are allowed to have long lines. - if (strpos($file->getFilename(), DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR) !== false) { + $filename = MoodleUtil::getStandardisedFilename($file); + if (strpos($filename, '/lang/') !== false) { return; } parent::process($file, $stackptr); diff --git a/moodle/Sniffs/Files/MoodleInternalSniff.php b/moodle/Sniffs/Files/MoodleInternalSniff.php index 7dbbe24..7d96095 100644 --- a/moodle/Sniffs/Files/MoodleInternalSniff.php +++ b/moodle/Sniffs/Files/MoodleInternalSniff.php @@ -49,8 +49,9 @@ public function register() { public function process(File $file, $pointer) { // Guess moodle root, so we can do better dispensations below. $moodleRoot = MoodleUtil::getMoodleRoot($file); + $filepath = MoodleUtil::getStandardisedFilename($file); if ($moodleRoot) { - $relPath = str_replace('\\', '/', substr($file->path, strlen($moodleRoot))); + $relPath = substr($filepath, strlen($moodleRoot); // Special dispensation for /tests/behat/ and /lib/behat/ dirs at any level. if (strpos($relPath, '/tests/behat/') !== false || strpos($relPath, '/lib/behat/') !== false) { return; @@ -62,11 +63,11 @@ public function process(File $file, $pointer) { } else { // Falback to simpler dispensations, only looking 1 level. // Special dispensation for behat files. - if (basename(dirname($file->getFilename())) === 'behat') { + if (basename(dirname($filepath)) === 'behat') { return; } // Special dispensation for lang files. - if (basename(dirname(dirname($file->getFilename()))) === 'lang') { + if (basename(dirname(dirname($filepath))) === 'lang') { return; } } diff --git a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php index 5e81c37..4001ea0 100644 --- a/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php +++ b/moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php @@ -82,7 +82,7 @@ public function process(File $file, $pointer) { if (!MoodleUtil::isUnitTest($file) && !$runningPHPUnit) { return; // @codeCoverageIgnore } - $fileName = basename($file->getFilename()); + $fileName = basename(MoodleUtil::getStandardisedFilename($file)); // In order to cover the duplicates detection, we need to set some // properties (caches) here. It's extremely hard to do @@ -195,7 +195,7 @@ public function process(File $file, $pointer) { // Add the new element. $this->foundClasses[$fdqnClass][] = [ - 'file' => $file->getFilename(), + 'file' => $fileName, 'line' => $tokens[$cStart]['line'], ]; @@ -234,8 +234,8 @@ public function process(File $file, $pointer) { $relns = str_replace('\\', '/', substr(trim($namespace, ' \\'), $bspos + 1)); // 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) { @@ -282,7 +282,7 @@ public function process(File $file, $pointer) { // Add the new element. $this->proposedClasses[$fdqnProposed][] = [ - 'file' => $file->getFilename(), + 'file' => $fileName, 'line' => $tokens[$cStart]['line'], ]; diff --git a/moodle/Util/Docblocks.php b/moodle/Util/Docblocks.php index 4c87eff..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 = str_replace(DIRECTORY_SEPARATOR, "/", $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 e22fad7..e2836c3 100644 --- a/moodle/Util/MoodleUtil.php +++ b/moodle/Util/MoodleUtil.php @@ -243,13 +243,14 @@ public static function getMoodleComponent(File $file, $selfPath = true): ?string } // Let's find the first component that matches the file path. + $filepath = self::getStandardisedFilename($file)); 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 . DIRECTORY_SEPARATOR) === 0) { + if (strpos($filepath, $componentPath . DIRECTORY_SEPARATOR) === 0) { // First match found should be the better one always. We are done. return $component; } @@ -410,7 +411,7 @@ public static function getMoodleRoot(?File $file = null, bool $selfPath = true): // Still not found, let's look upwards for a main version file and config-dist.php file // starting from the file path being checked (given it has been passed). if ($file instanceof File) { - $path = $lastPath = $file->path; + $path = $lastPath = self::getStandardisedFilename($file)); while (($path = pathinfo($path, PATHINFO_DIRNAME)) !== $lastPath) { // If we find both a version.php and config-dist.php file then we have arrived to moodle root. if (is_readable($path . '/version.php') && is_readable($path . '/config-dist.php')) { @@ -450,14 +451,15 @@ public static function getMoodleRoot(?File $file = null, bool $selfPath = true): */ public static function isLangFile(File $phpcsFile): bool { + $filename = self::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 +478,29 @@ public static function isLangFile(File $phpcsFile): bool */ public static function isUnitTest(File $phpcsFile): bool { + $filename = self::getStandardisedFilename($phpcsFile); // If the file isn't called, _test.php, nothing to check. - if (stripos(basename($phpcsFile->getFilename()), '_test.php') === false) { + if (stripos(basename($filename), '_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 +612,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()); + } } diff --git a/moodle/Util/TokenUtil.php b/moodle/Util/TokenUtil.php index 07c13ce..8fd1f6a 100644 --- a/moodle/Util/TokenUtil.php +++ b/moodle/Util/TokenUtil.php @@ -54,7 +54,7 @@ public static function getObjectName( ): string { $tokens = $phpcsFile->getTokens(); if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { - return basename($phpcsFile->getFilename()); + return basename(MoodleUtil::getStandardisedFilename($phpcsFile)); } return ObjectDeclarations::getName($phpcsFile, $stackPtr);