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

Arg 2 passed to Sebdesign\SM\Factory\Factory::__construct() must implement interface exception #22

Open
ziming opened this issue May 30, 2019 · 11 comments
Labels

Comments

@ziming
Copy link

ziming commented May 30, 2019

Got this error when I run composer update today. Didn't have this issue yesterday. Yet to make any new changes to my code. Using Laravel 5.8. Hope it helps!

Symfony\Component\Debug\Exception\FatalThrowableError : Argument 2 passed to Sebdesign\SM\Factory\Factory::__construct() must implement interface Symfony\Component\EventDispatcher\EventDispatcherInterface or be null, instance of Sebdesign\SM\Event\Dispatcher given

called in .../vendor/sebdesign/laravel-state-machine/src/ServiceProvider.php on line 73

@ziming ziming changed the title Arg 2 passed to Sebdesign\SM\Factory\Factory::__construct() must implement interface Symfony\Component\EventDispatcher\EventDispatcherInterface or be null Arg 2 passed to Sebdesign\SM\Factory\Factory::__construct() must implement interface exception May 30, 2019
@sebdesign sebdesign added the bug label May 30, 2019
@sebdesign
Copy link
Owner

That's strange. I installed the package (v1.4.0) to a fresh laravel project (v5.8.19), and it works fine. Could you paste the whole stack trace here? Did you try running composer dump-autoload again?

@ziming
Copy link
Author

ziming commented May 31, 2019

Okay I think I discovered what caused it. The very popular barryvdh/laravel-ide-helper

Try installing that package to your require-dev then have the following in your composer.json

"post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan ide-helper:generate",
            "php artisan ide-helper:meta",
            "php artisan ide-helper:models --nowrite"
]

Pretty sure php artisan ide-helper:meta is what causes the error. But previously it never caused an error with your package.

Now I'm not sure if you should adapt your package to barryvdh/laravel-ide-helper or barryvdh/laravel-ide-helper should have some code to handle packages like yours.

This problem only occured since yesterday if it helps

@ziming
Copy link
Author

ziming commented May 31, 2019

The stack trace of php artisan ide-helper:meta is as below

> php artisan ide-helper:meta

   Symfony\Component\Debug\Exception\FatalThrowableError  : Argument 2 passed to Sebdesign\SM\Factory\Factory::__construct() must implement interface Symfony\Component\EventDispatcher\EventDispatcherInterface or be null, instance of Sebdesign\SM\Event\Dispatcher given, called in /Users/my-username/laravel-apps/my-project/vendor/sebdesign/laravel-state-machine/src/ServiceProvider.php on line 73

  at /Users/my-username/laravel-apps/my-project/vendor/sebdesign/laravel-state-machine/src/Factory/Factory.php:19
    15|      * @var \Sebdesign\SM\Metadata\MetadataStoreInterface|null
    16|      */
    17|     protected $metadataStore;
    18| 
  > 19|     public function __construct(
    20|         array $configs,
    21|         EventDispatcherInterface $dispatcher      = null,
    22|         CallbackFactoryInterface $callbackFactory = null,
    23|         MetadataStoreInterface   $metadataStore   = null

  Exception trace:

  1   Sebdesign\SM\Factory\Factory::__construct([], Object(Sebdesign\SM\Event\Dispatcher), Object(Sebdesign\SM\Callback\ContainerAwareCallbackFactory))
      /Users/my-username/laravel-apps/my-project/vendor/sebdesign/laravel-state-machine/src/ServiceProvider.php:73

  2   Sebdesign\SM\ServiceProvider::Sebdesign\SM\{closure}(Object(Illuminate\Foundation\Application), [])
      /Users/my-username/laravel-apps/my-project/vendor/laravel/framework/src/Illuminate/Container/Container.php:787

@sebdesign
Copy link
Owner

sebdesign commented May 31, 2019

I was able to reproduce the error by installing laravel-ide-helper and running the ide-helper:meta command.

This is quite obscure, but I think it might be related to Symfony 4.3 which has released a few days ago. The interface of the event dispatcher was changed and that probably doesn't play well with laravel-ide-helper. I will investigate more with this and get back to you.

@sebdesign
Copy link
Owner

After some tinkering I found that the ide-helper:meta command throws a ReflectionException when a class cannot be loaded, and then it's catching the Exception.

Now the EventDispatcherInterface in Symfony 4.3 is trying to extend the PSR-14 interface which cannot be found, so an ReflectionException is thrown.

In this PR: barryvdh/laravel-ide-helper#779 the Throwable error is being caught instead of being rethrown as a ReflectionException which is somehow improving things.

If you also comment-out $this->registerClassAutoloadExceptions(); in the Barryvdh\LaravelIdeHelper\Console\MetaCommand everything works as expected!

@ziming
Copy link
Author

ziming commented Jun 1, 2019

So do we make a pull request over there?

@sebdesign
Copy link
Owner

I'm not sure yet, because it seems those changes were recently added and have valid purposes.

Also, it seems that in Symfony 4.3 the arguments of the dispatch method have been swapped: symfony/symfony#28920 and the old way is deprecated.

This means the winzou/state-machine package should be updated to support Symfony 4.3 and beyond.

I think I will make a PR over there and see how it will go.

By for the time being, you can manually alert the code in the ide-helper:meta command manually from your vendor if that's blocking you.

@ziming
Copy link
Author

ziming commented Jun 1, 2019

Thanks! No worries it's not blocking me I could just not call the ide-helper meta docs command or remove this package for now. Given the complaints I got from my current client on how they can't reverse a status they transit to by mistake and would rather have the freedom to set to any status. So the enforcement by a state machine has its cons as well :)

May be off topic, just curious but have you tried any of those composer patches packages before in your projects?

@sebdesign
Copy link
Owner

Yeah state machines have their drawbacks too. In that case you could leverage gates/policies to allow for the admin to change any status they want. :)

https://github.com/sebdesign/laravel-state-machine#using-gates-and-policies

I haven't look into those packages, but I tend to fork dependencies and use vcs repositories in my composer.json sometimes.

@ziming
Copy link
Author

ziming commented Jun 2, 2019

great. Thanks for the tips!

@lopezdonaque
Copy link

Hi,

I have the same error and it seams winzou/state-machine is not maintained.
Maybe a solution will be migrate to Symfony Workflow.

https://symfony.com/doc/current/components/workflow.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants