diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index aad21e6..2a9b762 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -22,7 +22,14 @@ class Compose extends Command public function __construct() { - $this->workingDir = getcwd(); + $workingDir = getcwd(); + + if (! $workingDir) { + throw new Exception('Unable to determine the working directory.'); + } + + $this->workingDir = $workingDir; + parent::__construct(); } diff --git a/src/Mover.php b/src/Mover.php index 4f3d65d..8b676be 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -73,7 +73,7 @@ private function deleteDepTargetDirs(Package $package): void switch ($autoloaderType) { case Psr0::class: case Psr4::class: - $outputDir = $this->config->getDepDirectory() . $packageAutoloader->namespace; + $outputDir = $this->config->getDepDirectory() . $packageAutoloader->getSearchNamespace(); $outputDir = str_replace('\\', DIRECTORY_SEPARATOR, $outputDir); $this->filesystem->deleteDirectory($outputDir); break; diff --git a/src/Replace/ClassmapReplacer.php b/src/Replace/ClassmapReplacer.php index 41323fa..39b4fa5 100644 --- a/src/Replace/ClassmapReplacer.php +++ b/src/Replace/ClassmapReplacer.php @@ -6,6 +6,8 @@ namespace CoenJacobs\Mozart\Replace; +use Exception; + class ClassmapReplacer extends BaseReplacer { @@ -15,16 +17,13 @@ class ClassmapReplacer extends BaseReplacer /** @var string */ public $classmap_prefix; - /** - * @param false|string $contents - */ - public function replace($contents): string + public function replace(string $contents): string { - if (empty($contents) || false === $contents) { + if (empty($contents)) { return ''; } - return preg_replace_callback( + $replaced = preg_replace_callback( " / # Start the pattern namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+[;{\s\n]{1}.*?(?=namespace|$) @@ -39,7 +38,7 @@ public function replace($contents): string (?:{|extends|implements|\n) # Class declaration can be followed by {, extends, # implements, or a new line /sx", // # dot matches newline, ignore whitespace in regex. - function ($matches) use ($contents) { + function ($matches) { // If we're inside a namespace other than the global namesspace, just return. if (preg_match('/^namespace\s+[a-zA-Z0-9_\x7f-\xff\\\\]+[;{\s\n]{1}.*/', $matches[0])) { @@ -53,9 +52,15 @@ function ($matches) use ($contents) { }, $contents ); + + if (empty($replaced)) { + throw new Exception('Failed to replace contents of the file.'); + } + + return $replaced; } - public function saveReplacedClass($classname, string $replacedName): void + public function saveReplacedClass(string $classname, string $replacedName): void { $this->replacedClasses[ $classname ] = $replacedName; } diff --git a/src/Replace/NamespaceReplacer.php b/src/Replace/NamespaceReplacer.php index 680fb41..b348fb2 100644 --- a/src/Replace/NamespaceReplacer.php +++ b/src/Replace/NamespaceReplacer.php @@ -2,6 +2,8 @@ namespace CoenJacobs\Mozart\Replace; +use Exception; + class NamespaceReplacer extends BaseReplacer { /** @@ -15,12 +17,12 @@ class NamespaceReplacer extends BaseReplacer * @param string $contents The text to make replacements in. * @param null $file Only used in ClassmapReplacer (for recording which files were changed). */ - public function replace($contents, $file = null): string + public function replace(string $contents, string $file = null): string { $searchNamespace = preg_quote($this->autoloader->getSearchNamespace(), '/'); $dependencyNamespace = preg_quote($this->dep_namespace, '/'); - return preg_replace_callback( + $replaced = preg_replace_callback( " / # Start the pattern ([^a-zA-Z0-9_\x7f-\xff]) # Match the non-class character before the namespace @@ -37,5 +39,11 @@ function ($matches) { }, $contents ); + + if (empty($replaced)) { + throw new Exception('Failed to replace contents of the file.'); + } + + return $replaced; } } diff --git a/src/Replace/Replacer.php b/src/Replace/Replacer.php index 33b1a15..907bdf9 100644 --- a/src/Replace/Replacer.php +++ b/src/Replace/Replacer.php @@ -6,6 +6,6 @@ interface Replacer { - public function setAutoloader(Autoloader $autoloader); - public function replace($contents); + public function setAutoloader(Autoloader $autoloader): void; + public function replace(string $contents): string; } diff --git a/src/Replacer.php b/src/Replacer.php index bc2668b..c092bcf 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -9,6 +9,7 @@ use CoenJacobs\Mozart\Config\Package; use CoenJacobs\Mozart\Replace\ClassmapReplacer; use CoenJacobs\Mozart\Replace\NamespaceReplacer; +use Exception; use League\Flysystem\Local\LocalFilesystemAdapter; use League\Flysystem\UnableToReadFile; use League\Flysystem\Filesystem; @@ -25,7 +26,7 @@ class Replacer /** @var Mozart */ protected $config; - /** @var array */ + /** @var array */ protected $replacedClasses = []; /** @var Filesystem */ @@ -48,7 +49,7 @@ public function __construct(string $workingDir, Mozart $config) /** * @param Package[] $packages */ - public function replacePackages($packages): void + public function replacePackages(array $packages): void { foreach ($packages as $package) { $this->replacePackages($package->getDependencies()); @@ -141,10 +142,6 @@ public function replaceParentClassesInDirectory(string $directory): void continue; } - if (!$contents) { - continue; - } - foreach ($replacedClasses as $original => $replacement) { $contents = preg_replace_callback( '/(.*)([^a-zA-Z0-9_\x7f-\xff])'. $original . '([^a-zA-Z0-9_\x7f-\xff])/U', @@ -156,10 +153,10 @@ function ($matches) use ($replacement) { }, $contents ); - } - if (empty($contents)) { - continue; + if (empty($contents)) { + throw new Exception('Failed to replace parent classes in directory.'); + } } $this->filesystem->write($targetFile, $contents);