Skip to content

Commit

Permalink
Various path normalisations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Apr 1, 2024
1 parent 5143bd8 commit c378c5c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
4 changes: 3 additions & 1 deletion moodle/Sniffs/Files/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions moodle/Sniffs/Files/MoodleInternalSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand Down
10 changes: 5 additions & 5 deletions moodle/Sniffs/PHPUnit/TestCaseNamesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
];

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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'],
];

Expand Down
2 changes: 1 addition & 1 deletion moodle/Util/Docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 22 additions & 9 deletions moodle/Util/MoodleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion moodle/Util/TokenUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c378c5c

Please sign in to comment.