netbeans icon indicating copy to clipboard operation
netbeans copied to clipboard

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

Open jjdunn opened this issue 1 year ago • 2 comments

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

jjdunn avatar Sep 18 '24 11:09 jjdunn

Please write an example code. Thanks!

junichi11 avatar Sep 21 '24 04:09 junichi11

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)

jjdunn avatar Sep 21 '24 14:09 jjdunn

Not only in case of different number of arguments, also in arg-less constructor.

My code:

Class Reader
{
    public function __construct()
    { [...] }
}

Class CLIReader extends Reader
{
    public function __construct()
    { [...] }
}

results in the same issue.

Image

LeonardoCor avatar Apr 30 '25 06:04 LeonardoCor

closing this ticket because it seems to have been fixed in https://github.com/apache/netbeans/issues/8420

jjdunn avatar May 23 '25 13:05 jjdunn