gabrielrcouto / php-gui

Extensionless PHP Graphic User Interface library
2.24k stars 175 forks source link

Invalid argument supplied for foreach() in D:\paiva\geraCpf\vendor\gabrielrcouto\php-gui\src\Ipc\Receiver.php #165

Closed paiva-thiago closed 5 years ago

paiva-thiago commented 5 years ago

Hello everbody.

I'm facing a problem while executing an code with php-gui:

Executing this code, when I click the $button, the error below appears and the event does not execute

<?php
require 'vendor/autoload.php';

use Gui\Application;
use Gui\Components\Button;

$application = new Application();

$application->on('start', function() use ($application) {
    $button = (new Button())
        ->setLeft(40)
        ->setTop(100)
        ->setWidth(200)
        ->setValue('Look, I\'m a button!');

    $button->on('click', function() use ($button) {
        $button->setValue('Look, I\'m a clicked button!');
    });
});

$application->run();
?[0mPHP Warning:  Invalid argument supplied for foreach() in D:\paiva\geraCpf\vendor\gabrielrcouto\php-gui\src\Ipc\Receiver.php on line 244
PHP Stack trace:
PHP   1. {main}() D:\paiva\geraCpf\issue.php:0
PHP   2. Gui\Application->run() D:\paiva\geraCpf\issue.php:21
PHP   3. React\EventLoop\StreamSelectLoop->run() D:\paiva\geraCpf\vendor\gabrielrcouto\php-gui\src\Application.php:351
PHP   4. React\EventLoop\Timer\Timers->tick() D:\paiva\geraCpf\vendor\react\event-loop\src\StreamSelectLoop.php:177
PHP   5. call_user_func:{D:\paiva\geraCpf\vendor\react\event-loop\src\Timer\Timers.php:90}() D:\paiva\geraCpf\vendor\react\event-loop\src\Timer\Timers.php:90
PHP   6. Gui\Application->Gui\{closure}() D:\paiva\geraCpf\vendor\react\event-loop\src\Timer\Timers.php:90
PHP   7. Gui\Ipc\Receiver->tick() D:\paiva\geraCpf\vendor\gabrielrcouto\php-gui\src\Application.php:347
PHP   8. React\Stream\Stream->emit() D:\paiva\geraCpf\vendor\gabrielrcouto\php-gui\src\Ipc\Receiver.php:391
PHP   9. Gui\Application->Gui\{closure}() D:\paiva\geraCpf\vendor\evenement\evenement\src\Evenement\EventEmitterTrait.php:70
PHP  10. Gui\Ipc\Receiver->onData() D:\paiva\geraCpf\vendor\gabrielrcouto\php-gui\src\Application.php:324

I was evaluating the values and I believe the problem might be in Receiver.php in line 176. Below is this class between lines 171 and 183:

      if ($fbrac < $fcurl || $fcurl === false) {
            // If '[' is before '{' use [].
            $fchar = '[';
            $lchar = ']';
            $start = $fbrac;
        } elseif ($fcurl > $fbrac || $fbrac === false) {
            // If '{' is before '[' use {}.
            $fchar = '{';
            $lchar = '}';
            $start = $fcurl;
        } else {
            return false;
        }

The comparisons in the lines 171 and 176 are equals and will return the same result.

I changed line 176 to } elseif ($fcurl > $fbrac || $fbrac === false) { and it worked well.

Edit

Obviously I didn't change to the code above and it worked. I changed to $fcurl < $fbrac!

kingga commented 5 years ago

What did the $data and $splits variable look like look like after line 242 $splits = $this->jsonSplit($data);, was it a boolean? If so maybe this should be done:

if (is_array($splits)) {
    foreach ($splits as $split) { ... }
}
kingga commented 5 years ago

This was the problem < vs >:

if ($fbrac < $fcurl || $fcurl === false) {
    // If '[' is before '{' use [].
    $fchar = '[';
    $lchar = ']';
    $start = $fbrac;
} elseif ($fcurl < $fbrac || $fbrac === false) { // Fixed.
} elseif ($fcurl > $fbrac || $fbrac === false) { // Old.
    // If '{' is before '[' use {}.
    $fchar = '{';
    $lchar = '}';
    $start = $fcurl;
} else {
    return false;
}
paiva-thiago commented 5 years ago

@kingga

If I am not wrong, $fcurl > $fbrac and $fbrac < $fcurl will return the same result.

And the purpose of this comparison is for checking the first positions of the { ($fcurl) and [ ($fbrac), and for verify who comes first.

That's the reason I changed for $fcurl < $fbrac in the condition. It will check if the curl comes before the brackets.

And I agree about adding a validation if $splits is an array.

Edit Now I see! I put the wrong code in the end of this issue! 😭 Let me edit there.

reisraff commented 5 years ago

166 merged