Skip to content

Commit

Permalink
Merge pull request #1 from ayrtonandino/implement-ModelInspector
Browse files Browse the repository at this point in the history
Implement model inspector
  • Loading branch information
ayrtonandino authored Nov 28, 2024
2 parents 914e9c7 + 3644f21 commit d434e4d
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 419 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
],
"require": {
"php": "^8.2",
"illuminate/support": "^11.0",
"illuminate/database": "^11.0",
"illuminate/console": "^11.0"
"illuminate/support": "^11.33.0",
"illuminate/database": "^11.33.0",
"illuminate/console": "^11.33.0"
},
"require-dev": {
"orchestra/testbench": "^9.0.0",
"phpunit/phpunit": "^11.0.0",
"orchestra/testbench": "^9.6.1",
"phpunit/phpunit": "^11.4.4",
"laravel/pint": "^1.18.3",
"larastan/larastan": "^3.0.2",
"consolidation/robo": "^5.1.0",
"phpstan/phpstan-deprecation-rules": "^2.0.0",
"totten/lurkerlite": "^1.3"
},
"conflict": {
"laravel/framework": ">=11.33.0"
"laravel/framework": "<11.33.0"
},
"autoload": {
"psr-4": {
Expand Down
170 changes: 87 additions & 83 deletions composer.lock

Large diffs are not rendered by default.

40 changes: 14 additions & 26 deletions src/Actions/BuildModelDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace FumeApp\ModelTyper\Actions;

use FumeApp\ModelTyper\Exceptions\AbstractModelException;
use FumeApp\ModelTyper\Exceptions\NestedCommandException;
use FumeApp\ModelTyper\Traits\ClassBaseName;
use FumeApp\ModelTyper\Traits\ModelRefClass;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ReflectionException;
use Symfony\Component\Finder\SplFileInfo;

Expand Down Expand Up @@ -38,23 +37,18 @@ public function __invoke(SplFileInfo $modelFile, bool $resolveAbstract = false):
$columns = collect($modelDetails['attributes'])->filter(fn ($att) => in_array($att['name'], $databaseColumns));
$nonColumns = collect($modelDetails['attributes'])->filter(fn ($att) => ! in_array($att['name'], $databaseColumns));
$relations = collect($modelDetails['relations']);
$interfaces = collect($laravelModel->interfaces)->map(fn ($interface, $key) => [

$interfaces = collect($laravelModel->interfaces ?? [])->map(fn ($interface, $key) => [
'name' => $key,
'type' => $interface['type'] ?? 'unknown',
'nullable' => $interface['nullable'] ?? false,
'import' => $interface['import'] ?? null,
'forceType' => true,
]);

$imports = $interfaces->filter(function ($interface) {
return isset($interface['import']);
})
->map(function ($interface) {
return [
'import' => $interface['import'],
'type' => $interface['type'],
];
})
$imports = $interfaces
->filter(fn (array $interface): bool => isset($interface['import']))
->map(fn (array $interface): array => ['import' => $interface['import'], 'type' => $interface['type']])
->unique()
->values();

Expand All @@ -71,28 +65,22 @@ public function __invoke(SplFileInfo $modelFile, bool $resolveAbstract = false):
'columns' => $columns,
'nonColumns' => $nonColumns,
'relations' => $relations,
'interfaces' => $interfaces->values(),
'interfaces' => $interfaces,
'imports' => $imports,
];
}

/**
* @throws NestedCommandException
* @return array{"class": class-string<\Illuminate\Database\Eloquent\Model>, database: string, table: string, policy: class-string|null, attributes: \Illuminate\Support\Collection, relations: \Illuminate\Support\Collection, events: \Illuminate\Support\Collection, observers: \Illuminate\Support\Collection, collection: class-string<\Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>>, builder: class-string<\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>>}|null
*/
private function getModelDetails(SplFileInfo $modelFile, bool $resolveAbstract): ?array
{
$modelFileArg = $modelFile->getRelativePathname();
$modelFileArg = app()->getNamespace() . $modelFileArg;
$modelFileArg = str_replace('.php', '', $modelFileArg);

try {
return app(RunModelShowCommand::class)($modelFileArg, $resolveAbstract);
} catch (NestedCommandException $exception) {
if ($exception->wasCausedBy(AbstractModelException::class) && ! $resolveAbstract) {
return null;
}
throw $exception;
}
$modelFile = Str::of(app()->getNamespace())
->append($modelFile->getRelativePathname())
->replace('.php', '')
->toString();

return app(RunModelInspector::class)($modelFile, $resolveAbstract);
}

private function overrideCollectionWithInterfaces(Collection $columns, Collection $interfaces): Collection
Expand Down
18 changes: 9 additions & 9 deletions src/Actions/DetermineAccessorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;

class DetermineAccessorType
Expand All @@ -13,26 +14,25 @@ class DetermineAccessorType
* Determine the type of accessor.
*
* @see https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor
* @see https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor
*
* @param \ReflectionClass<\Illuminate\Database\Eloquent\Model> $reflectionModel
*
* @throws Exception
*/
public function __invoke(ReflectionClass $reflectionModel, string $mutator): ReflectionMethod
{
$mutator = Str::studly($mutator);

// Try traditional
try {
$accessor = 'get' . Str::studly($mutator) . 'Attribute';

return $reflectionModel->getMethod($accessor);
} catch (Exception $e) {
return $reflectionModel->getMethod('get' . $mutator . 'Attribute');
} catch (ReflectionException $e) {
}

// Try new
try {
$method = Str::studly($mutator);

return $reflectionModel->getMethod($method);
} catch (Exception $e) {
return $reflectionModel->getMethod($mutator);
} catch (ReflectionException $e) {
}

throw new Exception('Accessor method for ' . $mutator . ' on model ' . $reflectionModel->getName() . ' does not exist');
Expand Down
27 changes: 27 additions & 0 deletions src/Actions/RunModelInspector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace FumeApp\ModelTyper\Actions;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\ModelInspector;

class RunModelInspector
{
public function __construct(protected ?Application $app = null)
{
$this->app = $app ?? app();
}

/**
* Run internal Laravel ModelInspector class.
*
* @see https://github.com/laravel/framework/blob/11.x/src/Illuminate\Database\Eloquent\ModelInspector.php
*
* @param class-string<\Illuminate\Database\Eloquent\Model> $model
* @return array{"class": class-string<\Illuminate\Database\Eloquent\Model>, database: string, table: string, policy: class-string|null, attributes: \Illuminate\Support\Collection, relations: \Illuminate\Support\Collection, events: \Illuminate\Support\Collection, observers: \Illuminate\Support\Collection, collection: class-string<\Illuminate\Database\Eloquent\Collection<\Illuminate\Database\Eloquent\Model>>, builder: class-string<\Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model>>}|null
*/
public function __invoke(string $model, bool $resolveAbstract = false): ?array
{
return app(ModelInspector::class)->inspect($model);
}
}
116 changes: 0 additions & 116 deletions src/Actions/RunModelShowCommand.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Actions/WriteColumnAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class WriteColumnAttribute
/**
* Get model columns and attributes to the output.
*
* @param \ReflectionClass<\Illuminate\Database\Eloquent\Model> $reflectionModel
* @param array{name: string, type: string, increments: bool, nullable: bool, default: mixed, unique: bool, fillable: bool, hidden?: bool, appended: mixed, cast?: string|null, forceType?: bool} $attribute
* @param array<string, string> $mappings
* @return array{array{name: string, type: string}, ReflectionClass|null}|array{string, ReflectionClass|null}|array{null, null}
Expand Down
63 changes: 0 additions & 63 deletions src/Commands/ShowModelCommand.php

This file was deleted.

14 changes: 0 additions & 14 deletions src/Exceptions/NestedCommandException.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/ModelTyperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace FumeApp\ModelTyper;

use FumeApp\ModelTyper\Commands\ModelTyperCommand;
use FumeApp\ModelTyper\Commands\ShowModelCommand;
use Illuminate\Support\ServiceProvider;

class ModelTyperServiceProvider extends ServiceProvider
Expand All @@ -20,7 +19,6 @@ public function boot(): void
if ($this->app->runningInConsole()) {
$this->commands([
ModelTyperCommand::class,
ShowModelCommand::class,
]);
}

Expand Down
Loading

0 comments on commit d434e4d

Please sign in to comment.