Skip to content

Commit 4a5acf1

Browse files
authored
[Configuration] Add ability to specify file path without extension (#6839)
* [Configuration] Add ability to specify file path without extension * fix * fix * final touch: ensure provide realpath() * final touch: rename method for grammar * final touch: ensure realpath() early
1 parent c4690eb commit 4a5acf1

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

src/Configuration/ConfigurationFactory.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,33 @@ private function resolvePaths(InputInterface $input): array
143143

144144
// give priority to command line
145145
if ($commandLinePaths !== []) {
146+
$this->setFilesWithoutExtensionParameter($commandLinePaths);
146147
return $commandLinePaths;
147148
}
148149

149150
// fallback to parameter
150-
return SimpleParameterProvider::provideArrayParameter(Option::PATHS);
151+
$configPaths = SimpleParameterProvider::provideArrayParameter(Option::PATHS);
152+
$this->setFilesWithoutExtensionParameter($configPaths);
153+
154+
return $configPaths;
155+
}
156+
157+
/**
158+
* @param string[] $paths
159+
*/
160+
private function setFilesWithoutExtensionParameter(array $paths): void
161+
{
162+
foreach ($paths as $path) {
163+
if (is_file($path) && pathinfo($path, PATHINFO_EXTENSION) === '') {
164+
$path = realpath($path);
165+
166+
if ($path === false) {
167+
continue;
168+
}
169+
170+
SimpleParameterProvider::addParameter(Option::FILES_WITHOUT_EXTENSION, $path);
171+
}
172+
}
151173
}
152174

153175
private function resolveMemoryLimit(InputInterface $input): string | null

src/Configuration/Option.php

+5
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,9 @@ final class Option
307307
* @internal The other half of ROOT_STANDALONE_REGISTERED_RULES to compare
308308
*/
309309
public const SET_REGISTERED_RULES = 'set_registered_rules';
310+
311+
/**
312+
* @internal to allow process file without extension if explicitly registered
313+
*/
314+
public const FILES_WITHOUT_EXTENSION = 'files_without_extension';
310315
}

src/FileSystem/FilesFinder.php

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public function findInDirectoriesAndFiles(
7070

7171
$filteredFilePaths = array_filter($filteredFilePaths, $fileWithSuffixFilter);
7272

73+
// add file without extension after file extension filter
74+
$filteredFilePaths = array_merge(
75+
$filteredFilePaths,
76+
SimpleParameterProvider::provideArrayParameter(Option::FILES_WITHOUT_EXTENSION)
77+
);
78+
7379
$filteredFilePaths = array_filter(
7480
$filteredFilePaths,
7581
function (string $file): bool {

tests-paths/path/NoExtensionFile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
echo 'hi';

tests/Configuration/ConfigurationFactoryTest.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ public function test(): void
1515
$configurationFactory = $this->make(ConfigurationFactory::class);
1616
$configuration = $configurationFactory->createForTests([
1717
__DIR__ . '/../../tests-paths/path/*/some_directory/*',
18+
__DIR__ . '/../../tests-paths/path/NoExtensionFile',
1819
]);
1920

2021
$filesFinder = $this->make(FilesFinder::class);
2122

2223
$filePaths = $filesFinder->findInDirectoriesAndFiles($configuration->getPaths());
23-
$this->assertCount(2, $filePaths);
24+
$this->assertCount(3, $filePaths);
2425

2526
$firstFilePath = $filePaths[0];
2627
$secondFilePath = $filePaths[1];
28+
$thirdFilePath = $filePaths[2];
2729

2830
$this->assertSame(
2931
realpath(__DIR__ . '/../../tests-paths/path/wildcard-nested/some_directory/AnotherFile.php'),
@@ -34,5 +36,10 @@ public function test(): void
3436
realpath(__DIR__ . '/../../tests-paths/path/wildcard-next/some_directory/YetAnotherFile.php'),
3537
realpath($secondFilePath),
3638
);
39+
40+
$this->assertSame(
41+
realpath(__DIR__ . '/../../tests-paths/path/NoExtensionFile'),
42+
realpath($thirdFilePath),
43+
);
3744
}
3845
}

0 commit comments

Comments
 (0)