Skip to content

Commit

Permalink
Cleanup, documentation fixes and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
coenjacobs committed Sep 19, 2024
1 parent 952d847 commit 36a61f7
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 86 deletions.
20 changes: 12 additions & 8 deletions src/Composer/Autoload/NamespaceAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
* @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
{
Expand All @@ -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, '\\') . '\\';
Expand Down
4 changes: 2 additions & 2 deletions src/Config/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
21 changes: 6 additions & 15 deletions src/Console/Commands/Compose.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down
21 changes: 11 additions & 10 deletions src/Mover.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

class Mover
{
/** @var Mozart */
protected $config;

protected Mozart $config;
protected FilesHandler $files;

/** @var array<string> */
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/PackageFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Replace/ClassmapReplacer.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
/**
* The purpose of this file is to find and update classnames (and interfaces...) in their declarations.
* Those replaced are recorded and their uses elsewhere are updated in a later step.
* The purpose of this file is to find and update classnames (and interfaces...)
* in their declarations. Those replaced are recorded and their uses elsewhere
* are updated in a later step.
*/

namespace CoenJacobs\Mozart\Replace;
Expand Down
12 changes: 3 additions & 9 deletions src/Replace/NamespaceReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@
class NamespaceReplacer extends BaseReplacer
{
/**
* The prefix to add to existing namespaces.
*
* @var string "My\Mozart\Prefix".
* The prefix to add to existing namespaces, for example: "My\Mozart\Prefix"
*/
public $depNamespace = '';
public string $depNamespace = '';

/**
* @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(string $contents, string $file = null): string
public function replace(string $contents): string
{
$searchNamespace = preg_quote($this->autoloader->getSearchNamespace(), '/');
$dependencyNamespace = preg_quote($this->depNamespace, '/');
Expand Down
3 changes: 3 additions & 0 deletions src/Replace/Replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/Replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
42 changes: 19 additions & 23 deletions tests/Console/Commands/ComposeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
24 changes: 15 additions & 9 deletions tests/MoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion tests/replacers/ClassMapReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
9 changes: 6 additions & 3 deletions tests/replacers/NamespaceReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;';

Expand Down

0 comments on commit 36a61f7

Please sign in to comment.