Skip to content

Commit

Permalink
Resolve get class string instead of an object. (#64)
Browse files Browse the repository at this point in the history
* 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 <p.thouvenot@optimumcit.com>
Co-authored-by: Jasper Tey <jasper.tey@gmail.com>
  • Loading branch information
3 people authored Sep 2, 2024
1 parent 56eeddd commit 4f4e121
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 9 additions & 9 deletions src/Support/DomainAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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';
});
}

Expand All @@ -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;
Expand Down

0 comments on commit 4f4e121

Please sign in to comment.