From 36a61f754a27bfdbd941f088c05d2ca66b32b885 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 19 Sep 2024 11:14:58 +0200 Subject: [PATCH] Cleanup, documentation fixes and formatting --- src/Composer/Autoload/NamespaceAutoloader.php | 20 +++++---- src/Config/Autoload.php | 4 +- src/Console/Commands/Compose.php | 21 +++------- src/Mover.php | 21 +++++----- src/PackageFinder.php | 5 +-- src/Replace/ClassmapReplacer.php | 5 ++- src/Replace/NamespaceReplacer.php | 12 ++---- src/Replace/Replacer.php | 3 ++ src/Replacer.php | 3 +- tests/Console/Commands/ComposeTest.php | 42 +++++++++---------- tests/MoverTest.php | 24 +++++++---- tests/replacers/ClassMapReplacerTest.php | 3 +- tests/replacers/NamespaceReplacerTest.php | 9 ++-- 13 files changed, 86 insertions(+), 86 deletions(-) diff --git a/src/Composer/Autoload/NamespaceAutoloader.php b/src/Composer/Autoload/NamespaceAutoloader.php index 5e89eb23..071b5650 100644 --- a/src/Composer/Autoload/NamespaceAutoloader.php +++ b/src/Composer/Autoload/NamespaceAutoloader.php @@ -7,24 +7,23 @@ abstract class NamespaceAutoloader extends AbstractAutoloader { - /** @var string */ - public $namespace = ''; + public string $namespace = ''; /** - * The subdir of the vendor/domain/package directory that contains the files for this autoloader type. + * The subdir of the vendor/domain/package directory that contains the files + * for this autoloader type. e.g. src/ * - * e.g. src/ - * - * @var array + * @var string[] */ public $paths = []; private FilesHandler $fileHandler; /** - * A package's composer.json config autoload key's value, where $key is `psr-1`|`psr-4`|`classmap`. + * A package's composer.json config autoload key's value, where $key is + * `psr-0`|`psr-4`|`classmap`. * - * @param $autoloadConfig + * @inheritdoc */ public function processConfig($autoloadConfig): void { @@ -38,6 +37,11 @@ public function processConfig($autoloadConfig): void array_push($this->paths, $autoloadConfig); } + public function setNamespace(string $namespace): void + { + $this->namespace = $namespace; + } + public function getNamespace(): string { return rtrim($this->namespace, '\\') . '\\'; diff --git a/src/Config/Autoload.php b/src/Config/Autoload.php index 78198e14..617519d0 100644 --- a/src/Config/Autoload.php +++ b/src/Config/Autoload.php @@ -18,7 +18,7 @@ public function setupAutoloaders(stdClass $autoloadData, Package $package): void $psr4Autoloaders = (array) $autoloadData->{'psr-4'}; foreach ($psr4Autoloaders as $key => $value) { $autoloader = new Psr4(); - $autoloader->namespace = $key; + $autoloader->setNamespace($key); $autoloader->processConfig($value); $autoloader->setPackage($package); $autoloaders[] = $autoloader; @@ -29,7 +29,7 @@ public function setupAutoloaders(stdClass $autoloadData, Package $package): void $psr0Autoloaders = (array) $autoloadData->{'psr-0'}; foreach ($psr0Autoloaders as $key => $value) { $autoloader = new Psr0(); - $autoloader->namespace = $key; + $autoloader->setNamespace($key); $autoloader->processConfig($value); $autoloader->setPackage($package); $autoloaders[] = $autoloader; diff --git a/src/Console/Commands/Compose.php b/src/Console/Commands/Compose.php index a8bf0dbd..7612719a 100644 --- a/src/Console/Commands/Compose.php +++ b/src/Console/Commands/Compose.php @@ -19,19 +19,6 @@ class Compose extends Command private Mozart $config; private string $workingDir; - public function __construct() - { - $workingDir = getcwd(); - - if (! $workingDir) { - throw new Exception('Unable to determine the working directory.'); - } - - $this->workingDir = $workingDir; - - parent::__construct(); - } - protected function configure(): void { $this->setName('compose'); @@ -44,10 +31,14 @@ protected function configure(): void */ protected function execute(InputInterface $input, OutputInterface $output): int { - if (! $this->workingDir) { - throw new Exception('Could not determine working directory.'); + $workingDir = getcwd(); + + if (! $workingDir) { + throw new Exception('Unable to determine the working directory.'); } + $this->workingDir = $workingDir; + $composerFile = $this->workingDir . DIRECTORY_SEPARATOR. 'composer.json'; try { $factory = new PackageFactory(); diff --git a/src/Mover.php b/src/Mover.php index 70b4427d..d1994c42 100644 --- a/src/Mover.php +++ b/src/Mover.php @@ -12,9 +12,7 @@ class Mover { - /** @var Mozart */ - protected $config; - + protected Mozart $config; protected FilesHandler $files; /** @var array */ @@ -30,9 +28,11 @@ public function __construct(Mozart $config) } /** - * Create the required `dep_directory` and `classmap_directory` and delete targetDirs of packages about to be moved. + * Create the required `dep_directory` and `classmap_directory` and delete + * targetDirs of packages about to be moved. * - * @param Package[] $packages The packages that, in the next step, will be moved. + * @param Package[] $packages The packages to delete the target directories + * for, which will be moved in the next step. */ public function deleteTargetDirs($packages): void { @@ -45,9 +45,8 @@ public function deleteTargetDirs($packages): void } /** - * Delete the directories about to be used for packages earmarked for Mozart namespacing. - * - * @visibility private to allow recursion through packages and subpackages. + * Delete the directories about to be used for packages earmarked for Mozart + * namespacing. */ private function deleteDepTargetDirs(Package $package): void { @@ -178,8 +177,10 @@ public function deletePackageVendorDirectories(): void $this->files->deleteDirectory($packageDir); - //Delete parent directory too if it became empty - //(because that package was the only one from that vendor) + /** + * Delete parent directory too if it became empty (because that + * package was the only one from that vendor). + */ $parentDir = dirname($packageDir); if ($this->files->isDirectoryEmpty($parentDir)) { $this->files->deleteDirectory($parentDir); diff --git a/src/PackageFinder.php b/src/PackageFinder.php index 5950cd4e..a257676e 100644 --- a/src/PackageFinder.php +++ b/src/PackageFinder.php @@ -69,11 +69,10 @@ public function getPackagesBySlugs(array $slugs): array } /** - * Loops through all dependencies and their dependencies and so on... - * will eventually return a list of all packages required by the full tree. + * Loops through all dependencies and their dependencies and so on... will + * eventually return a list of all packages required by the full tree. * * @param Package[] $packages - * * @return Package[] */ public function findPackages(array $packages): array diff --git a/src/Replace/ClassmapReplacer.php b/src/Replace/ClassmapReplacer.php index 803467fd..14a18f40 100644 --- a/src/Replace/ClassmapReplacer.php +++ b/src/Replace/ClassmapReplacer.php @@ -1,7 +1,8 @@ autoloader->getSearchNamespace(), '/'); $dependencyNamespace = preg_quote($this->depNamespace, '/'); diff --git a/src/Replace/Replacer.php b/src/Replace/Replacer.php index 907bdf9e..2266169c 100644 --- a/src/Replace/Replacer.php +++ b/src/Replace/Replacer.php @@ -7,5 +7,8 @@ interface Replacer { public function setAutoloader(Autoloader $autoloader): void; + /** + * @param string $contents The text to make replacements in. + */ public function replace(string $contents): string; } diff --git a/src/Replacer.php b/src/Replacer.php index af4d4832..ae341d60 100644 --- a/src/Replacer.php +++ b/src/Replacer.php @@ -200,7 +200,8 @@ public function replaceParentPackage(Package $package, Package $parent): void } /** - * Get an array containing all the dependencies and dependencies + * Get an array containing all the dependencies and dependencies. + * * @param Package $package * @param Package[] $dependencies * @return Package[] diff --git a/tests/Console/Commands/ComposeTest.php b/tests/Console/Commands/ComposeTest.php index 325ea3bc..6ec1d9aa 100644 --- a/tests/Console/Commands/ComposeTest.php +++ b/tests/Console/Commands/ComposeTest.php @@ -18,9 +18,8 @@ public static function setUpBeforeClass(): void } /** - * Before each test ensure the current working directory is this one. - * - * Record the previous PHPUnit cwd to restore after. + * Before each test ensure the current working directory is this one. Record + * the previous PHPUnit cwd to restore after. */ public function setUp(): void { @@ -30,9 +29,9 @@ public function setUp(): void } /** - * When composer.json is absent, instead of failing with: - * "failed to open stream: No such file or directory" - * a better message should be written to the OutputInterface. + * When composer.json is absent, instead of failing with: "failed to open + * stream: No such file or directory" a better message should be written to + * the OutputInterface. * * @test */ @@ -56,9 +55,8 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) } /** - * When json_decode fails, instead of - * "Trying to get property 'extra' of non-object" - * a better message should be written to the OutputInterface. + * When json_decode fails, instead of "Trying to get property 'extra' of + * non-object" a better message should be written to the OutputInterface. * * @test */ @@ -86,9 +84,9 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) } /** - * When composer.json->extra is absent, instead of - * "Undefined property: stdClass::$extra" - * a better message should be written to the OutputInterface. + * When composer.json->extra is absent, instead of "Undefined property: + * stdClass::$extra" a better message should be written to the + * OutputInterface. * * @test */ @@ -117,9 +115,9 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) /** - * When composer.json->extra is not an object, instead of - * "Trying to get property 'mozart' of non-object" - * a better message should be written to the OutputInterface. + * When composer.json->extra is not an object, instead of "Trying to get + * property 'mozart' of non-object" a better message should be written to + * the OutputInterface. * * @test */ @@ -147,9 +145,9 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) } /** - * When composer.json->extra->mozart is absent, instead of - * "Undefined property: stdClass::$mozart" - * a better message should be written to the OutputInterface. + * When composer.json->extra->mozart is absent, instead of "Undefined + * property: stdClass::$mozart" a better message should be written to the + * OutputInterface. * * @test */ @@ -177,11 +175,9 @@ public function __construct($inputInterfaceMock, $outputInterfaceMock) } /** - * When composer.json->extra->mozart is malformed, instead of - * "Undefined property: stdClass::$mozart" - * a better message should be written to the OutputInterface. - * - * is_object() added. + * When composer.json->extra->mozart is malformed, instead of "Undefined + * property: stdClass::$mozart" a better message should be written to the + * OutputInterface. * * @test */ diff --git a/tests/MoverTest.php b/tests/MoverTest.php index 9e6550ad..ac922d31 100644 --- a/tests/MoverTest.php +++ b/tests/MoverTest.php @@ -61,7 +61,8 @@ public function setUp(): void } /** - * If the specified `dep_directory` or `classmap_directory` are absent, create them. + * If the specified `dep_directory` or `classmap_directory` are absent, + * create them. * * @test */ @@ -81,7 +82,8 @@ public function it_creates_absent_dirs(): void } /** - * If the specified `dep_directory` or `classmap_directory` already exists with contents, it is not an issue. + * If the specified `dep_directory` or `classmap_directory` already exists + * with contents, it is not an issue. * * @test */ @@ -112,8 +114,10 @@ public function it_is_unpertrubed_by_existing_dirs(): void } /** - * If the specified `dep_directory` or `classmap_directory` contains a subdir we are going to need when moving, - * delete the subdir. aka: If subfolders exist for dependencies we are about to manage, delete those subfolders. + * If the specified `dep_directory` or `classmap_directory` contains a + * subdir we are going to need when moving, delete the subdir. aka: If + * subfolders exist for dependencies we are about to manage, delete those + * subfolders. * * @test */ @@ -155,14 +159,16 @@ public function it_deletes_subdirs_for_packages_about_to_be_moved(): void } /** - * If a file is specified more than once in an autoloader, e.g. is explicitly listed and is also in a folder listed, - * a "File already exists at path" error occurs. + * If a file is specified more than once in an autoloader, e.g. is + * explicitly listed and is also in a folder listed, a "File already exists + * at path" error occurs. * - * To fix this, we enumerate the files to be copied using a dictionary indexed with the source file path, then loop - * and copy, thus only copying each one once. + * To fix this, we list the files being moved/copied by their absolute path + * resulting in only copying each file only once. * * Original error: - * "League\Flysystem\FileExistsException : File already exists at path: lib/classes/tecnickcom/tcpdf/tcpdf.php" + * "League\Flysystem\FileExistsException : File already exists at path: + * lib/classes/tecnickcom/tcpdf/tcpdf.php" * * Test is using a known problematic autoloader: * "iio/libmergepdf": { diff --git a/tests/replacers/ClassMapReplacerTest.php b/tests/replacers/ClassMapReplacerTest.php index 6f08af06..131d2089 100644 --- a/tests/replacers/ClassMapReplacerTest.php +++ b/tests/replacers/ClassMapReplacerTest.php @@ -140,7 +140,8 @@ public function it_does_not_replace_inside_namespace_singleline(): void /** * It's possible to have multiple namespaces inside one file. * - * To have two classes in one file, one in a namespace and the other not, the global namespace needs to be explicit. + * To have two classes in one file, one in a namespace and the other not, + * the global namespace needs to be explicit. * * @test */ diff --git a/tests/replacers/NamespaceReplacerTest.php b/tests/replacers/NamespaceReplacerTest.php index 2fa41651..ea88c53e 100644 --- a/tests/replacers/NamespaceReplacerTest.php +++ b/tests/replacers/NamespaceReplacerTest.php @@ -24,7 +24,8 @@ protected function setUp(): void protected static function createReplacer(string $namespace, string $prefix = self::PREFIX) : NamespaceReplacer { $autoloader = new Psr0; - $autoloader->namespace = $namespace; + $autoloader->setNamespace($namespace); + $replacer = new NamespaceReplacer(); $replacer->setAutoloader($autoloader); $replacer->depNamespace = $prefix; @@ -83,8 +84,10 @@ public function it_doesnt_double_replace_namespaces_that_also_exist_inside_anoth $chickenReplacer = self::createReplacer('Chicken'); $eggReplacer = self::createReplacer('Egg'); - // This is a tricky situation. We are referencing Chicken\Egg, - // but Egg *also* exists as a separate top level class. + /** + * This is a tricky situation. We are referencing Chicken\Egg,but Egg + * *also* exists as a separate top level class. + */ $contents = 'use Chicken\\Egg;'; $expected = 'use My\\Mozart\\Prefix\\Chicken\\Egg;';