Skip to content

Commit

Permalink
Prevent NPE when IntroduceSuggestionHint generates a method apache#6258
Browse files Browse the repository at this point in the history
- apache#6258
- Find a position for the curl open brace `{` when a type element doesn't have other elements
```php
<?php
// e.g.
class Test {
}
```
- Add unit tests
  • Loading branch information
junichi11 committed Jul 28, 2023
1 parent 6e0a20d commit 4f3cda2
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -991,8 +991,12 @@ private static int getOffsetAfterNextTokenId(BaseDocument doc, int offset, PHPTo
* @return The offset after the last use trait statement if traits are used,
* otherwise the offset after the open curly for the type
*/
private static int getOffsetAfterUseTrait(BaseDocument document, TypeScope typeScope) {
private static int getOffsetAfterUseTrait(BaseDocument document, TypeScope typeScope) throws BadLocationException {
OffsetRange blockRange = typeScope.getBlockRange();
if (blockRange == null) {
// GH-6258 Block range of ClassScope created from ClassElement is null
return getOffsetAfterClassOpenCurly(document, typeScope.getOffset());
}
int offset = blockRange.getEnd() - 1; // before close curly "}"
Collection<ModelElement> elements = new HashSet<>();
elements.addAll(typeScope.getDeclaredMethods());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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.
*/
class TestClass {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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.
*/
TestClass::generatedMethod();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TestClass::generatedMeth^od();
----------------------------
HINT:Introduce Hint
FIX:Create Method " generatedMethod()" in Class "TestClass" (TestClass.php)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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.
*/
TestClass::generatedMethod();
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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.
*/
$test = new TestClass();
$test->generatedMethod();
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$test->generatedMet^hod();
------------------------
HINT:Introduce Hint
FIX:Create Method " generatedMethod()" in Class "TestClass" (TestClass.php)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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.
*/
$test = new TestClass();
$test->generatedMethod();
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,42 @@ public void testEnumMethods_Fix06() throws Exception {
applyHint("BackedEnumString::Case2::introdu^ceStaticMethod();", "Create Method");
}

public void testGH6258_01() throws Exception {
checkHints("testGH6258_01.php", "TestClass::generatedMeth^od();");
}

public void testGH6258_01Fix() throws Exception {
// no changes because the method is created into the TestClass
// check that NPE doesn't occur
applyHint("testGH6258_01.php", "TestClass::generatedMeth^od();", "Create Method");
}

public void testGH6258_02() throws Exception {
checkHints("testGH6258_02.php", "$test->generatedMet^hod();");
}

public void testGH6258_02Fix() throws Exception {
// no changes because the method is created into the TestClass
// check that NPE doesn't occur
applyHint("testGH6258_02.php", "$test->generatedMetho^d();", "Create Method");
}

private void checkHints(String caretLine) throws Exception {
checkHints(new IntroduceSuggestion(), getTestFileName(), caretLine);
}

private void checkHints(String fileName, String caretLine) throws Exception {
checkHints(new IntroduceSuggestion(), fileName, caretLine);
}

private void applyHint(String caretLine, String fixDesc) throws Exception {
applyHint(new IntroduceSuggestion(), getTestFileName(), caretLine, fixDesc);
}

private void applyHint(String fileName, String caretLine, String fixDesc) throws Exception {
applyHint(new IntroduceSuggestion(), fileName, caretLine, fixDesc);
}

private void fixContent(File file) throws Exception {
Path path = file.toPath();
Charset charset = StandardCharsets.UTF_8;
Expand Down

0 comments on commit 4f3cda2

Please sign in to comment.