diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java b/php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java index 952900c172dc..5101be74251e 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/actions/FixUsesPerformer.java @@ -590,9 +590,7 @@ private static int processDeclareStatementsOffset(NamespaceScope namespaceScope, // function foo() { // declare(ticks=1) {} // } - int maxDeclareOffset = namespaceScope.getElements().isEmpty() - ? namespaceScope.getBlockRange().getEnd() - : namespaceScope.getElements().get(0).getOffset(); + int maxDeclareOffset = getMaxDeclareOffset(namespaceScope, checkVisitor); for (DeclareStatement declareStatement : checkVisitor.getDeclareStatements()) { if (maxDeclareOffset < declareStatement.getStartOffset()) { break; @@ -602,6 +600,47 @@ private static int processDeclareStatementsOffset(NamespaceScope namespaceScope, return result; } + private static int getMaxDeclareOffset(NamespaceScope namespaceScope, CheckVisitor checkVisitor) { + int maxDeclareOffset = namespaceScope.getBlockRange().getEnd(); + if (!namespaceScope.getElements().isEmpty()) { + for (ModelElement element : namespaceScope.getElements()) { + if (isInDeclare(element.getOffset(), checkVisitor.getDeclareStatements())) { + maxDeclareOffset = getDeclareEndPosition(element.getOffset(), checkVisitor.getDeclareStatements()); + continue; + } + maxDeclareOffset = element.getOffset(); + break; + } + } + return maxDeclareOffset; + } + + private static boolean isInDeclare(int offset, List declareStatements) { + // e.g. + // declare(ticks=1) { + // $test = 1; // is this element in declare? + // } + for (DeclareStatement declareStatement : declareStatements) { + if (isInDeclare(offset, declareStatement)) { + return true; + } + } + return false; + } + + private static boolean isInDeclare(int offset, DeclareStatement declareStatement) { + return declareStatement.getStartOffset() < offset && offset < declareStatement.getEndOffset(); + } + + private static int getDeclareEndPosition(int offset, List declareStatements) { + for (DeclareStatement declareStatement : declareStatements) { + if (isInDeclare(offset, declareStatement)) { + return declareStatement.getEndOffset(); + } + } + return -1; + } + @CheckForNull private static ModelElement getLastUse(NamespaceScope namespaceScope, boolean group) { ModelElement offsetElement = null; diff --git a/php/php.editor/test/unit/data/testfiles/actions/testGH5578/declare03/testGH5578_declare03.php b/php/php.editor/test/unit/data/testfiles/actions/testGH5578/declare03/testGH5578_declare03.php new file mode 100644 index 000000000000..5df3a81f1e4c --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/actions/testGH5578/declare03/testGH5578_declare03.php @@ -0,0 +1,40 @@ + selections = new ArrayList<>(); + selections.add(new Selection("\\NS2\\DeclareTest2", ItemVariant.Type.CLASS)); + Options options = new Options.Builder(PhpVersion.PHP_81).build(); + performTest("class DeclareTest1 ^{", selections, true, options); + } + + public void testGH5578_declare04() throws Exception { + List selections = new ArrayList<>(); + selections.add(new Selection("\\NS2\\DeclareTest2", ItemVariant.Type.CLASS)); + Options options = new Options.Builder(PhpVersion.PHP_81).build(); + performTest("class DeclareTest1 ^{", selections, true, options); + } + + public void testGH5578_declare05() throws Exception { + List selections = new ArrayList<>(); + selections.add(new Selection("\\NS2\\DeclareTest2", ItemVariant.Type.CLASS)); + Options options = new Options.Builder(PhpVersion.PHP_81).build(); + performTest("class DeclareTest1 ^{", selections, true, options); + } + private String getTestResult(final String fileName, final String caretLine, final List selections, final boolean removeUnusedUses, final Options options) throws Exception { FileObject testFile = getTestFile(fileName);