This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 259111

Summary: [PHP7] Callable variable in parentheses is marked as error
Product: php Reporter: dasprid <dasprid>
Component: EditorAssignee: junichi11
Status: VERIFIED FIXED    
Severity: normal CC: amobilia, dasprid, femy_rox, gibster, junichi11, tmysik, udarkness
Priority: P3    
Version: 8.2   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:
Attachments: screenshot
screenshot
CC problem

Description dasprid 2016-05-03 13:10:08 UTC
Imagine having the following code:

<?php
$f = function(){};
($f)();

Putting the callable variable in parentheses is actually valid as of PHP 5.6, but Netbeans marks it as an error, with both PHP 5.6 and 7.0 project settings.
Comment 1 Tomas Mysik 2016-05-03 13:30:13 UTC
True. Unfortunately, this is quite difficult to fix so as a workaround, simply use "$f()", this should work. Sorry for inconveniences.

Thanks.
Comment 2 dasprid 2016-05-03 14:13:54 UTC
That workaround only works in that simple case, consider the following example:

class Foo
{
    private $callable;

    public function __construct(callable $callable)
    {
        $this->callable = $callable;
    }

    public function doSomething()
    {
        ($this->callable)();
    }
}

The only workaround to that would be to do this in the method:

$callable = $this->callable;
$callable();

Which is really quite ugly and requires creating a new reference.
Comment 3 Tomas Mysik 2016-05-03 15:02:01 UTC
You are right, we know about it. Unfortunately, as I wrote, it is not easy to fix this issue but we will try it, of course.

