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

PHP unspected "Abstract class MyClass cannot be instantiated" #6119

Closed
pine3ree opened this issue Jun 25, 2023 · 1 comment · Fixed by #6171
Closed

PHP unspected "Abstract class MyClass cannot be instantiated" #6119

pine3ree opened this issue Jun 25, 2023 · 1 comment · Fixed by #6171
Assignees
Labels
kind:bug Bug report or fix PHP [ci] enable extra PHP tests (php/php.editor)
Milestone

Comments

@pine3ree
Copy link

pine3ree commented Jun 25, 2023

Apache NetBeans version

Apache NetBeans 18

What happened

An "Abstract class SomeClass cannot be instantiated" appears when it shouldn't.
This seems a class-variable name collision bug. Changing the variable name, or reassigning it solves the issue.

How to reproduce

Create a php file with the following content and open it:

<?php

interface MyClassInterface
{
     public function doSomething();
}

abstract class MyClass
{
    public function doSomething()
    {

    }

     abstract public function doSomethingElse();
}

class MySubClass extends MyClass implements MyClassInterface
{
    public function doSomethingElse()
    {

    }
}

class MyClassFactory
{
    public function buildObject(string $myClass): ?MyClass
    {
        if (class_exists($myClass) && is_subclass_of($myClass, MyClass::class)) {
            // This causes "can not be instantiated error"
            $myObject = new $myClass(); // WORKS!
            // do something with $myObject
            return $myObject; // WORKS!

            // Just using another variable name prevents the bug
            $myFQCN = $myClass;
            $myObject = new $myFQCN();
            // do something with $myObject
            return $myObject;
        }

        return null;
    }

    // no error
    public function createObject(string $myFQCN): ?MyClass
    {
        if (class_exists($myFQCN) && is_subclass_of($myFQCN, MyClass::class)) {
            $myObject = new $myFQCN();
            // do something with $myObject
            return $myObject;
        }

        return null;
    }
}

$factory = new MyClassFactory();

$myObject1 = $factory->buildObject(MySubClass::class);
$myObject2 = $factory->createObject(MySubClass::class);

var_dump($myObject1);
var_dump($myObject2);

Did this work correctly in an earlier version?

No / Don't know

Operating System

mint/ubuntu 22.04

JDK

java-11-openjdk-amd64

Apache NetBeans packaging

Apache NetBeans binary zip

Anything else

nb18

Are you willing to submit a pull request?

No

@pine3ree pine3ree added kind:bug Bug report or fix needs:triage Requires attention from one of the committers labels Jun 25, 2023
@junichi11 junichi11 added PHP [ci] enable extra PHP tests (php/php.editor) and removed needs:triage Requires attention from one of the committers labels Jun 27, 2023
@junichi11 junichi11 self-assigned this Jul 9, 2023
@junichi11
Copy link
Member

@pine3ree Reproducible. Thank you for your report.

@junichi11 junichi11 added this to the NB19 milestone Jul 9, 2023
junichi11 added a commit to junichi11/netbeans that referenced this issue Jul 9, 2023
- apache#6119
- Check whether the class name has `$`
- Add unit tests
@junichi11 junichi11 linked a pull request Jul 9, 2023 that will close this issue
junichi11 added a commit to junichi11/netbeans that referenced this issue Jul 9, 2023
- apache#6119
- Check whether the class name has `$`
- Add unit tests
junichi11 added a commit to junichi11/netbeans that referenced this issue Jul 10, 2023
- apache#6119
- Check whether the class name has `$`
- Add unit tests
junichi11 added a commit that referenced this issue Jul 10, 2023
…t-instantiation-hint

Fix incorrect AbstractClassInstantiationHintError #6119
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug Bug report or fix PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants