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 262632

Summary: No error on return different type than the method declaration wants to
Product: php Reporter: Christian Lenz <chrizzly>
Component: EditorAssignee: Tomas Mysik <tmysik>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 8.1   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:

Description Christian Lenz 2016-06-30 11:15:42 UTC
If you have this piece of code:

function test($foo): int {
   return "t";
}

I expect, as in java for example, an error or warning, that this shouldn't be possible, because the return type is int and I tried to return a string.


Cheers

Chris
Comment 1 Tomas Mysik 2016-07-01 05:46:08 UTC
It works the same way as in this example:

/**
 * @return int
 */
function test($foo) {
   return "t";
}

It means, no error in any of those cases. Moreover, if you run lint (php -l) on such a file, even PHP does not report any error. So, changing to enhancement.

Thanks for reporting.
Comment 2 Christian Lenz 2016-10-27 09:58:53 UTC
Same happens for function test(int $a) {};
Test("Test");

Should give an error that int is expected and not string. Such scalar types do not make any sense if you not check the types.
Comment 3 Christian Lenz 2016-10-27 09:59:27 UTC
I can understand the thing with the linter, maybe it is an option where you can add such checks for warnings and errors too.
Comment 4 Christian Lenz 2016-10-27 10:01:51 UTC
And here I have an other example for overriding a method:

<?php
class Test {
    function Tester(int $a, string $b) {
        echo "Tester";
    }
}

class TestImpl extends Test {
   public function Tester($a, $b) {
      echo "Tester";
   }
}

$testImpl = new TestImpl();
$testImpl->Tester(1, "t");

The method which overrides the other, should give error/warnings/hint (configurable) to show me that I missed the type and both types "should" be compatible with each other. So it's not a must have for that.
Comment 5 Christian Lenz 2016-10-27 10:05:33 UTC
Here you can see the warning: http://sandbox.onlinephpfunctions.com/code/74103db5f2a736edf1ca64cddf3ec8165b71cb95

<br />
<b>Warning</b>:  Declaration of TestImpl::Tester($a, $b) should be compatible with Test::Tester(int $a, string $b) in <b>[...][...]</b> on line <b>13</b><br />
Override