Skip to content

Commit

Permalink
Fixes resulting from no phpstan baseline being used anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
coenjacobs committed Sep 12, 2024
1 parent eebcee9 commit ef3b2dc
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
9 changes: 8 additions & 1 deletion src/Console/Commands/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mover.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 13 additions & 8 deletions src/Replace/ClassmapReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace CoenJacobs\Mozart\Replace;

use Exception;

class ClassmapReplacer extends BaseReplacer
{

Expand All @@ -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|$)
Expand All @@ -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])) {
Expand All @@ -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;
}
Expand Down
12 changes: 10 additions & 2 deletions src/Replace/NamespaceReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CoenJacobs\Mozart\Replace;

use Exception;

class NamespaceReplacer extends BaseReplacer
{
/**
Expand All @@ -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
Expand All @@ -37,5 +39,11 @@ function ($matches) {
},
$contents
);

if (empty($replaced)) {
throw new Exception('Failed to replace contents of the file.');
}

return $replaced;
}
}
4 changes: 2 additions & 2 deletions src/Replace/Replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 6 additions & 9 deletions src/Replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,7 @@ class Replacer
/** @var Mozart */
protected $config;

/** @var array */
/** @var array<string,string> */
protected $replacedClasses = [];

/** @var Filesystem */
Expand All @@ -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());
Expand Down Expand Up @@ -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',
Expand All @@ -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);
Expand Down

0 comments on commit ef3b2dc

Please sign in to comment.