Skip to content

Commit

Permalink
Display the shorter relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa committed Dec 7, 2021
1 parent 9450629 commit 632d597
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 49 deletions.
18 changes: 6 additions & 12 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ private function analyze(InputInterface $input, Config $config, Broker $broker):
$broker->subscribe($report = new ResultAccumulator($config->getFilesToShow(), $config->getMinScoreToShow()));
$broker->subscribe($processFactory = $this->getProcessFactory($config));
$broker->notify(new BeforeAnalysisEvent());
$filesFinder = (new FileFinder($config->getFileExtensions(), $config->getFilesToIgnore()))
->getPhpFiles($this->getDirectoriesToScan($input, $config));
$basePath = $this->getBasePath($input, $config);
$filesFinder = (new FileFinder($config->getFileExtensions(), $config->getFilesToIgnore(), $basePath))
->getPhpFiles($config->getDirectoriesToScan());
$this->processHandlerFactory->getProcessHandler($config, $broker)->process($filesFinder, $processFactory);
$broker->notify(new AfterAnalysisEvent($report));

Expand All @@ -190,20 +191,13 @@ private function analyze(InputInterface $input, Config $config, Broker $broker):
/**
* @param InputInterface $input Input.
* @param Config $config The configuration object.
* @return array<string> Array of absolute paths.
* @return string The base path.
*/
private function getDirectoriesToScan(InputInterface $input, Config $config): array
private function getBasePath(InputInterface $input, Config $config): string
{
$basePath = [] === $input->getArgument('paths')
return [] === $input->getArgument('paths')
? $config->getDirPath()
: \getcwd();
$paths = [];

foreach ($config->getDirectoriesToScan() as $path) {
$paths[] = FileHelper::toAbsolutePath($path, $basePath);
}

return $paths;
}

/**
Expand Down
28 changes: 24 additions & 4 deletions src/File/FileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ class FileFinder
*/
private $filters;

/**
* The base path.
*
* @var string
*/
private $basePath;

/**
* Class constructor.
*
* @param array<string> $fileExtensions List of file extensions to look for.
* @param array<string> $filesToIgnore List of files to ignore.
* @param string $basePath The base path.
*/
public function __construct(array $fileExtensions, array $filesToIgnore)
public function __construct(array $fileExtensions, array $filesToIgnore, string $basePath)
{
$this->fileExtensions = $fileExtensions;
$this->filters = \array_map(function (string $fileToIgnore): string {
return $this->patternToRegex($fileToIgnore);
}, $filesToIgnore);
$this->basePath = $basePath;
}

/**
Expand All @@ -53,7 +62,9 @@ public function __construct(array $fileExtensions, array $filesToIgnore)
public function getPhpFiles(array $paths): Generator
{
foreach ($paths as $path) {
yield from $this->getPhpFilesFromPath($path);
$absolutePath = FileHelper::toAbsolutePath($path, $this->basePath);

yield from $this->getPhpFilesFromPath($absolutePath);
}
}

Expand All @@ -69,7 +80,7 @@ private function getPhpFilesFromPath(string $path): Generator
if (\is_file($path)) {
$file = new SplFileInfo($path);

yield new File($file->getRealPath(), $file->getPathName());
yield new File($file->getRealPath(), $this->getDisplayPath($file));

return;
}
Expand All @@ -80,10 +91,19 @@ private function getPhpFilesFromPath(string $path): Generator
}

foreach ($this->findPhpFiles($path) as $file) {
yield new File($file->getRealPath(), $file->getPathName());
yield new File($file->getRealPath(), $this->getDisplayPath($file));
}
}

/**
* @param SplFileInfo $file The file object.
* @return string The file path to display.
*/
private function getDisplayPath(SplFileInfo $file): string
{
return FileHelper::toRelativePath($file->getPathName(), $this->basePath);
}

