Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return statements in Commands/GeneratorCommand.php #1026

Merged
merged 8 commits into from
Jul 22, 2020
Merged

Add return statements in Commands/GeneratorCommand.php #1026

merged 8 commits into from
Jul 22, 2020

Conversation

rcbgalido
Copy link
Contributor

@rcbgalido rcbgalido commented Jul 21, 2020

These return statements are useful when a subclass of the GeneratorCommand class wants to execute the handle() of its parent class but wants to know if the file creation was successful or not.

@nWidart
Copy link
Owner

nWidart commented Jul 21, 2020

Hello & thank you for the contribution.

Could you also add return type hints in this case then? Thanks

@rcbgalido
Copy link
Contributor Author

Hi @nWidart , added return type hint as requested.

@nWidart
Copy link
Owner

nWidart commented Jul 21, 2020

Sorry I meant the pho 7 type hint : bool (and remove the comment version)

@rcbgalido
Copy link
Contributor Author

@nWidart It seems adding a return type hint of bool in the handle() of GenerateCommand also forces its subclasses to return a bool as well on its own corresponding implementations. I don't know if it's ideal, please advise. Thank you.

@nWidart
Copy link
Owner

nWidart commented Jul 21, 2020

Hm yea that's the cost of this change.

I'm not sure if this is worth it, as this is the first time this request has been made.
What's your use-case for this?
I understand you want to check for a successful generation, but why for example?

@rcbgalido
Copy link
Contributor Author

I was trying to make a custom command (which extends the GeneratorCommand class of laravel-modules) which is similar to Laravel's php artisan make:model -a but for modules. The -a option will create additional files related to the created model. In Laravel, the GeneratorCommand class returns a boolean indicating if the creation of the model was successful. This is beneficial since a checking could be implemented in order for the additional files not to be created if there is a failure in the creation of the model (ex. model already exists).

@nWidart
Copy link
Owner

nWidart commented Jul 21, 2020

Hm, I see that's indeed useful.
In that case, I'd say let's add the return type hint on all handle methods so that every command can be checked for its success.

@rcbgalido
Copy link
Contributor Author

If I may suggest, let's use error codes instead where 0 indicates success and E_ERROR or 1 indicates error. This is in relation to this test case in ModelMakeCommandTest:

public function it_generates_module()
{
    $code = $this->artisan('module:make', ['name' => ['Blog']]);

    $this->assertDirectoryExists($this->modulePath);
    $this->assertSame(0, $code);
}

@nWidart
Copy link
Owner

nWidart commented Jul 22, 2020

I like that idea about return codes a lot @rcbgalido ! Indeed we will be able to test for it.

@rcbgalido
Copy link
Contributor Author

@nWidart Great! Will try to implement it then.

@rcbgalido
Copy link
Contributor Author

Here are the new updates:

  1. Set all handle functions to return a code (0 for success, E_ERROR or 1 for error)
  2. Set generate function of Generators/ModuleGenerator to return a code (0 for success, E_ERROR or 1 for error)
  3. Updated test cases to include checking of code
  4. Added logic where the RouteServiceProvider file is deleted before the execution of module:route-provider for some test cases (execution of module:route-provider is unnecessary if the RouteServiceProvider file already exists).
  5. Updated snapshot for the it_can_change_the_default_namespace_specific test case since it contains the wrong namespace.

@nWidart
Copy link
Owner

nWidart commented Jul 22, 2020

Thanks! This is looking really good, very helpful! 💯

Interesting that #5 did not fail before :o

@rcbgalido
Copy link
Contributor Author

No problem! For #5, I believe it was testing the already created RouteServiceProvider file during the execution of module:make command. That's why the deletion of the file is necessary.

@nWidart nWidart merged commit 1e14248 into nWidart:master Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants