apache / netbeans

Apache NetBeans
https://netbeans.apache.org/
Apache License 2.0
2.64k stars 844 forks source link

adjust PHP #[\Override] hint to handle parent method with incompatible function signature #7766

Open jjdunn opened 1 week ago

jjdunn commented 1 week ago

Description

6701 added support for PHP 8.3 #[\Override] attribute. This feature works nicely.

However if the parent method has a function signature which is "incompatible" with the child method (i.e. the parent method has more arguments), then adding the #[\Override] attribute results in a PHP runtime error:

PHP Fatal error: {CLASSNAME}::__construct() has #[\Override] attribute, but no matching parent method exists

The wording of this runtime error is misleading because the parent method DOES exist, but has more arguments than the child.

We went through a large code base, adding the #[\Override] attribute according the NB hinting, only to find that our code was breaking. Now we have to review every instance of #[\Override] checking to be sure that the parent class is compatible.

It would be nice if NetBeans detected this situation and either did not offer the "Override" hint (or) offered a warning about too-few-arguments.

p.s. this is on PHP 8.3.10, NetBeans 22

Use case/motivation

No response

Related issues

No response

Are you willing to submit a pull request?

No

junichi11 commented 6 days ago

Please write an example code. Thanks!

jjdunn commented 6 days ago

here's some code extracted from our application, which is based on Yii 1.1 framework:

<?php
class CException extends Exception
{
}

class CHttpException extends CException
{
    /**
     * @var integer HTTP status code, such as 403, 404, 500, etc.
     */
    public $statusCode;

    /**
     * Constructor.
     * @param integer $status HTTP status code, such as 404, 500, etc.
     * @param string $message error message
     * @param integer $code error code
     */
        //#[\Override]  // causes PHP runtime error
    public function __construct($status,$message=null,$code=0)
    {
        $this->statusCode=$status;
        parent::__construct((string)$message,$code);
    }
}

class ConfirmAccessException extends CHttpException {
    /**
     * Constructor.
     * @param string $message error message
     * @param integer $code error code
     */
    //#[\Override]  // causes PHP runtime error
    public function __construct($message = null, $code = 0) {
        parent::__construct(403, $message, $code);
    }
}

// test
throw new ConfirmAccessException;

save the above code to a file, then open in NB 22.
NB offers the "Add #[\Override]...." hint on the __construct() method of CHttpException, in this example

In the original code, the three classes are in separate files: NB offers the "Add #[\Override]..." hint on the __construct() method of ConfirmAccessException too.

adding the #[\Override] attribute in the constructor of either class results in the PHP runtime error as mentioned in the ticket description

[21-Sep-2024 10:06:32 America/New_York] PHP Fatal error:  CHttpException::__construct() has #[\Override] attribute, but no matching parent method exists in C:\temp\test2.php on line 21

Product Version: Apache NetBeans IDE 22 Java: 20.0.2; Java HotSpot(TM) 64-Bit Server VM 20.0.2+9-78 Runtime: Java(TM) SE Runtime Environment 20.0.2+9-78 System: Windows 10 version 10.0 running on amd64; UTF-8; en_US (nb)