/**
* Recursively finds all PHP files in a given directory.
*
Expand Down
17 changes: 17 additions & 0 deletions src/File/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Churn\File;

use InvalidArgumentException;
use Symfony\Component\Filesystem\Exception\InvalidArgumentException as FilesystemException;
use Symfony\Component\Filesystem\Filesystem;

/**
Expand All @@ -25,6 +26,22 @@ public static function toAbsolutePath(string $path, string $basePath): string
: $basePath . '/' . $path;
}

/**
* @param string $path The absolute path of an item.
* @param string $basePath The absolute base path.
* @return string The relative path of the given item.
*/
public static function toRelativePath(string $path, string $basePath): string
{
try {
$relativePath = (new Filesystem())->makePathRelative($path, $basePath);

return \rtrim($relativePath, '/\\');
} catch (FilesystemException $e) {
return $path;
}
}

/**
* Check whether the path is writable and create the missing folders if needed.
*
Expand Down
12 changes: 6 additions & 6 deletions tests/EndToEnd/FossilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function it_works_with_fossil()
( (__ ) _ ( )(__)( ) / ) ((___))___/ ) _ ( )___/
\___)(_) (_)(______)(_)\_)(_)\_) (__) (_) (_)(__)
+-------------------+---------------+------------+-------+
| File | Times Changed | Complexity | Score |
+-------------------+---------------+------------+-------+
| /tmp/test/Foo.php | 2 | 1 | 1 |
| /tmp/test/Bar.php | 1 | 1 | 0.5 |
+-------------------+---------------+------------+-------+
+---------+---------------+------------+-------+
| File | Times Changed | Complexity | Score |
+---------+---------------+------------+-------+
| Foo.php | 2 | 1 | 1 |
| Bar.php | 1 | 1 | 0.5 |
+---------+---------------+------------+-------+
";

$this->assertEquals(0, $exitCode);
Expand Down
12 changes: 6 additions & 6 deletions tests/EndToEnd/MercurialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function it_works_with_mercurial()
( (__ ) _ ( )(__)( ) / ) ((___))___/ ) _ ( )___/
\___)(_) (_)(______)(_)\_)(_)\_) (__) (_) (_)(__)
+-------------------+---------------+------------+-------+
| File | Times Changed | Complexity | Score |
+-------------------+---------------+------------+-------+
| /tmp/test/Foo.php | 2 | 1 | 1 |
| /tmp/test/Bar.php | 1 | 1 | 0.5 |
+-------------------+---------------+------------+-------+
+---------+---------------+------------+-------+
| File | Times Changed | Complexity | Score |
+---------+---------------+------------+-------+
| Foo.php | 2 | 1 | 1 |
| Bar.php | 1 | 1 | 0.5 |
+---------+---------------+------------+-------+
";

$this->assertEquals(0, $exitCode);
Expand Down
12 changes: 6 additions & 6 deletions tests/EndToEnd/SubversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function it_works_with_subversion()
( (__ ) _ ( )(__)( ) / ) ((___))___/ ) _ ( )___/
\___)(_) (_)(______)(_)\_)(_)\_) (__) (_) (_)(__)
+-------------------+---------------+------------+-------+
| File | Times Changed | Complexity | Score |
+-------------------+---------------+------------+-------+
| /tmp/test/Foo.php | 2 | 1 | 1 |
| /tmp/test/Bar.php | 1 | 1 | 0.5 |
+-------------------+---------------+------------+-------+
+---------+---------------+------------+-------+
| File | Times Changed | Complexity | Score |
+---------+---------------+------------+-------+
| Foo.php | 2 | 1 | 1 |
| Bar.php | 1 | 1 | 0.5 |
+---------+---------------+------------+-------+
";

$this->assertEquals(0, $exitCode);
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/File/FileFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public function setUp()
{
parent::setup();

$this->fileFinder = new FileFinder(['php'], []);
$this->fileFinder = new FileFinder(['php'], [], __DIR__);
}
}
24 changes: 12 additions & 12 deletions tests/Unit/File/FileFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FileFinderTest extends BaseTestCase

public function setup()
{
$this->fileFinder = new FileFinder(['php'], []);
$this->fileFinder = new FileFinder(['php'], [], __DIR__);
}

/** @test */
Expand Down Expand Up @@ -53,53 +53,53 @@ public function it_does_not_throw_with_non_existing_path()
/** @test */
public function it_ignores_files_specified_to_ignore_in_the_config()
{
$fileFinder = new FileFinder(['php'], ['Assets/Baz.php']);
$fileFinder = new FileFinder(['php'], ['Assets/Baz.php'], __DIR__);
$this->assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets']), false));
}