Thanks.
Comment 4 dasprid 2016-05-03 23:40:15 UTC
I sadly don't know much about Netbeans source, let alone Java. But could you point me to the file which is responsible of the parsing, so I could take a look at it?
Comment 5 Tomas Mysik 2016-05-04 08:02:11 UTC
(In reply to dasprid from comment #4)
> But could you
> point me to the file which is responsible of the parsing, so I could take a
> look at it?

Sure, its this file [1]. Adding Junichi to CC, he could be maybe interested in fixing of this issue. If not, I will try to have a look at it.

Thanks.
[1] http://hg.netbeans.org/web-main/file/tip/php.editor/tools/ASTPHP5Parser.cup
Comment 6 junichi11 2016-05-04 11:45:52 UTC
I'm not sure whether I can fix it. But I'll try to have a look at it once (later).

Thanks.
Comment 7 Tomas Mysik 2016-09-19 12:24:55 UTC
*** Bug 268100 has been marked as a duplicate of this bug. ***
Comment 8 Tomas Mysik 2016-09-23 05:17:58 UTC
*** Bug 268163 has been marked as a duplicate of this bug. ***
Comment 9 junichi11 2016-09-27 09:55:41 UTC
Created attachment 162235 [details]
screenshot

I'm looking at this. We may be able to fix it.(I can be wrong.)

Could anyone provide more examples((...)())? Is there anyone who has them?
 

Thanks.
Comment 10 dasprid 2016-09-27 10:04:43 UTC
Here are a few more:

class Bar { public function __invoke() {} }
(new Bar())();

(new class { public function __invoke() {} })();

'strlen'('1');
Comment 11 dasprid 2016-09-27 10:05:47 UTC
And another one:

class Baz { public static function bat() {} }
php > ['Baz', 'bat']();
Comment 12 junichi11 2016-09-27 10:38:39 UTC
(In reply to dasprid from comment #10)
> Here are a few more:
> 
> class Bar { public function __invoke() {} }
> (new Bar())();
> 
> (new class { public function __invoke() {} })();
> 
> 'strlen'('1');

(In reply to dasprid from comment #11)
> And another one:
> 
> class Baz { public static function bat() {} }
> php > ['Baz', 'bat']();

Thank you!

They are errors in my change yet. I'll try it again...
Comment 13 Tomas Mysik 2016-09-27 11:48:15 UTC
(In reply to junichi11 from comment #9)
> Created attachment 162235 [details]
> screenshot
> 
> I'm looking at this. We may be able to fix it.(I can be wrong.)

Seems great, Junichi! Adding 82patch-candidate Whiteboard so it could be part of the patch for NB 8.2.

Thanks a lot!
Comment 14 junichi11 2016-10-04 04:41:50 UTC
Created attachment 162324 [details]
screenshot

I fixed the syntax errors for the following code:

<?php
$func1 = function(){echo "anonymous function1" . PHP_EOL;};
($func1)(); // anonymous function1

$func2 = function() {
    return function() {echo "anonymous function2" . PHP_EOL;};
};
(($func2)())(); // anonymous function2

(function(){echo "anonymous function3" . PHP_EOL;})(); // anonymous function3

(function($param1, $param2) {echo "anonymous function4" . PHP_EOL;})("param1", "param2"); // anonymous function4

class Bar { public function __invoke() {echo "class __invoke" . PHP_EOL;} }
(new Bar())(); // class __invoke
(new Bar)(); // class __invoke

(new class { public function __invoke() {echo "anonymous class __invoke" . PHP_EOL;} })(); // anonymous class __invoke

echo 'strlen'('scalar call') . PHP_EOL; // 11
$something = 'something'; 
echo 'strlen'($something) . PHP_EOL; // 9
echo ("strlen")($something) . PHP_EOL; // 9

$a = "strlen";
$b = "a";
echo ($$b)("foo") . PHP_EOL; // 3

class ArrayCall { public static function test() { echo "array call" . PHP_EOL;} }
$array = new ArrayCall();
['ArrayCall', 'test'](); // array call
(['ArrayCall', 'test'])(); // array call
[new ArrayCall, 'test'](); // array call
[new ArrayCall(), 'test'](); // array call
array('ArrayCall', 'test')(); // array call
array($array, 'test')(); // array call
$test = "test";
[new class {public static function test($param) {echo "anonymous class array call" . PHP_EOL;}}, $test]("test"); // anonymous call array call

class Foo
{
    private $callable;

    public function __construct(callable $callable)
    {
        $this->callable = $callable;
    }

    public function doSomething()
    {
        ($this->callable)();
    }
}

But something is missing yet... There is a CC problem. I'll try to fix it.

Thanks.
Comment 15 junichi11 2016-10-04 04:58:10 UTC
Created attachment 162325 [details]
CC problem

(In reply to junichi11 from comment #14)

> But something is missing yet... There is a CC problem. I'll try to fix it.

Sorry, I'm wrong... This also occurs when anonymous functions are used in NB8.1. So, it's another problem.
Comment 16 junichi11 2016-10-04 05:43:06 UTC
Now, some existing tests fail. So I'll look at them.
Comment 17 Tomas Mysik 2016-10-04 05:50:11 UTC
Great job, Junichi! Thanks a lot!
Comment 18 junichi11 2016-10-07 06:00:56 UTC
I fixed it. But there is one formatting problem[1] yet. IMHO, it is a minor use case. So, I didn't fix it.

Tomas, Which do you prefer?
- Attach a patch
- Push my changes

Should I update version number at the same time?

Thanks.


[1]
<?php

[new class {public static function test($param) {echo "anonymous class array call" . PHP_EOL;}}, $test]("test");

Actual:
[new class {

public static function test($param) {
    echo "anonymous class array call" . PHP_EOL;
}
}, $test]("test");

Expected:
[new class {

    public static function test($param) {
        echo "anonymous class array call" . PHP_EOL;
    }
}, $test]("test");
Comment 19 junichi11 2016-10-14 03:46:41 UTC
Fixed.

http://hg.netbeans.org/web-main/rev/f93a28afd2e8

Thanks.
Comment 20 Tomas Mysik 2016-10-14 06:23:18 UTC
Guys, in order to be part of the patch for NB 8.2, could you please verify that this issue is fixed now? Just wait for a message that will appear here saying in which daily build this change will be available.

Junichi, great job as always!

Thanks!
Comment 21 Quality Engineering 2016-10-15 02:13:53 UTC
Integrated into 'main-silver', will be available in build *201610150002* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/f93a28afd2e8
User: Junichi Yamamoto <junichi11@netbeans.org>
Log: #259111 - [PHP7] Callable variable in parentheses is marked as error
Comment 22 dasprid 2016-10-19 11:14:53 UTC
Looks fixed for me.
Comment 23 junichi11 2016-10-19 22:53:50 UTC
(In reply to dasprid from comment #22)
> Looks fixed for me.

Thanks for your verification!
Comment 24 Tomas Mysik 2016-10-20 06:04:08 UTC
Thanks a lot!
Comment 25 Tomas Mysik 2016-10-24 08:03:12 UTC
*** Bug 268652 has been marked as a duplicate of this bug. ***
Comment 26 Tomas Mysik 2016-12-03 14:10:02 UTC
Transplanted to the releases repo branch release82:

http://hg.netbeans.org/releases/rev/4d288f1e22ad

Thanks.
Comment 27 amobilia 2016-12-15 12:17:35 UTC
82patch1-verified