WildPHP / irc-bot

A simple and modular PHP IRC bot
MIT License
84 stars 24 forks source link

Fatal error: Argument 1 passed to WildPHP\Core\Connection\ConnectionDetails::fromArray() must be of the type array, object given #160

Closed ivptr closed 3 years ago

ivptr commented 3 years ago

When trying to run bot getting:

Fatal error: Uncaught TypeError: Argument 1 passed to WildPHP\Core\Connection\ConnectionDetails::fromArray() must be of the type array, object given, called in app\container_configuration.php on line 87 and defined in src\Connection\ConnectionDetails.php:181
Stack trace:
#0 app\container_configuration.php(87): WildPHP\Core\Connection\ConnectionDetails::fromArray(Object(WildPHP\Core\Configuration\Configuration))
#1 [internal function]: DI\Definition\Source\DefinitionFile::{closure}(Object(WildPHP\Core\Events\EventEmitter), Object(Monolog\Logger), Object(DI\Container))
#2 vendor\php-di\invoker\src\Invoker.php(75): call_user_func_array(Object(Closure), Array)
#3 vendor\php-di\php-di\src\Definition\Resolver\FactoryResolver.php(80): Invoker\Invoker->call(Object(Closure), Array)
#4 vendor\php-di\php-di\src\Definition\Resolver\ResolverDispatcher.php(71): DI\Definition\Resolver\FactoryResolver->resolve(Object(DI in src\Modules\ModuleFactory.php on line 73

I see the latest commit has changes regarding that function: https://github.com/WildPHP/irc-bot/commit/f5f6dd01787328774bfe7546f62a7cf79dad83e2#diff-09f77239c63a31f8dbdd10fbda706cd8a7063cfa3449e257e2d11854ed59de05

NanoSector commented 3 years ago

https://github.com/WildPHP/irc-bot/commit/252437e752d5bc5bbed713544e433ecc76671e66#diff-5d29dfaba05c0cd6c91cd1b0917371511eed89bf36a8001c34f5f0b287d0a923

That makes sense as PHP won't cast the Configuration object to an array, as it is a collection which in turn is an ArrayObject, but that does not mean it is an array.

Configuration can be cast to an array though which is the original approach that I removed. I don't know why this error hasn't triggered for me but this definitely needs fixing regardless. Either it should be cast to an array or the getArrayCopy method should be used.

It'd be interesting to look into the side effects and performance of either option. Maybe it is a better idea to add a getAsArray method to the Configuration system to avoid casting the entire configuration tree on every access.

Overall I am not happy with the way ConnectionDetails::fromArray works (the enforcement of the root connection key seems arbitrary for one) so I'll likely be rewriting that as well.

ivptr commented 3 years ago

Yes, it looks somewhat messy.

Why are you considering performance for this particular thing, connecting is done once only when starting the bot?

NanoSector commented 3 years ago

In this scenario the performance does not matter as it indeed has to be done just once while starting. However throughout the bot the configuration can be read and I'd like to have a uniform way of turning the tree into an array.

In hindsight, once fromArray has been rewritten though just passing the connection key of the tree should be enough to turn it into an array, as it is provided as an array in the configuration file. So I'm not sure how much use this would be.

The core issue is the messy method though so I'll fix that.