Skip to content

Commit

Permalink
Ensure always normalize file paths for multi OSs support (#7)
Browse files Browse the repository at this point in the history
* Ensure always normalze file paths for multi OSs support

* fix phpstan
  • Loading branch information
samsonasik authored Feb 15, 2024
1 parent 00e1054 commit 4534676
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Command/FindMultiClassesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\SwissKnife\Command;

use Rector\SwissKnife\FileSystem\PathHelper;
use Rector\SwissKnife\Finder\MultipleClassInOneFileFinder;
use Rector\SwissKnife\Finder\PhpFilesFinder;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -50,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($multipleClassesByFile as $filePath => $classes) {
// get relative path to getcwd()
$relativeFilePath = str_replace(getcwd() . '/', '', $filePath);
$relativeFilePath = PathHelper::relativeToCwd($filePath);

$message = sprintf('File "%s" contains %d classes', $relativeFilePath, count($classes));
$this->symfonyStyle->section($message);
Expand Down
3 changes: 2 additions & 1 deletion src/EntityClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\SwissKnife;

use Nette\Utils\Strings;
use PhpParser\NodeTraverser;
use Rector\SwissKnife\Finder\PhpFilesFinder;
use Rector\SwissKnife\Finder\YamlFilesFinder;
Expand Down Expand Up @@ -87,7 +88,7 @@ private function resolveYamlEntityClassNames(array $paths): array

/** @var SplFileInfo $yamlFileInfo */
foreach ($yamlFileInfos as $yamlFileInfo) {
$matches = \Nette\Utils\Strings::matchAll($yamlFileInfo->getContents(), self::YAML_ENTITY_CLASS_NAME_REGEX);
$matches = Strings::matchAll($yamlFileInfo->getContents(), self::YAML_ENTITY_CLASS_NAME_REGEX);

foreach ($matches as $match) {
$yamlEntityClassNames[] = $match['class_name'];
Expand Down
9 changes: 8 additions & 1 deletion src/FileSystem/PathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@

final class PathHelper
{
private static function normalize(string $filePath): string
{
return str_replace('\\', '/', $filePath);
}

public static function relativeToCwd(string $filePath): string
{
$filePath = self::normalize($filePath);

// get relative path from getcwd()
return str_replace(getcwd() . '/', '', $filePath);
return str_replace(self::normalize((string) getcwd()) . '/', '', $filePath);
}
}
3 changes: 1 addition & 2 deletions src/Testing/Command/DetectUnitTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$filesPHPUnitXmlContents = $this->phpunitXmlPrinter->printFiles(
$unitTestCasesClassesToFilePaths,
(string) getcwd()
$unitTestCasesClassesToFilePaths
);

FileSystem::write(self::OUTPUT_FILENAME, $filesPHPUnitXmlContents);
Expand Down
9 changes: 3 additions & 6 deletions src/Testing/Printer/PHPUnitXmlPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\SwissKnife\Testing\Printer;

use Nette\Utils\Strings;
use Rector\SwissKnife\FileSystem\PathHelper;

final class PHPUnitXmlPrinter
{
Expand All @@ -13,14 +13,11 @@ final class PHPUnitXmlPrinter
*
* @param string[] $filePaths
*/
public function printFiles(array $filePaths, string $rootDirectory): string
public function printFiles(array $filePaths): string
{
$rootDirectory = realpath($rootDirectory);

$fileContents = '';
foreach ($filePaths as $filePath) {
$relativeFilePath = Strings::after($filePath, $rootDirectory . '/');

$relativeFilePath = PathHelper::relativeToCwd($filePath);
$fileContents .= '<file>' . $relativeFilePath . '</file>' . PHP_EOL;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace Rector\SwissKnife\Tests;

use Illuminate\Container\Container;
use PHPUnit\Framework\TestCase;
use Rector\SwissKnife\DependencyInjection\ContainerFactory;

abstract class AbstractTestCase extends TestCase
{
private \Illuminate\Container\Container $container;
private Container $container;

protected function setUp(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/EntityClassResolver/EntityClassResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function setUp(): void

public function test(): void
{
$entityClasses = $this->entityClassResolver->resolve([__DIR__ . '/Fixture'], function () {
$entityClasses = $this->entityClassResolver->resolve([__DIR__ . '/Fixture'], static function () : void {
});

$this->assertSame(
Expand Down

0 comments on commit 4534676

Please sign in to comment.