Error: implode(): Argument #2 ($array) must be of type ?array, string given
gutorsantos opened this issue · comments
I have been working on a Laravel project and I had to use PAMI, by the way it's a great library! However, recently I got this error
[2022-01-21 09:55:06] ERROR: implode(): Argument #2 ($array) must be of type ?array, string given {"userId":8,"exception":"[object] (TypeError(code: 0): implode(): Argument #2 ($array) must be of type ?array, string given at .../marcelog/pami/src/PAMI/Message/Event/Factory/Impl/EventFactoryImpl.php:72)
I'm using the following settings:
OS: Ubuntu 20.04
Laravel 8
PHP 8+ (at least 8 or newer)
After a few days of headache and tests, I finally found what was occurring. I went to the error line on EventFactoryImpl.php. At beginning, I was thinking Asterisk server was returning a wrong message, after some tests and verifications I concluded it was not the server. In order that, I went to php documentation and found the order of implode function arguments was wrong. According to the documentation the function signature is as follows:
implode(string $separator, array $array): string
As we can see on code, the order is inverted:
public static function createFromRaw($message)
{
$eventStart = strpos($message, 'Event: ') + 7;
$eventEnd = strpos($message, Message::EOL, $eventStart);
if ($eventEnd === false) {
$eventEnd = strlen($message);
}
$name = substr($message, $eventStart, $eventEnd - $eventStart);
$parts = explode('_', $name);
$totalParts = count($parts);
for ($i = 0; $i < $totalParts; $i++) {
$parts[$i] = ucfirst($parts[$i]);
}
$name = implode($parts, ''); # trouble line
$className = '\\PAMI\\Message\\Event\\' . $name . 'Event';
if (class_exists($className, true)) {
return new $className($message);
}
return new UnknownEvent($message);
}
Because of that I made a slightly change, reverting the order.
public static function createFromRaw($message)
{
$eventStart = strpos($message, 'Event: ') + 7;
$eventEnd = strpos($message, Message::EOL, $eventStart);
if ($eventEnd === false) {
$eventEnd = strlen($message);
}
$name = substr($message, $eventStart, $eventEnd - $eventStart);
$parts = explode('_', $name);
$totalParts = count($parts);
for ($i = 0; $i < $totalParts; $i++) {
$parts[$i] = ucfirst($parts[$i]);
}
$name = implode('', $parts); # changed line
$className = '\\PAMI\\Message\\Event\\' . $name . 'Event';
if (class_exists($className, true)) {
return new $className($message);
}
return new UnknownEvent($message);
}
Hope this can help someone. And also be fixed in this wonderful project.
Thanks!
Confirmed!
They only need to Implement the Change in the repository, when they believe that this
@lorismich where did you merged this PR because I can't find it in your fork?
@lorismich where did you merged this PR because I can't find it in your fork?
@91Abdullah
I reopened the PR here: #213