Skip to content

Avoid triggering Symfony HTTPlug Client exception #170

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

Merged
merged 2 commits into from
Jun 14, 2020
Merged

Avoid triggering Symfony HTTPlug Client exception #170

merged 2 commits into from
Jun 14, 2020

Conversation

matt-allan
Copy link
Contributor

@matt-allan matt-allan commented May 6, 2020

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets resolves #171
Documentation N/A
License MIT

What's in this PR?

This PR prevents the following LogicException from being triggered if you attempt to use Psr18ClientDiscovery::find() when the "php-http/httplug" package is not installed.

PHP Fatal error:  Uncaught LogicException: You cannot use "Symfony\Component\HttpClient\HttplugClient" as the "php-http/httplug" package is not installed. Try running "composer require php-http/httplug". in /Users/matt/code/discovery-repro/vendor/symfony/http-client/HttplugClient.php:44
Stack trace:
#0 /Users/matt/code/discovery-repro/vendor/composer/ClassLoader.php(444): include()
#1 /Users/matt/code/discovery-repro/vendor/composer/ClassLoader.php(322): Composer\Autoload\includeFile('/Users/matt/cod...')
#2 [internal function]: Composer\Autoload\ClassLoader->loadClass('Symfony\\Compone...')
#3 [internal function]: spl_autoload_call('Symfony\\Compone...')
#4 /Users/matt/code/discovery-repro/vendor/php-http/discovery/src/Strategy/CommonClassesStrategy.php(115): is_subclass_of('Symfony\\Compone...', 'Psr\\Http\\Client...')
#5 [internal function]: Http\Discovery\Strategy\CommonClassesStrategy::getCandidates('Psr\\Http\\Client...')
#6 /Users/matt/code/discovery-repro/vendor/php-http/discovery/src/ClassDiscovery.php(56): call_user_ in /Users/matt/code/discovery-repro/vendor/symfony/http-client/HttplugClient.php on line 44

Why?

When the Symfony\Component\HttpClient\HttplugClient class is loaded Symfony checks if the Http\Client\HttpClient interface exists. If it doesn't exist an exception is thrown.

When discovering a PSR-18 client the CommonClassesStrategy calls is_subclass_of on the Symfony\Component\HttpClient\HttplugClient, which triggers the exception.

We don't need to call is_subclass_of on the Symfony Httplug client because we know it doesn't implement the PSR-18 interface, so we can just avoid calling it, thus avoiding the exception.

There might be a more elegant way to solve this. Let me know if you would prefer an alternative solution.

Checklist

  • Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix
  • Documentation pull request created (if not simply a bugfix)

@matt-allan
Copy link
Contributor Author

I tried following the contributor guide but I couldn't run the tests locally because composer install is failing with this message:

Warning: Plugin initialization failed: The "puli"/"puli.phar" command could not be found.
PHP Fatal error:  Uncaught Error: Call to a member function run() on null in /Users/matt/code/discovery/vendor/puli/composer-plugin/src/PuliPluginImpl.php:671
Stack trace:
#0 /Users/matt/code/discovery/vendor/puli/composer-plugin/src/PuliPluginImpl.php(220): Puli\ComposerPlugin\PuliPluginImpl->verifyPuliVersion()
#1 /Users/matt/code/discovery/vendor/puli/composer-plugin/src/PuliPluginImpl.php(114): Puli\ComposerPlugin\PuliPluginImpl->initialize()
#2 /Users/matt/code/discovery/vendor/puli/composer-plugin/src/PuliPlugin.php(82): Puli\ComposerPlugin\PuliPluginImpl->postAutoloadDump()
#3 [internal function]: Puli\ComposerPlugin\PuliPlugin->listen(Object(Composer\Script\Event))
#4 phar:///usr/local/Cellar/composer/1.9.1/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php(176): call_user_func(Array, Object(Composer\Script\Event))
#5 phar:///usr/local/Cellar/composer/1.9.1/bin/composer/src/Composer/EventDispatcher/EventDispatcher.php(96): Composer\EventDispatcher\EventDispatcher->doDispatch(Object(Composer\Script\Event))
#6 in /Users/matt/code/discovery/vendor/puli/composer-plugin/src/PuliPluginImpl.php on line 671

I tried to check the Puli docs but I'm getting a Server not found error. Let me know if you know how to fix the error.

@matt-allan matt-allan changed the title Avoid triggering Symfony HttpPlug Client exception Avoid triggering Symfony HTTPlug Client exception May 6, 2020
@dbu
Copy link
Contributor

dbu commented Jun 10, 2020

the alternative would be to wrap the is_subclass in a try-catch that catches everything. that would be the more generic solution, in case others decide to do similar things. wdyt, would that make sense to you?

@matt-allan
Copy link
Contributor Author

Thanks @dbu, that makes sense to me. I added a call to trigger_error like the isPsr17FactoryInstalled method so we don't completely swallow the exception.

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like the user warning level error log

@dbu dbu merged commit 838e7a0 into php-http:master Jun 14, 2020
@matt-allan matt-allan deleted the avoid-symfony-http-plug-error branch June 14, 2020 13:30
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.

Psr18ClientDiscovery with the Symfony Http Client throws a LogicException
2 participants