/** @test */
public function it_ignores_everything_within_a_folder()
{
$fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/*']);
$fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/*'], __DIR__);
$this->assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false));

$fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/*']);
$fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/*'], __DIR__);
$this->assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false));
}

/** @test */
public function it_ignores_everything_starts_with_a_string()
{
$fileFinder = new FileFinder(['php'], ['Assets2/F*']);
$fileFinder = new FileFinder(['php'], ['Assets2/F*'], __DIR__);
$this->assertCount(3, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false));

$fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/Deep*']);
$fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/Deep*'], __DIR__);
$this->assertCount(2, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false));

$fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/Dif*']);
$fileFinder = new FileFinder(['php'], ['Assets2/DeepAssets/Dif*'], __DIR__);
$this->assertCount(3, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false));
}

/** @test */
public function it_ignores_multiple_matching_patterns_in_multiple_folders()
{
$fileFinder = new FileFinder(['php'], ['Assets2/F*', 'Assets/B*']);
$fileFinder = new FileFinder(['php'], ['Assets2/F*', 'Assets/B*'], __DIR__);
$this->assertCount(4, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets', __DIR__ . '/../Assets2']), false));

$fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/De*', 'Assets/B*']);
$fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/De*', 'Assets/B*'], __DIR__);
$this->assertCount(5, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets', __DIR__ . '/../Assets2']), false));

$fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/Di*', 'Assets2/DeepAssets/De*', 'Assets2/F*']);
$fileFinder = new FileFinder(['php', 'inc'], ['Assets2/DeepAssets/Di*', 'Assets2/DeepAssets/De*', 'Assets2/F*'], __DIR__);
$this->assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false));

$fileFinder = new FileFinder(['php', 'inc'], ['Assets2/*.php']);
$fileFinder = new FileFinder(['php', 'inc'], ['Assets2/*.php'], __DIR__);
$this->assertCount(1, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets2']), false));
}

/** @test */
public function it_uses_extensions_specified_in_the_config()
{
$fileFinder = new FileFinder(['php', 'inc'], []);
$fileFinder = new FileFinder(['php', 'inc'], [], __DIR__);
$this->assertCount(4, iterator_to_array($fileFinder->getPhpFiles([__DIR__ . '/../Assets']), false));
}
}
26 changes: 24 additions & 2 deletions tests/Unit/File/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,42 @@ class FileHelperTest extends BaseTestCase

/**
* @test
* @dataProvider provide_paths
* @dataProvider provide_absolute_paths
*/
public function it_can_return_absolute_path(string $path, string $confPath, string $expectedPath): void
{
$this->assertEquals($expectedPath, FileHelper::toAbsolutePath($path, $confPath));
}

public function provide_paths(): iterable
public function provide_absolute_paths(): iterable
{
yield ['/tmp', '/path', '/tmp'];
yield ['foo', '/path', '/path/foo'];
yield ['foo', 'C:\\path', 'C:\\path/foo'];
yield ['C:\\foo', '/path', 'C:\\foo'];
yield ['d:\\foo', '/path', 'd:\\foo'];
yield ['E:/foo', '/path', 'E:/foo'];
yield ['f:/foo', '/path', 'f:/foo'];
}

/**
* @test
* @dataProvider provide_relative_paths
*/
public function it_can_return_relative_path(string $path, string $confPath, string $expectedPath): void
{
$this->assertEquals($expectedPath, FileHelper::toRelativePath($path, $confPath));
}

public function provide_relative_paths(): iterable
{
yield ['/tmp/file.php', '/tmp', 'file.php'];
yield ['/tmp/file.php', '/tmp/', 'file.php'];
yield ['file.php', '/tmp', 'file.php'];
yield ['C:/foo/file.php', 'C:/foo', 'file.php'];

if ('\\' === \DIRECTORY_SEPARATOR) {
yield ['C:\\foo\\file.php', 'C:\\foo\\', 'file.php'];
}
}
}

0 comments on commit 632d597

Please sign in to comment.