Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jjdunn opened this issue Sep 18, 2024 · 2 comments
Labels
kind:feature A feature request needs:triage Requires attention from one of the committers PHP [ci] enable extra PHP tests (php/php.editor)

Comments

@jjdunn
Copy link

jjdunn commented Sep 18, 2024

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 jjdunn added kind:feature A feature request needs:triage Requires attention from one of the committers labels Sep 18, 2024
@troizet troizet added the PHP [ci] enable extra PHP tests (php/php.editor) label Sep 18, 2024
@junichi11
Copy link
Member

Please write an example code. Thanks!

@jjdunn
Copy link
Author

jjdunn commented Sep 21, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feature A feature request needs:triage Requires attention from one of the committers PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

No branches or pull requests

3 participants