fumeapp / modeltyper

Generate TypeScript interfaces from Laravel Models
MIT License
136 stars 16 forks source link

model:typer command tries to write abstract models #41

Closed nonetallt closed 1 year ago

nonetallt commented 1 year ago

Running the php artisan model:typer command results in the following error if your project has an abstract model class:

   TypeError 

  FumeApp\ModelTyper\Actions\RunModelShowCommand::__invoke(): Return value must be of type array, null returned

  at vendor/fumeapp/modeltyper/src/Actions/RunModelShowCommand.php:30
     26▕         if ($exitCode !== 0) {
     27▕             throw new Exception('You may need to install the doctrine/dbal package to use this command.');
     28▕         }
     29▕ 
  ➜  30▕         return json_decode(Artisan::output(), true);
     31▕     }
     32▕ }
     33▕

      +20 vendor frames 
  21  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

I ran into this issue when I added an abstract class called BaseModel that extended the eloquent model.

Suggestions: 1) Do not attempt to write abstract classes 2) Create a validation check for the show:model command output in RunShowModelCommand action to avoid confusing error messages when artisan fails.

Point number 2 is also relevant for the future testing, since Artisan::output() returns empty string in Laravel test environment unless you have set the following property in your test case class.

public $mockConsoleOutput = false;

We could do a validity check along the lines of

$output = json_decode(Artisan::output(), true);

if (empty($output)) {
    $msg = "Could not resolve types for model '$model', Artisan::output() is empty.";
    $msg .= PHP_EOL . 'If you are running tests, make sure to set {public $mockConsoleOutput = false;}';
    throw new \Exception($msg);
}
acidjazz commented 1 year ago

Thanks for reporting @nonetallt - PR's are greatly appreciated