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

Prevent deleting the nullable type prefix when the "fix imports" is run #7123 #7421

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,22 @@ public void visit(NamespaceName node) {

@Override
public void visit(PHPDocTypeNode node) {
UsedNamespaceName usedName = new UsedNamespaceName(node);
UsedNamespaceName usedName = createUsedNameNamespaceName(node);
if (isValidTypeName(usedName.getName()) && isValidAliasTypeName(usedName.getName())) {
processUsedName(usedName);
}
}

private UsedNamespaceName createUsedNameNamespaceName(PHPDocTypeNode node) {
// GH-7123 ignore nullable type prefix
PHPDocTypeNode phpDocTypeNode = node;
if (CodeUtils.isNullableType(node.getValue())) {
phpDocTypeNode = new PHPDocTypeNode(node.getStartOffset() + CodeUtils.NULLABLE_TYPE_PREFIX.length(), node.getEndOffset(),
CodeUtils.removeNullableTypePrefix(node.getValue()), node.isArray());
}
return new UsedNamespaceName(phpDocTypeNode);
}

private boolean isValidTypeName(final String typeName) {
return !SPECIAL_NAMES.contains(typeName)
&& !Type.isPrimitive(typeName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Name: User
?User --> ?User:869
User --> User:870
25 changes: 25 additions & 0 deletions php/php.editor/test/unit/data/testfiles/actions/gh7123_01.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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.
*/

/**
* @property ?GH7123_01 $test_1
* @property GH7123_01|null $test_2
*/
class GH7123_01{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name: GH7123_01
GH7123_01 --> GH7123_01:833
GH7123_01 --> GH7123_01:868
23 changes: 23 additions & 0 deletions php/php.editor/test/unit/data/testfiles/actions/gh7123_02.php
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.
*/

class GH7123_02{
/** @var ?GH7123_02 $test */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name: GH7123_02
GH7123_02 --> GH7123_02:842
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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.
*/

/**
* @property ?GH7123_01 $test_1
* @property GH7123_01|null $test_2
*/
class GH7123_01{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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.
*/

/**
* @property ?GH7123_01 $test_1
* @property GH7123_01|null $test_2
*/
class GH7123_01{}
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.
*/

class GH7123_02{
/** @var ?GH7123_01 $test */
}
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.
*/

class GH7123_02{
/** @var ?GH7123_01 $test */
}
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,18 @@ public void testKeepExistingUseTypeOrder_MultiplePSR05b() throws Exception {
performTest("$const1 = NS1_CONST^ANT;", selections, true, options);
}

public void testGH7123_01() throws Exception {
String[] selections = new String[] {};
Options options = new Options.Builder(PhpVersion.PHP_81).build();
performTest("class GH7123_01{}^", createSelections(selections, ItemVariant.Type.CLASS), true, options);
}

public void testGH7123_02() throws Exception {
String[] selections = new String[] {};
Options options = new Options.Builder(PhpVersion.PHP_81).build();
performTest("class GH7123_02{^", createSelections(selections, ItemVariant.Type.CLASS), true, options);
}

private String getTestResult(final String fileName, final String caretLine, final List<Selection> selections, final boolean removeUnusedUses, final Options options) throws Exception {
FileObject testFile = getTestFile(fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ public void testGH6162_01() throws Exception {
performTest("gh6162_01", "class Test ^{}");
}

public void testGH7123_01() throws Exception {
performTest("gh7123_01", "class GH7123_01^{}");
}

public void testGH7123_02() throws Exception {
performTest("gh7123_02", "class GH7123_02^{");
}

protected void performTest(String fileName, String caretLine) throws Exception {
String exactFileName = "testfiles/actions/" + fileName + ".php";
Map<String, List<UsedNamespaceName>> testResult = getTestResult(exactFileName, caretLine);
Expand Down
Loading