From 4f4e121eff7f7b6c0f0daf15939ebc75a4eae4fb Mon Sep 17 00:00:00 2001 From: PhilippeThouvenot Date: Mon, 2 Sep 2024 17:47:37 +0200 Subject: [PATCH] Resolve get class string instead of an object. (#64) * Resolve get class string instead of an object. This fix resolve a crash with phpstan or ide-helper * Reload composer before test scripts to ensure working clean slate. * Factory autoloading: use DomainFactory::resolveFactoryName() as long as it's not null. * Revert test runner execution command without composer reload. --------- Co-authored-by: Philippe Co-authored-by: Jasper Tey --- composer.json | 5 ++++- src/Support/DomainAutoloader.php | 18 +++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index c1ae6cc..6e3da31 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,10 @@ "scripts": { "post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi", "analyse": "vendor/bin/phpstan analyse", - "test": "vendor/bin/pest", + "test": [ + "@composer dump-autoload", + "vendor/bin/pest" + ], "test-coverage": "vendor/bin/pest --coverage", "format": "vendor/bin/pint", "lint": "vendor/bin/pint" diff --git a/src/Support/DomainAutoloader.php b/src/Support/DomainAutoloader.php index f107dc0..c203803 100644 --- a/src/Support/DomainAutoloader.php +++ b/src/Support/DomainAutoloader.php @@ -50,7 +50,7 @@ public function autoload(): void protected static function normalizePaths($path): array { return collect($path) - ->filter(fn ($path) => is_dir($path)) + ->filter(fn($path) => is_dir($path)) ->toArray(); } @@ -99,27 +99,27 @@ protected function handlePolicies(): void return Arr::wrap(Collection::times(count($classDirnameSegments), function ($index) use ($class, $classDirnameSegments) { $classDirname = implode('\\', array_slice($classDirnameSegments, 0, $index)); - return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; + return $classDirname . '\\Policies\\' . class_basename($class) . 'Policy'; })->reverse()->values()->first(function ($class) { return class_exists($class); - }) ?: [$classDirname.'\\Policies\\'.class_basename($class).'Policy']); + }) ?: [$classDirname . '\\Policies\\' . class_basename($class) . 'Policy']); }); } protected function handleFactories(): void { Factory::guessFactoryNamesUsing(function (string $modelName) { - if (DomainResolver::isDomainClass($modelName)) { - return DomainFactory::factoryForModel($modelName); + if ($factoryName = DomainFactory::resolveFactoryName($modelName)) { + return $factoryName; } $appNamespace = static::appNamespace(); - $modelName = Str::startsWith($modelName, $appNamespace.'Models\\') - ? Str::after($modelName, $appNamespace.'Models\\') + $modelName = Str::startsWith($modelName, $appNamespace . 'Models\\') + ? Str::after($modelName, $appNamespace . 'Models\\') : Str::after($modelName, $appNamespace); - return 'Database\\Factories\\'.$modelName.'Factory'; + return 'Database\\Factories\\' . $modelName . 'Factory'; }); } @@ -132,7 +132,7 @@ protected static function finder($paths) ->finish('/'); $ignoredFolders = collect(config('ddd.autoload_ignore', [])) - ->map(fn ($path) => Str::finish($path, '/')); + ->map(fn($path) => Str::finish($path, '/')); if ($pathAfterDomain->startsWith($ignoredFolders)) { return false;