Skip to content

Commit

Permalink
Merge pull request apache#6171 from junichi11/php-gh-6119-incorrect-a…
Browse files Browse the repository at this point in the history
…bstract-instantiation-hint

Fix incorrect AbstractClassInstantiationHintError apache#6119
  • Loading branch information
junichi11 authored Jul 10, 2023
2 parents 4550e47 + e8bf72e commit 3a61869
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,15 @@ public static boolean isConstructor(MethodDeclaration node) {
return "__construct".equals(extractMethodName(node)); //NOI18N
}

public static boolean isDollaredName(ClassName className) {
Expression name = className.getName();
if (name instanceof Variable) {
Variable variable = (Variable) name;
return variable.isDollared();
}
return false;
}

/**
* Finds common namespace prefixes for the given <b>sorted</b> namespaces.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
package org.netbeans.modules.php.editor.verification;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.netbeans.modules.csl.api.Hint;
import org.netbeans.modules.csl.api.OffsetRange;
import org.netbeans.modules.csl.spi.support.CancelSupport;
import org.netbeans.modules.php.editor.CodeUtils;
import org.netbeans.modules.php.editor.api.ElementQuery.Index;
import org.netbeans.modules.php.editor.api.NameKind;
import org.netbeans.modules.php.editor.api.QualifiedName;
Expand Down Expand Up @@ -79,7 +81,7 @@ private CheckVisitor(FileObject fileObject, Index index, Model model) {
}

public List<Hint> getHints() {
return hints;
return Collections.unmodifiableList(hints);
}

@Override
Expand All @@ -88,7 +90,12 @@ public List<Hint> getHints() {
"AbstractClassInstantiationDesc=Abstract class {0} can not be instantiated"
})
public void visit(ClassInstanceCreation node) {
if (CancelSupport.getDefault().isCancelled()) {
if (CancelSupport.getDefault().isCancelled()
|| CodeUtils.isDollaredName(node.getClassName())) {
// GH-6119
// e.g.
// abstract class AbstractClass {}
// $a = new $abstractClass();
return;
}
ASTNodeInfo<ClassInstanceCreation> info = ASTNodeInfo.create(node);
Expand All @@ -110,7 +117,6 @@ public void visit(ClassInstanceCreation node) {
}
}
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

abstract class AbstractClass {}

$abstractClass = new $abstractClass();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.php.editor.verification;

public class AbstractClassInstantiationHintErrorTest extends PHPHintsTestBase {

public AbstractClassInstantiationHintErrorTest(String testName) {
super(testName);
}

@Override
protected String getTestDirectory() {
return TEST_DIRECTORY + "AbstractClassInstantiationHint/";
}

public void testAbstractClassInstantiationHint() throws Exception {
checkHints("testAbstractClassInstantiationHint.php");
}

public void testAbstractClassInstantiationHint_02() throws Exception {
checkHints("testAbstractClassInstantiationHint_02.php");
}

public void testGH6119() throws Exception {
checkHints("gh6119.php");
}

private void checkHints(String fileName) throws Exception {
checkHints(new AbstractClassInstantiationHintError(), fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ protected File getDataFile(String relFilePath) {
return inputFile;
}

public void testAbstractClassInstantiationHint() throws Exception {
checkHints(new AbstractClassInstantiationHintError(), "testAbstractClassInstantiationHint.php");
}

public void testAbstractClassInstantiationHint_02() throws Exception {
checkHints(new AbstractClassInstantiationHintError(), "testAbstractClassInstantiationHint_02.php");
}

public void testAmbiguousComparisonHint() throws Exception {
checkHints(new AmbiguousComparisonHint(), "testAmbiguousComparisonHint.php");
}
Expand Down

0 comments on commit 3a61869

Please sign in to comment.