diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f5583e8a..00f7d12e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -60,3 +60,20 @@ jobs: cache-to: type=gha,mode=max - name: Run analysis run: docker compose run --rm actions-tester composer test:phpstan + mess-detect: + runs-on: ubuntu-latest + name: Mess detector + steps: + - name: Setup Docker buildx + uses: docker/setup-buildx-action@v2 + - uses: actions/checkout@v4 + - name: Build Docker image + id: build-and-push + uses: docker/build-push-action@v4 + with: + context: "{{defaultContext}}" + push: false + cache-from: type=gha + cache-to: type=gha,mode=max + - name: Run mess detector + run: docker compose run --rm actions-tester composer test:phpmd diff --git a/composer.json b/composer.json index 05f06aa5..2170597c 100644 --- a/composer.json +++ b/composer.json @@ -40,13 +40,15 @@ "symfony/console": "^5.4", "symfony/finder": "^5.4", "dealerdirect/phpcodesniffer-composer-installer": "^1.0", - "phpcompatibility/php-compatibility": "dev-develop" + "phpcompatibility/php-compatibility": "dev-develop", + "phpmd/phpmd": "^2.15" }, "scripts": { "test": [ "@test:lint", "@test:phpunit", - "@test:phpstan" + "@test:phpstan", + "@test:phpmd" ], "test:lint": [ "composer validate", @@ -57,6 +59,9 @@ ], "test:phpstan": [ "./vendor/bin/phpstan analyse -c phpstan.neon.dist --memory-limit=2G" + ], + "test:phpmd": [ + "./vendor/bin/phpmd src ansi phpmd.xml.dist" ] } } diff --git a/phpmd.xml.dist b/phpmd.xml.dist new file mode 100644 index 00000000..185b7f28 --- /dev/null +++ b/phpmd.xml.dist @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/src/Composer/Autoload/AbstractAutoloader.php b/src/Composer/Autoload/AbstractAutoloader.php new file mode 100644 index 00000000..05b8c379 --- /dev/null +++ b/src/Composer/Autoload/AbstractAutoloader.php @@ -0,0 +1,27 @@ +package; + } + + public function setPackage(Package $package): void + { + $this->package = $package; + } + + public function getOutputDir(string $basePath, string $autoloadPath): string + { + $outputDir = $basePath . $autoloadPath; + $outputDir = str_replace('\\', DIRECTORY_SEPARATOR, $outputDir); + return $outputDir; + } +} diff --git a/src/Composer/Autoload/Autoloader.php b/src/Composer/Autoload/Autoloader.php index 15f190a9..06ea8f90 100644 --- a/src/Composer/Autoload/Autoloader.php +++ b/src/Composer/Autoload/Autoloader.php @@ -2,6 +2,9 @@ namespace CoenJacobs\Mozart\Composer\Autoload; +use CoenJacobs\Mozart\FilesHandler; +use Symfony\Component\Finder\SplFileInfo; + interface Autoloader { /** @@ -9,4 +12,10 @@ interface Autoloader */ public function processConfig($autoloadConfig): void; public function getSearchNamespace(): string; + public function getOutputDir(string $basePath, string $autoloadPath): string; + /** + * @return array + */ + public function getFiles(FilesHandler $files): array; + public function getTargetFilePath(SplFileInfo $file): string; } diff --git a/src/Composer/Autoload/NamespaceAutoloader.php b/src/Composer/Autoload/NamespaceAutoloader.php index 62ef6328..5e89eb23 100644 --- a/src/Composer/Autoload/NamespaceAutoloader.php +++ b/src/Composer/Autoload/NamespaceAutoloader.php @@ -2,7 +2,10 @@ namespace CoenJacobs\Mozart\Composer\Autoload; -abstract class NamespaceAutoloader implements Autoloader +use CoenJacobs\Mozart\FilesHandler; +use Symfony\Component\Finder\SplFileInfo; + +abstract class NamespaceAutoloader extends AbstractAutoloader { /** @var string */ public $namespace = ''; @@ -16,6 +19,8 @@ abstract class NamespaceAutoloader implements Autoloader */ public $paths = []; + private FilesHandler $fileHandler; + /** * A package's composer.json config autoload key's value, where $key is `psr-1`|`psr-4`|`classmap`. * @@ -27,9 +32,10 @@ public function processConfig($autoloadConfig): void foreach ($autoloadConfig as $path) { array_push($this->paths, $path); } - } else { - array_push($this->paths, $autoloadConfig); + + return; } + array_push($this->paths, $autoloadConfig); } public function getNamespace(): string @@ -46,4 +52,50 @@ public function getNamespacePath(): string { return ''; } + + public function getFiles(FilesHandler $fileHandler): array + { + $this->fileHandler = $fileHandler; + $filesToMove = array(); + + foreach ($this->paths as $path) { + $sourcePath = $fileHandler->getConfig()->getWorkingDir() . 'vendor' . DIRECTORY_SEPARATOR + . $this->getPackage()->getName() . DIRECTORY_SEPARATOR . $path; + + $sourcePath = str_replace('/', DIRECTORY_SEPARATOR, $sourcePath); + + + $files = $fileHandler->getFilesFromPath($sourcePath); + + foreach ($files as $foundFile) { + $filePath = $foundFile->getRealPath(); + $filesToMove[ $filePath ] = $foundFile; + } + } + + return $filesToMove; + } + + public function getTargetFilePath(SplFileInfo $file): string + { + $suffix = ''; + foreach ($this->paths as $path) { + if (! empty(strstr($file->getPathname(), $this->getPackage()->getName() . DIRECTORY_SEPARATOR . $path))) { + $suffix = $path; + break; + } + } + + $replaceWith = $this->fileHandler->getConfig()->getDepDirectory() . $this->getNamespacePath(); + $targetFile = str_replace($this->fileHandler->getConfig()->getWorkingDir(), $replaceWith, $file->getPathname()); + + $packageVendorPath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $this->getPackage()->getName(); + + if (! empty($suffix)) { + $packageVendorPath = $packageVendorPath . DIRECTORY_SEPARATOR . $suffix; + } + + $packageVendorPath = str_replace('/', DIRECTORY_SEPARATOR, $packageVendorPath); + return str_replace($packageVendorPath, DIRECTORY_SEPARATOR, $targetFile); + } } diff --git a/src/Config/Autoload.php b/src/Config/Autoload.php index 55fd1aae..78198e14 100644 --- a/src/Config/Autoload.php +++ b/src/Config/Autoload.php @@ -10,7 +10,7 @@ class Autoload /** @var array */ public array $autoloaders = []; - public function setupAutoloaders(stdClass $autoloadData): void + public function setupAutoloaders(stdClass $autoloadData, Package $package): void { $autoloaders = []; @@ -20,6 +20,7 @@ public function setupAutoloaders(stdClass $autoloadData): void $autoloader = new Psr4(); $autoloader->namespace = $key; $autoloader->processConfig($value); + $autoloader->setPackage($package); $autoloaders[] = $autoloader; } } @@ -30,6 +31,7 @@ public function setupAutoloaders(stdClass $autoloadData): void $autoloader = new Psr0(); $autoloader->namespace = $key; $autoloader->processConfig($value); + $autoloader->setPackage($package); $autoloaders[] = $autoloader; } } @@ -37,6 +39,7 @@ public function setupAutoloaders(stdClass $autoloadData): void if (isset($autoloadData->classmap)) { $autoloader = new Classmap(); $autoloader->processConfig($autoloadData->classmap); + $autoloader->setPackage($package); $autoloaders[] = $autoloader; } diff --git a/src/Config/Classmap.php b/src/Config/Classmap.php index c53529ed..4788f3d2 100644 --- a/src/Config/Classmap.php +++ b/src/Config/Classmap.php @@ -2,10 +2,12 @@ namespace CoenJacobs\Mozart\Config; -use CoenJacobs\Mozart\Composer\Autoload\Autoloader; +use CoenJacobs\Mozart\Composer\Autoload\AbstractAutoloader; +use CoenJacobs\Mozart\FilesHandler; use Exception; +use Symfony\Component\Finder\SplFileInfo; -class Classmap implements Autoloader +class Classmap extends AbstractAutoloader { /** @var string[] */ public $files = []; @@ -13,6 +15,8 @@ class Classmap implements Autoloader /** @var string[] */ public $paths = []; + private FilesHandler $fileHandler; + /** * @inheritdoc */ @@ -21,9 +25,10 @@ public function processConfig($autoloadConfig): void foreach ($autoloadConfig as $value) { if ('.php' == substr($value, -4, 4)) { array_push($this->files, $value); - } else { - array_push($this->paths, $value); + continue; } + + array_push($this->paths, $value); } } @@ -34,4 +39,64 @@ public function getSearchNamespace(): string { throw new Exception('Classmap autoloaders do not contain a namespace and this method can not be used.'); } + + /** + * @return array + */ + public function getFiles(FilesHandler $fileHandler): array + { + $this->fileHandler = $fileHandler; + $filesToMove = array(); + + foreach ($this->files as $file) { + $sourcePath = $fileHandler->getConfig()->getWorkingDir() . 'vendor' + . DIRECTORY_SEPARATOR . $this->getPackage()->getName(); + + $files = $fileHandler->getFile($sourcePath, $file); + + foreach ($files as $foundFile) { + $filePath = $foundFile->getRealPath(); + $filesToMove[ $filePath ] = $foundFile; + } + } + + foreach ($this->paths as $path) { + $sourcePath = $fileHandler->getConfig()->getWorkingDir() . 'vendor' + . DIRECTORY_SEPARATOR . $this->getPackage()->getName() . DIRECTORY_SEPARATOR . $path; + + $files = $fileHandler->getFilesFromPath($sourcePath); + foreach ($files as $foundFile) { + $filePath = $foundFile->getRealPath(); + $filesToMove[ $filePath ] = $foundFile; + } + } + + return $filesToMove; + } + + public function getTargetFilePath(SplFileInfo $file): string + { + $suffix = ''; + foreach ($this->paths as $path) { + if (! empty(strstr($file->getPathname(), $this->getPackage()->getName() . DIRECTORY_SEPARATOR . $path))) { + $suffix = $path; + break; + } + } + + $namespacePath = $this->getPackage()->getName(); + $replaceWith = $this->fileHandler->getConfig()->getClassmapDirectory() . $namespacePath . DIRECTORY_SEPARATOR; + + $targetFile = str_replace($this->fileHandler->getConfig()->getWorkingDir(), $replaceWith, $file->getPathname()); + + $packageVendorPath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $this->getPackage()->getName() + . DIRECTORY_SEPARATOR; + + if (! empty($suffix)) { + $packageVendorPath = $packageVendorPath . DIRECTORY_SEPARATOR . $suffix; + } + + $packageVendorPath = str_replace('/', DIRECTORY_SEPARATOR, $packageVendorPath); + return str_replace($packageVendorPath, DIRECTORY_SEPARATOR, $targetFile); + } } diff --git a/src/Config/Mozart.php b/src/Config/Mozart.php index a07c5b5b..ac02e32f 100644 --- a/src/Config/Mozart.php +++ b/src/Config/Mozart.php @@ -11,22 +11,42 @@ class Mozart { use ReadsConfig; - public string $dep_namespace; - public string $dep_directory; - public string $classmap_directory; - public string $classmap_prefix; + public string $depNamespace; + public string $depDirectory; + public string $classmapDir; + public string $classmapPrefix; /** @var string[] */ public array $packages = []; /** @var string[] */ - public array $excluded_packages = []; + public array $excludedPackages = []; - public OverrideAutoload $override_autoload; - public bool $delete_vendor_directories; + public OverrideAutoload $overrideAutoload; + public bool $deleteVendorDir = true; public string $workingDir = ''; + public function setDepNamespace(string $depNamespace): void + { + $this->depNamespace = $depNamespace; + } + + public function setDepDirectory(string $depDirectory): void + { + $this->depDirectory = $depDirectory; + } + + public function setClassmapDirectory(string $classmapDirectory): void + { + $this->classmapDir = $classmapDirectory; + } + + public function setClassmapPrefix(string $classmapPrefix): void + { + $this->classmapPrefix = $classmapPrefix; + } + /** * @return string[] */ @@ -44,21 +64,26 @@ public function setPackages(array $packages): void } /** - * @param string[] $excluded_packages + * @param string[] $excludedPackages */ - public function setExcludedPackages(array $excluded_packages): void + public function setExcludedPackages(array $excludedPackages): void { - $this->excluded_packages = $excluded_packages; + $this->excludedPackages = $excludedPackages; } public function setOverrideAutoload(stdClass $object): void { - $this->override_autoload = new OverrideAutoload($object); + $this->overrideAutoload = new OverrideAutoload($object); + } + + public function setDeleteVendorDir(bool $deleteVendorDir): void + { + $this->deleteVendorDir = $deleteVendorDir; } public function isValidMozartConfig(): bool { - $required = [ 'dep_namespace', 'dep_directory', 'classmap_directory', 'classmap_prefix' ]; + $required = [ 'depNamespace', 'depDirectory', 'classmapDir', 'classmapPrefix' ]; foreach ($required as $requiredProp) { if (empty($this->$requiredProp)) { @@ -80,22 +105,22 @@ public function isExcludedPackage(Package $package): bool */ public function getDepDirectory(): string { - return rtrim($this->dep_directory, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; + return trim($this->depDirectory, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; } public function getClassmapDirectory(): string { - return rtrim($this->classmap_directory, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; + return trim($this->classmapDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; } public function getDeleteVendorDirectories(): bool { - return $this->delete_vendor_directories; + return $this->deleteVendorDir; } public function getDependencyNamespace(): string { - $namespace = preg_replace("/\\\{2,}$/", "\\", $this->dep_namespace."\\"); + $namespace = preg_replace("/\\\{2,}$/", "\\", $this->depNamespace."\\"); if (empty($namespace)) { throw new Exception('Could not get target dependency namespace'); @@ -106,12 +131,12 @@ public function getDependencyNamespace(): string public function getClassmapPrefix(): string { - return $this->classmap_prefix; + return $this->classmapPrefix; } public function getOverrideAutoload(): OverrideAutoload { - return $this->override_autoload; + return $this->overrideAutoload; } /** @@ -119,7 +144,7 @@ public function getOverrideAutoload(): OverrideAutoload */ public function getExcludedPackages(): array { - return $this->excluded_packages; + return $this->excludedPackages; } public function setWorkingDir(string $workingDir): void @@ -129,6 +154,6 @@ public function setWorkingDir(string $workingDir): void public function getWorkingDir(): string { - return $this->workingDir; + return rtrim($this->workingDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } } diff --git a/src/Config/Package.php b/src/Config/Package.php index c466ee9f..5e4dece2 100644 --- a/src/Config/Package.php +++ b/src/Config/Package.php @@ -25,10 +25,12 @@ class Package public ?Autoload $autoload = null; public ?Extra $extra = null; + private bool $dependenciesLoaded = false; + public function setAutoload(stdClass $data): void { $autoload = new Autoload(); - $autoload->setupAutoloaders($data); + $autoload->setupAutoloaders($data, $this); $this->autoload = $autoload; } @@ -83,9 +85,12 @@ public function getDependencies(): array return $this->dependencies; } - public function loadDependencies(): void + public function loadDependencies(PackageFinder $finder): void { - $finder = PackageFinder::instance(); + if ($this->dependenciesLoaded) { + return; + } + if ($this->isValidMozartConfig() && !empty($this->getExtra())) { $mozart = $this->getExtra()->getMozart(); @@ -98,6 +103,7 @@ public function loadDependencies(): void $dependencies = $finder->getPackagesBySlugs($this->getRequire()); $this->registerDependencies($dependencies); + $this->dependenciesLoaded = true; } public function registerDependency(Package $package): void diff --git a/src/Config/ReadsConfig.php b/src/Config/ReadsConfig.php index 4e7f3d30..22f7a83e 100644 --- a/src/Config/ReadsConfig.php +++ b/src/Config/ReadsConfig.php @@ -8,7 +8,7 @@ trait ReadsConfig { - public static function loadFromFile(string $filePath): self + public function loadFromFile(string $filePath): self { $fileContents = file_get_contents($filePath); @@ -16,13 +16,13 @@ public static function loadFromFile(string $filePath): self throw new Exception('Could not read config from provided file.'); } - return self::loadFromString($fileContents); + return $this->loadFromString($fileContents); } /** * @param array $config */ - public static function loadFromArray(array $config): self + public function loadFromArray(array $config): self { $encoded = json_encode($config); @@ -36,10 +36,10 @@ public static function loadFromArray(array $config): self throw new Exception('Could not read config from provided array.'); } - return self::loadFromStdClass($config); + return $this->loadFromStdClass($config); } - public static function loadFromStdClass(stdClass $config): self + public function loadFromStdClass(stdClass $config): self { $mapper = new JsonMapper(); $mapper->bEnforceMapType = false; @@ -52,7 +52,7 @@ public static function loadFromStdClass(stdClass $config): self return $object; } - public static function loadFromString(string $config): self + public function loadFromString(string $config): self { $config = json_decode($config); diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index 2a9b762d..a8bf0dbd 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -17,7 +17,6 @@ class Compose extends Command private Mover $mover; private Replacer $replacer; private Mozart $config; - private PackageFinder $finder; private string $workingDir; public function __construct() @@ -40,6 +39,9 @@ protected function configure(): void $this->setHelp(''); } + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ protected function execute(InputInterface $input, OutputInterface $output): int { if (! $this->workingDir) { @@ -48,7 +50,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $composerFile = $this->workingDir . DIRECTORY_SEPARATOR. 'composer.json'; try { - $package = PackageFactory::createPackage($composerFile, null, false); + $factory = new PackageFactory(); + $package = $factory->createPackage($composerFile); } catch (Exception $e) { $output->write('Unable to read the composer.json file'); return 1; @@ -74,16 +77,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int $require = $package->getRequire(); } - $this->finder = PackageFinder::instance(); - $this->finder->setConfig($this->config); + $finder = new PackageFinder(); + $finder->setConfig($this->config); - $package->loadDependencies(); + $package->loadDependencies($finder); + $packages = $finder->findPackages($package->getDependencies()); - $packages = $this->finder->getPackagesBySlugs($require); - $packages = $this->finder->findPackages($packages); - - $this->mover = new Mover($this->workingDir, $this->config); - $this->replacer = new Replacer($this->workingDir, $this->config); + $this->mover = new Mover($this->config); + $this->replacer = new Replacer($this->config); $this->mover->deleteTargetDirs($packages); $this->mover->movePackages($packages); @@ -91,6 +92,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->replacer->replaceParentInTree($packages); $this->replacer->replaceParentClassesInDirectory($this->config->getClassmapDirectory()); + if ($this->config->getDeleteVendorDirectories()) { + $this->mover->deletePackageVendorDirectories(); + } + return 0; } } diff --git a/src/FilesHandler.php b/src/FilesHandler.php new file mode 100644 index 00000000..46787b55 --- /dev/null +++ b/src/FilesHandler.php @@ -0,0 +1,81 @@ +config = $config; + + $adapter = new LocalFilesystemAdapter( + $this->config->getWorkingDir() + ); + + // The FilesystemOperator + $this->filesystem = new Filesystem($adapter); + } + + public function readFile(string $path): string + { + try { + $contents = $this->filesystem->read($path); + } catch (UnableToReadFile $e) { + $contents = ''; + } + + return $contents; + } + + public function getConfig(): Mozart + { + return $this->config; + } + + public function writeFile(string $path, string $contents): void + { + $this->filesystem->write($path, $contents); + } + + public function getFilesFromPath(string $path): Iterator + { + $finder = new Finder(); + return $finder->files()->in($path)->getIterator(); + } + + public function getFile(string $path, string $fileName): Iterator + { + $finder = new Finder(); + return $finder->files()->name($fileName)->in($path)->getIterator(); + } + + public function createDirectory(string $path): void + { + $this->filesystem->createDirectory($path); + } + + public function deleteDirectory(string $path): void + { + $this->filesystem->deleteDirectory($path); + } + + public function isDirectoryEmpty(string $path): bool + { + return count($this->filesystem->listContents($path, true)->toArray()) === 0; + } + + public function copyFile(string $origin, string $destination): void + { + $this->filesystem->copy($origin, $destination); + } +} diff --git a/src/Mover.php b/src/Mover.php index 8b676be7..70b4427d 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -3,46 +3,30 @@ namespace CoenJacobs\Mozart; use CoenJacobs\Mozart\Composer\Autoload\Autoloader; -use CoenJacobs\Mozart\Composer\Autoload\NamespaceAutoloader; use CoenJacobs\Mozart\Config\Classmap; use CoenJacobs\Mozart\Config\Mozart; use CoenJacobs\Mozart\Config\Package; use CoenJacobs\Mozart\Config\Psr0; use CoenJacobs\Mozart\Config\Psr4; -use League\Flysystem\Local\LocalFilesystemAdapter; -use League\Flysystem\Filesystem; -use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; class Mover { - /** @var string */ - protected $workingDir; - - /** @var string */ - protected $targetDir; - /** @var Mozart */ protected $config; - /** @var Filesystem */ - protected $filesystem; + protected FilesHandler $files; /** @var array */ protected $movedPackages = []; - public function __construct(string $workingDir, Mozart $config) + /** @var array */ + protected $movedFiles = []; + + public function __construct(Mozart $config) { $this->config = $config; - $this->workingDir = $workingDir; - $this->targetDir = $this->config->getDepDirectory(); - - $adapter = new LocalFilesystemAdapter( - $this->workingDir - ); - - // The FilesystemOperator - $this->filesystem = new Filesystem($adapter); + $this->files = new FilesHandler($config); } /** @@ -52,8 +36,8 @@ public function __construct(string $workingDir, Mozart $config) */ public function deleteTargetDirs($packages): void { - $this->filesystem->createDirectory($this->config->getDepDirectory()); - $this->filesystem->createDirectory($this->config->getClassmapDirectory()); + $this->files->createDirectory($this->config->getDepDirectory()); + $this->files->createDirectory($this->config->getClassmapDirectory()); foreach ($packages as $package) { $this->deleteDepTargetDirs($package); @@ -67,37 +51,47 @@ public function deleteTargetDirs($packages): void */ private function deleteDepTargetDirs(Package $package): void { - foreach ($package->getAutoloaders() as $packageAutoloader) { - $autoloaderType = get_class($packageAutoloader); + foreach ($package->getAutoloaders() as $autoloader) { + $autoloaderType = get_class($autoloader); + $outputDir = ''; switch ($autoloaderType) { case Psr0::class: case Psr4::class: - $outputDir = $this->config->getDepDirectory() . $packageAutoloader->getSearchNamespace(); - $outputDir = str_replace('\\', DIRECTORY_SEPARATOR, $outputDir); - $this->filesystem->deleteDirectory($outputDir); + $outputDir = $autoloader->getOutputDir( + $this->config->getDepDirectory(), + $autoloader->getSearchNamespace() + ); break; case Classmap::class: - $outputDir = $this->config->getClassmapDirectory() . $package->getName(); - $outputDir = str_replace('\\', DIRECTORY_SEPARATOR, $outputDir); - $this->filesystem->deleteDirectory($outputDir); + $outputDir = $autoloader->getOutputDir( + $this->config->getClassmapDirectory(), + $package->getName() + ); break; } + + if (empty($outputDir)) { + continue; + } + + $this->files->deleteDirectory($outputDir); } + foreach ($package->getDependencies() as $subPackage) { $this->deleteDepTargetDirs($subPackage); } } - public function deleteEmptyDirs(): void + private function deleteEmptyDirs(): void { - if (count($this->filesystem->listContents($this->config->getDepDirectory(), true)->toArray()) === 0) { - $this->filesystem->deleteDirectory($this->config->getDepDirectory()); + if ($this->files->isDirectoryEmpty($this->config->getDepDirectory())) { + $this->files->deleteDirectory($this->config->getDepDirectory()); } - if (count($this->filesystem->listContents($this->config->getClassmapDirectory(), true)->toArray()) === 0) { - $this->filesystem->deleteDirectory($this->config->getClassmapDirectory()); + if ($this->files->isDirectoryEmpty($this->config->getClassmapDirectory())) { + $this->files->deleteDirectory($this->config->getClassmapDirectory()); } } @@ -107,112 +101,66 @@ public function deleteEmptyDirs(): void public function movePackages($packages): void { foreach ($packages as $package) { - $this->movePackages($package->getDependencies()); $this->movePackage($package); } $this->deleteEmptyDirs(); } - public function movePackage(Package $package): void + private function movePackage(Package $package): void { - if (in_array($package->getName(), $this->movedPackages)) { - return; - } - - if ($this->config->isExcludedPackage($package)) { + if (!$this->shouldPackageBeMoved($package)) { return; } + /** + * @todo: This maybe even warrants its own 'File' class, where stuff + * like the SplFileInfo etc can be stored in. + */ foreach ($package->getAutoloaders() as $autoloader) { - if ($autoloader instanceof NamespaceAutoloader) { - $finder = new Finder(); - - foreach ($autoloader->paths as $path) { - $source_path = $this->workingDir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR - . $package->getName() . DIRECTORY_SEPARATOR . $path; - - $source_path = str_replace('/', DIRECTORY_SEPARATOR, $source_path); - - $finder->files()->in($source_path); - - foreach ($finder as $file) { - $this->moveFile($package, $autoloader, $file, $path); - } - } - } elseif ($autoloader instanceof Classmap) { - $finder = new Finder(); - - $files_to_move = array(); - - foreach ($autoloader->files as $file) { - $source_path = $this->workingDir . DIRECTORY_SEPARATOR . 'vendor' - . DIRECTORY_SEPARATOR . $package->getName(); - $finder->files()->name($file)->in($source_path); + $filesToMove = $autoloader->getFiles($this->files); - foreach ($finder as $foundFile) { - $filePath = $foundFile->getRealPath(); - $files_to_move[ $filePath ] = $foundFile; - } - } - - $finder = new Finder(); - - foreach ($autoloader->paths as $path) { - $source_path = $this->workingDir . DIRECTORY_SEPARATOR . 'vendor' - . DIRECTORY_SEPARATOR . $package->getName() . DIRECTORY_SEPARATOR . $path; - - $finder->files()->in($source_path); - - foreach ($finder as $foundFile) { - $filePath = $foundFile->getRealPath(); - $files_to_move[ $filePath ] = $foundFile; - } - } - - foreach ($files_to_move as $foundFile) { - $this->moveFile($package, $autoloader, $foundFile); - } + foreach ($filesToMove as $foundFile) { + $this->moveFile($autoloader, $foundFile); } + } - if (!in_array($package->getName(), $this->movedPackages)) { - $this->movedPackages[] = $package->getName(); - } + if (!in_array($package->getName(), $this->movedPackages)) { + $this->movedPackages[] = $package->getName(); } + } - if ($this->config->getDeleteVendorDirectories()) { - $this->deletePackageVendorDirectories(); + private function shouldPackageBeMoved(Package $package): bool + { + if (in_array($package->getName(), $this->movedPackages)) { + return false; } + + if ($this->config->isExcludedPackage($package)) { + return false; + } + + return true; } - public function moveFile(Package $package, Autoloader $autoloader, SplFileInfo $file, string $path = ''): string + private function moveFile(Autoloader $autoloader, SplFileInfo $file): void { - if ($autoloader instanceof NamespaceAutoloader) { - $namespacePath = $autoloader->getNamespacePath(); - $replaceWith = $this->config->getDepDirectory() . $namespacePath; - $targetFile = str_replace($this->workingDir, $replaceWith, $file->getPathname()); - - $packageVendorPath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package->getName() - . DIRECTORY_SEPARATOR . $path; - $packageVendorPath = str_replace('/', DIRECTORY_SEPARATOR, $packageVendorPath); - $targetFile = str_replace($packageVendorPath, '', $targetFile); - } else { - $namespacePath = $package->getName(); - $replaceWith = $this->config->getClassmapDirectory() . $namespacePath; - $targetFile = str_replace($this->workingDir, $replaceWith, $file->getPathname()); - - $packageVendorPath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . $package->getName() - . DIRECTORY_SEPARATOR; - $packageVendorPath = str_replace('/', DIRECTORY_SEPARATOR, $packageVendorPath); - $targetFile = str_replace($packageVendorPath, DIRECTORY_SEPARATOR, $targetFile); + if (in_array($file->getRealPath(), $this->movedFiles)) { + return; } - $this->filesystem->copy( - str_replace($this->workingDir, '', $file->getPathname()), + $targetFile = $autoloader->getTargetFilePath($file); + $this->copyFile($file, $targetFile); + + array_push($this->movedFiles, $file->getRealPath()); + } + + private function copyFile(SplFileInfo $file, string $targetFile): void + { + $this->files->copyFile( + str_replace($this->config->getWorkingDir(), '', $file->getPathname()), $targetFile ); - - return $targetFile; } /** @@ -220,7 +168,7 @@ public function moveFile(Package $package, Autoloader $autoloader, SplFileInfo $ * prevent packages that are prefixed/namespaced from being used or * influencing the output of the code. They just need to be gone. */ - protected function deletePackageVendorDirectories(): void + public function deletePackageVendorDirectories(): void { foreach ($this->movedPackages as $movedPackage) { $packageDir = 'vendor' . DIRECTORY_SEPARATOR . $movedPackage; @@ -228,20 +176,14 @@ protected function deletePackageVendorDirectories(): void continue; } - $this->filesystem->deleteDirectory($packageDir); + $this->files->deleteDirectory($packageDir); //Delete parent directory too if it became empty //(because that package was the only one from that vendor) $parentDir = dirname($packageDir); - if ($this->dirIsEmpty($parentDir)) { - $this->filesystem->deleteDirectory($parentDir); + if ($this->files->isDirectoryEmpty($parentDir)) { + $this->files->deleteDirectory($parentDir); } } } - - private function dirIsEmpty(string $dir): bool - { - $di = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS); - return iterator_count($di) === 0; - } } diff --git a/src/PackageFactory.php b/src/PackageFactory.php index da67d61e..4591181a 100644 --- a/src/PackageFactory.php +++ b/src/PackageFactory.php @@ -8,28 +8,22 @@ class PackageFactory { /** @var array */ - public static array $cache = []; + public array $cache = []; - public static function createPackage( - string $path, - stdClass $overrideAutoload = null, - bool $loadDependencies = true - ): Package { - if (isset(self::$cache[$path])) { - return self::$cache[$path]; + public function createPackage(string $path, stdClass $overrideAutoload = null): Package + { + if (isset($this->cache[$path])) { + return $this->cache[$path]; } - $package = Package::loadFromFile($path); + $package = new Package(); + $package = $package->loadFromFile($path); if (! empty($overrideAutoload)) { $package->setAutoload($overrideAutoload); } - if ($loadDependencies) { - $package->loadDependencies(); - } - - self::$cache[$path] = $package; + $this->cache[$path] = $package; return $package; } } diff --git a/src/PackageFinder.php b/src/PackageFinder.php index 9f162c63..5950cd4e 100644 --- a/src/PackageFinder.php +++ b/src/PackageFinder.php @@ -9,16 +9,11 @@ class PackageFinder { private ?Mozart $config; + public PackageFactory $factory; - public static function instance(): self + public function __construct() { - static $instance; - - if (! is_object($instance) || ! $instance instanceof self) { - $instance = new self(); - } - - return $instance; + $this->factory = new PackageFactory(); } public function setConfig(Mozart $config): void @@ -48,12 +43,14 @@ public function getPackageBySlug(string $slug): ?Package } $autoloaders = null; - $override_autoload = $this->config->getOverrideAutoload(); - if ($override_autoload !== false && isset($override_autoload->$slug)) { - $autoloaders = $override_autoload->$slug; + $overrideAutoload = $this->config->getOverrideAutoload(); + if ($overrideAutoload !== false && isset($overrideAutoload->$slug)) { + $autoloaders = $overrideAutoload->$slug; } - return PackageFactory::createPackage($packageDir . 'composer.json', $autoloaders, true); + $package = $this->factory->createPackage($packageDir . 'composer.json', $autoloaders); + $package->loadDependencies($this); + return $package; } /** @@ -84,8 +81,9 @@ public function findPackages(array $packages): array foreach ($packages as $package) { $dependencies = $package->getDependencies(); - $package->registerDependencies($this->findPackages($dependencies)); - $packages[$package->getName()] = $package; + if (! empty($dependencies)) { + $packages = array_merge($packages, $this->findPackages($dependencies)); + } } return $packages; diff --git a/src/Replace/ClassmapReplacer.php b/src/Replace/ClassmapReplacer.php index 39b4fa52..803467fd 100644 --- a/src/Replace/ClassmapReplacer.php +++ b/src/Replace/ClassmapReplacer.php @@ -15,7 +15,7 @@ class ClassmapReplacer extends BaseReplacer public $replacedClasses = []; /** @var string */ - public $classmap_prefix; + public $classmapPrefix; public function replace(string $contents): string { @@ -46,7 +46,7 @@ function ($matches) { } // The prepended class name. - $replace = $this->classmap_prefix . $matches[1]; + $replace = $this->classmapPrefix . $matches[1]; $this->saveReplacedClass($matches[1], $replace); return str_replace($matches[1], $replace, $matches[0]); }, diff --git a/src/Replace/NamespaceReplacer.php b/src/Replace/NamespaceReplacer.php index b348fb24..9c324c5e 100644 --- a/src/Replace/NamespaceReplacer.php +++ b/src/Replace/NamespaceReplacer.php @@ -11,7 +11,7 @@ class NamespaceReplacer extends BaseReplacer * * @var string "My\Mozart\Prefix". */ - public $dep_namespace = ''; + public $depNamespace = ''; /** * @param string $contents The text to make replacements in. @@ -20,7 +20,7 @@ class NamespaceReplacer extends BaseReplacer public function replace(string $contents, string $file = null): string { $searchNamespace = preg_quote($this->autoloader->getSearchNamespace(), '/'); - $dependencyNamespace = preg_quote($this->dep_namespace, '/'); + $dependencyNamespace = preg_quote($this->depNamespace, '/'); $replaced = preg_replace_callback( " @@ -35,7 +35,7 @@ public function replace(string $contents, string $file = null): string ) # End the namespace matcher /Ux", function ($matches) { - return $matches[1] . $this->dep_namespace . $matches[2]; + return $matches[1] . $this->depNamespace . $matches[2]; }, $contents ); diff --git a/src/Replacer.php b/src/Replacer.php index c092bcf9..af4d4832 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -9,41 +9,24 @@ use CoenJacobs\Mozart\Config\Package; use CoenJacobs\Mozart\Replace\ClassmapReplacer; use CoenJacobs\Mozart\Replace\NamespaceReplacer; +use CoenJacobs\Mozart\Replace\Replacer as ReplacerInterface; use Exception; -use League\Flysystem\Local\LocalFilesystemAdapter; -use League\Flysystem\UnableToReadFile; -use League\Flysystem\Filesystem; -use Symfony\Component\Finder\Finder; class Replacer { - /** @var string */ - protected $workingDir; - - /** @var string */ - protected $targetDir; - /** @var Mozart */ protected $config; /** @var array */ protected $replacedClasses = []; - /** @var Filesystem */ - protected $filesystem; + /** @var FilesHandler */ + protected $files; - public function __construct(string $workingDir, Mozart $config) + public function __construct(Mozart $config) { - $this->workingDir = $workingDir; $this->config = $config; - $this->targetDir = $this->config->getDepDirectory(); - - $adapter = new LocalFilesystemAdapter( - $this->workingDir - ); - - // The FilesystemOperator - $this->filesystem = new Filesystem($adapter); + $this->files = new FilesHandler($config); } /** @@ -66,33 +49,36 @@ public function replacePackage(Package $package): void public function replaceInFile(string $targetFile, Autoloader $autoloader): void { - $targetFile = str_replace($this->workingDir, '', $targetFile); - try { - $contents = $this->filesystem->read($targetFile); - } catch (UnableToReadFile $e) { - return; - } + $targetFile = str_replace($this->config->getWorkingDir(), '', $targetFile); + $contents = $this->files->readFile($targetFile); if (!$contents) { return; } - if ($autoloader instanceof NamespaceAutoloader) { - $replacer = new NamespaceReplacer(); - $replacer->dep_namespace = $this->config->getDependencyNamespace(); - } else { - $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = $this->config->getClassmapPrefix(); - } - - $replacer->setAutoloader($autoloader); + $replacer = $this->getReplacerByAutoloader($autoloader); $contents = $replacer->replace($contents); if ($replacer instanceof ClassmapReplacer) { $this->replacedClasses = array_merge($this->replacedClasses, $replacer->replacedClasses); } - $this->filesystem->write($targetFile, $contents); + $this->files->writeFile($targetFile, $contents); + } + + public function getReplacerByAutoloader(Autoloader $autoloader): ReplacerInterface + { + if ($autoloader instanceof NamespaceAutoloader) { + $replacer = new NamespaceReplacer(); + $replacer->depNamespace = $this->config->getDependencyNamespace(); + $replacer->setAutoloader($autoloader); + return $replacer; + } + + $replacer = new ClassmapReplacer(); + $replacer->classmapPrefix = $this->config->getClassmapPrefix(); + $replacer->setAutoloader($autoloader); + return $replacer; } public function replacePackageByAutoloader(Package $package, Autoloader $autoloader): void @@ -102,15 +88,14 @@ public function replacePackageByAutoloader(Package $package, Autoloader $autoloa } if ($autoloader instanceof NamespaceAutoloader) { - $source_path = $this->workingDir . $this->targetDir + $sourcePath = $this->config->getWorkingDir() . $this->config->getDepDirectory() . str_replace('\\', DIRECTORY_SEPARATOR, $autoloader->getNamespace()); - $this->replaceInDirectory($autoloader, $source_path); + $this->replaceInDirectory($autoloader, $sourcePath); } elseif ($autoloader instanceof Classmap) { - $finder = new Finder(); - $source_path = $this->workingDir . $this->config->getClassmapDirectory() . $package->getName(); - $finder->files()->in($source_path); + $sourcePath = $this->config->getWorkingDir() . $this->config->getClassmapDirectory() . $package->getName(); + $files = $this->files->getFilesFromPath($sourcePath); - foreach ($finder as $foundFile) { + foreach ($files as $foundFile) { $targetFile = $foundFile->getRealPath(); if ('.php' == substr($targetFile, -4, 4)) { @@ -127,26 +112,21 @@ public function replaceParentClassesInDirectory(string $directory): void } $directory = trim($directory, '//'); - $finder = new Finder(); - $finder->files()->in($directory); + $files = $this->files->getFilesFromPath($directory); $replacedClasses = $this->replacedClasses; - foreach ($finder as $file) { + foreach ($files as $file) { $targetFile = $file->getPathName(); if ('.php' == substr($targetFile, -4, 4)) { - try { - $contents = $this->filesystem->read($targetFile); - } catch (UnableToReadFile $e) { - continue; - } + $contents = $this->files->readFile($targetFile); foreach ($replacedClasses as $original => $replacement) { $contents = preg_replace_callback( '/(.*)([^a-zA-Z0-9_\x7f-\xff])'. $original . '([^a-zA-Z0-9_\x7f-\xff])/U', function ($matches) use ($replacement) { - if (preg_match('/(include|require)/', $matches[0], $output_array)) { + if (preg_match('/(include|require)/', $matches[0])) { return $matches[0]; } return $matches[1] . $matches[2] . $replacement . $matches[3]; @@ -159,17 +139,16 @@ function ($matches) use ($replacement) { } } - $this->filesystem->write($targetFile, $contents); + $this->files->writeFile($targetFile, $contents); } } } public function replaceInDirectory(NamespaceAutoloader $autoloader, string $directory): void { - $finder = new Finder(); - $finder->files()->in($directory); + $files = $this->files->getFilesFromPath($directory); - foreach ($finder as $file) { + foreach ($files as $file) { $targetFile = $file->getPathName(); if ('.php' == substr($targetFile, -4, 4)) { @@ -193,26 +172,29 @@ public function replaceParentPackage(Package $package, Package $parent): void foreach ($package->getAutoloaders() as $autoloader) { if ($parentAutoloader instanceof NamespaceAutoloader) { $namespace = str_replace('\\', DIRECTORY_SEPARATOR, $parentAutoloader->namespace); - $directory = $this->workingDir . $this->config->getDepDirectory() . $namespace + $directory = $this->config->getWorkingDir() . $this->config->getDepDirectory() . $namespace . DIRECTORY_SEPARATOR; if ($autoloader instanceof NamespaceAutoloader) { $this->replaceInDirectory($autoloader, $directory); - } else { - $directory = str_replace($this->workingDir, '', $directory); - $this->replaceParentClassesInDirectory($directory); + return; } - } else { - $directory = $this->workingDir . - $this->config->getClassmapDirectory() . $parent->getName(); - if ($autoloader instanceof NamespaceAutoloader) { - $this->replaceInDirectory($autoloader, $directory); - } else { - $directory = str_replace($this->workingDir, '', $directory); - $this->replaceParentClassesInDirectory($directory); - } + $directory = str_replace($this->config->getWorkingDir(), '', $directory); + $this->replaceParentClassesInDirectory($directory); + return; + } + + $directory = $this->config->getWorkingDir() . + $this->config->getClassmapDirectory() . $parent->getName(); + + if ($autoloader instanceof NamespaceAutoloader) { + $this->replaceInDirectory($autoloader, $directory); + return; } + + $directory = str_replace($this->config->getWorkingDir(), '', $directory); + $this->replaceParentClassesInDirectory($directory); } } } diff --git a/tests/Config/ConfigMapperTest.php b/tests/Config/ConfigMapperTest.php index 42e1ed7a..46d5553f 100644 --- a/tests/Config/ConfigMapperTest.php +++ b/tests/Config/ConfigMapperTest.php @@ -5,6 +5,7 @@ use CoenJacobs\Mozart\Config\Mozart; use CoenJacobs\Mozart\Config\Package; use CoenJacobs\Mozart\PackageFactory; +use CoenJacobs\Mozart\PackageFinder; use PHPUnit\Framework\TestCase; class ConfigMapperTest extends TestCase @@ -15,7 +16,10 @@ class ConfigMapperTest extends TestCase #[Test] public function it_creates_a_valid_config_object_based_on_composer_file() { - $package = PackageFactory::createPackage(__DIR__ . '/config-mapper-test.json'); + $finder = new PackageFinder(); + $factory = new PackageFactory(); + $package = $factory->createPackage(__DIR__ . '/config-mapper-test.json'); + $package->loadDependencies($finder); $this->assertInstanceOf(Package::class, $package); $this->assertInstanceOf(Mozart::class, $package->getExtra()->getMozart()); $this->assertCount(4, $package->autoload->getAutoloaders()); diff --git a/tests/MoverTest.php b/tests/MoverTest.php index 8945f49e..9e6550ad 100644 --- a/tests/MoverTest.php +++ b/tests/MoverTest.php @@ -5,6 +5,7 @@ use CoenJacobs\Mozart\PackageFactory; use CoenJacobs\Mozart\Console\Commands\Compose; use CoenJacobs\Mozart\Mover; +use CoenJacobs\Mozart\PackageFinder; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\Attributes\Test; use Symfony\Component\Console\Input\InputInterface; @@ -54,7 +55,9 @@ public function setUp(): void ), ); - $this->config = Mozart::loadFromString( json_encode($configArgs) ); + $mozart = new Mozart(); + $this->config = $mozart->loadFromString( json_encode($configArgs) ); + $this->config->setWorkingDir($this->testsWorkingDir); } /** @@ -65,7 +68,7 @@ public function setUp(): void #[Test] public function it_creates_absent_dirs(): void { - $mover = new Mover($this->testsWorkingDir, $this->config); + $mover = new Mover($this->config); $packages = array(); @@ -85,7 +88,7 @@ public function it_creates_absent_dirs(): void #[Test] public function it_is_unpertrubed_by_existing_dirs(): void { - $mover = new Mover($this->testsWorkingDir, $this->config); + $mover = new Mover($this->config); if (!file_exists($this->testsWorkingDir . $this->config->getDepDirectory())) { mkdir($this->testsWorkingDir . $this->config->getDepDirectory()); @@ -137,11 +140,14 @@ public function it_deletes_subdirs_for_packages_about_to_be_moved(): void if ( ! empty( $overrideAutoload ) ) { $overrideAutoload = $overrideAutoload->getByKey( $packageString ); } - $parsedPackage = PackageFactory::createPackage($testDummyComposerPath, $overrideAutoload); + $factory = new PackageFactory(); + $finder = new PackageFinder(); + $parsedPackage = $factory->createPackage($testDummyComposerPath, $overrideAutoload); + $parsedPackage->loadDependencies($finder); $packages[] = $parsedPackage; } - $mover = new Mover($this->testsWorkingDir, $this->config); + $mover = new Mover($this->config); $mover->deleteTargetDirs($packages); $this->assertDirectoryDoesNotExist($this->testsWorkingDir . $this->config->getDepDirectory() . 'Pimple'); diff --git a/tests/replacers/ClassMapReplacerTest.php b/tests/replacers/ClassMapReplacerTest.php index 10b10ce8..6f08af06 100644 --- a/tests/replacers/ClassMapReplacerTest.php +++ b/tests/replacers/ClassMapReplacerTest.php @@ -13,7 +13,7 @@ public function it_replaces_class_declarations(): void { $contents = 'class Hello_World {'; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $contents = $replacer->replace($contents); $this->assertEquals('class Mozart_Hello_World {', $contents); } @@ -24,7 +24,7 @@ public function it_replaces_abstract_class_declarations(): void { $contents = 'abstract class Hello_World {'; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $contents = $replacer->replace($contents); $this->assertEquals('abstract class Mozart_Hello_World {', $contents); } @@ -35,7 +35,7 @@ public function it_replaces_interface_class_declarations(): void { $contents = 'interface Hello_World {'; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $contents = $replacer->replace($contents); $this->assertEquals('interface Mozart_Hello_World {', $contents); } @@ -46,7 +46,7 @@ public function it_replaces_class_declarations_that_extend_other_classes(): void { $contents = 'class Hello_World extends Bye_World {'; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $contents = $replacer->replace($contents); $this->assertEquals('class Mozart_Hello_World extends Bye_World {', $contents); } @@ -57,7 +57,7 @@ public function it_replaces_class_declarations_that_implement_interfaces(): void { $contents = 'class Hello_World implements Bye_World {'; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $contents = $replacer->replace($contents); $this->assertEquals('class Mozart_Hello_World implements Bye_World {', $contents); } @@ -68,7 +68,7 @@ public function it_stores_replaced_class_names(): void { $contents = 'class Hello_World {'; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $replacer->replace($contents); $this->assertArrayHasKey('Hello_World', $replacer->replacedClasses); } @@ -79,7 +79,7 @@ public function it_replaces_class_declarations_psr2(): void { $contents = "class Hello_World\n{"; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $contents = $replacer->replace($contents); $this->assertEquals("class Mozart_Hello_World\n{", $contents); } @@ -94,7 +94,7 @@ public function it_replaces_class(): void { $contents = "class Hello_World"; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $contents = $replacer->replace($contents); $this->assertEquals("class Mozart_Hello_World", $contents); } @@ -114,7 +114,7 @@ public function it_does_not_replace_inside_namespace_multiline(): void class Hello_World "; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $result = $replacer->replace($input); $this->assertEquals($input, $result); @@ -131,7 +131,7 @@ public function it_does_not_replace_inside_namespace_singleline(): void { $input = "namespace Mozart; class Hello_World"; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $result = $replacer->replace($input); $this->assertEquals($input, $result); @@ -158,7 +158,7 @@ class B_Class { } "; $replacer = new ClassmapReplacer(); - $replacer->classmap_prefix = 'Mozart_'; + $replacer->classmapPrefix = 'Mozart_'; $result = $replacer->replace($input); $this->assertStringNotContainsString('Mozart_A_Class', $result); diff --git a/tests/replacers/NamespaceReplacerTest.php b/tests/replacers/NamespaceReplacerTest.php index b0651bc7..2fa41651 100644 --- a/tests/replacers/NamespaceReplacerTest.php +++ b/tests/replacers/NamespaceReplacerTest.php @@ -27,7 +27,7 @@ protected static function createReplacer(string $namespace, string $prefix = sel $autoloader->namespace = $namespace; $replacer = new NamespaceReplacer(); $replacer->setAutoloader($autoloader); - $replacer->dep_namespace = $prefix; + $replacer->depNamespace = $prefix; return $replacer; }