Skip to content

Commit 27bff0d

Browse files
Rokas Mikalkėnaskukulich
authored andcommitted
Fix: exclude provided base path in TypeNameMatchesFileNameSniff
Fix: exclude provided base path in TypeNameMatchesFileNameSniff Change file path normalization.
1 parent b905a82 commit 27bff0d

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

SlevomatCodingStandard/Sniffs/Files/TypeNameMatchesFileNameSniff.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
use function sprintf;
1616
use function str_replace;
1717
use function strcasecmp;
18+
use function strlen;
19+
use function substr;
1820
use function ucfirst;
1921
use function uksort;
22+
use const DIRECTORY_SEPARATOR;
2023
use const T_CLASS;
2124
use const T_INTERFACE;
2225
use const T_STRING;
@@ -88,8 +91,14 @@ public function process(File $phpcsFile, $typePointer): void
8891
return;
8992
}
9093

94+
$filename = str_replace('/', DIRECTORY_SEPARATOR, $phpcsFile->getFilename());
95+
$basePath = str_replace('/', DIRECTORY_SEPARATOR, $phpcsFile->config->basepath ?? '');
96+
if ($basePath !== '' && StringHelper::startsWith($filename, $basePath)) {
97+
$filename = substr($filename, strlen($basePath));
98+
}
99+
91100
$expectedTypeName = $this->getNamespaceExtractor()->getTypeNameFromProjectPath(
92-
$phpcsFile->getFilename()
101+
$filename
93102
);
94103
if ($typeName === $expectedTypeName) {
95104
return;

SlevomatCodingStandard/Sniffs/TestCase.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
use PHP_CodeSniffer\Runner;
99
use ReflectionClass;
1010
use function array_map;
11+
use function array_merge;
1112
use function count;
13+
use function define;
14+
use function defined;
1215
use function implode;
1316
use function in_array;
1417
use function preg_replace;
@@ -28,14 +31,16 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
2831
* @param string $filePath
2932
* @param (string|int|bool|array<int|string, (string|int|bool|null)>)[] $sniffProperties
3033
* @param string[] $codesToCheck
34+
* @param string[] $cliArgs
3135
* @return File
3236
*/
33-
protected static function checkFile(string $filePath, array $sniffProperties = [], array $codesToCheck = []): File
37+
protected static function checkFile(string $filePath, array $sniffProperties = [], array $codesToCheck = [], array $cliArgs = []): File
3438
{
39+
if (defined('PHP_CODESNIFFER_CBF') === false) {
40+
define('PHP_CODESNIFFER_CBF', false);
41+
}
3542
$codeSniffer = new Runner();
36-
$codeSniffer->config = new Config([
37-
'-s',
38-
]);
43+
$codeSniffer->config = new Config(array_merge(['-s'], $cliArgs));
3944
$codeSniffer->init();
4045

4146
if (count($sniffProperties) > 0) {

tests/Sniffs/Files/TypeNameMatchesFileNameSniffTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,11 @@ public function testRootNamespacesNormalization(): void
8484
self::assertNoSniffErrorInFile(self::checkFile(__DIR__ . '/data/rootNamespace2/Xxx/Boo.php', $sniffProperties2));
8585
}
8686

87+
public function testWithProvidedBasePathAndNestedSameDirName(): void
88+
{
89+
self::assertNoSniffErrorInFile(self::checkFile(__DIR__ . '/data/data/Foo/Bar.php', [
90+
'rootNamespaces' => ['data' => 'Data'],
91+
], [], ['--basepath=' . __DIR__ . '/data']));
92+
}
93+
8794
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Data\Foo;
4+
5+
class Bar
6+
{
7+
}

0 commit comments

Comments
 (0)