Skip to content

Commit

Permalink
Fix incorrect unused constants [GH-8244]
Browse files Browse the repository at this point in the history
- #8244
- Scan attributes after `ConstantDeclaration`s are scanned
  • Loading branch information
junichi11 committed Feb 16, 2025
1 parent 2eccc63 commit 45332c9
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,15 @@ public void visit(ClassDeclaration cldec) {
}
addToPath(cldec);
typeInfo = new TypeDeclarationTypeInfo(cldec);
scan(cldec.getAttributes());
scan(cldec.getSuperClass());
scan(cldec.getInterfaces());
Identifier name = cldec.getName();
addColoringForNode(name, createTypeNameColoring(cldec));
needToScan.put(typeInfo, new ArrayList<>());
if (cldec.getBody() != null) {
cldec.getBody().accept(this);
// GH-8244 scan attributes after constant declarations are scanned
scan(cldec.getAttributes());

// find all usages in the method bodies
scanMethodBodies();
Expand Down Expand Up @@ -513,13 +514,14 @@ public void visit(ClassInstanceCreation node) {
// GH-5551 keep original type info to scan parent blocks
TypeInfo originalTypeInfo = typeInfo;
typeInfo = new ClassInstanceCreationTypeInfo(node);
scan(node.getAttributes());
scan(node.getSuperClass());
scan(node.getInterfaces());
needToScan.put(typeInfo, new ArrayList<>());
Block body = node.getBody();
if (body != null) {
body.accept(this);
// GH-8244 scan attributes after constant declarations are scanned
scan(node.getAttributes());

// find all usages in the method bodies
scanMethodBodies();
Expand Down Expand Up @@ -571,14 +573,15 @@ public void visit(EnumDeclaration node) {
return;
}
addToPath(node);
scan(node.getAttributes());
scan(node.getInterfaces());
typeInfo = new TypeDeclarationTypeInfo(node);
Identifier name = node.getName();
addColoringForNode(name, createTypeNameColoring(node));
needToScan.put(typeInfo, new ArrayList<>());
if (node.getBody() != null) {
node.getBody().accept(this);
// GH-8244 scan attributes after constant declarations are scanned
scan(node.getAttributes());
scanMethodBodies();
addColoringForUnusedPrivateConstants();
}
Expand Down
50 changes: 50 additions & 0 deletions php/php.editor/test/unit/data/testfiles/semantic/gh8244_01.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.
*/
namespace Test;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class SomeAttribute
{
public function __construct(string $name) {}
}

#[SomeAttribute(name: self::TEST_CLASS)]
class TestClass
{
private const string TEST_CLASS = 'test';
}

#[SomeAttribute(name: self::TEST_ENUM)]
enum TestEnum
{
private const string TEST_ENUM = 'test';
}

#[SomeAttribute(name: self::TEST_TRAIT)]
trait TestTrait
{
private const string TEST_TRAIT = 'test';
}

$anon = new #[SomeAttribute(name: self::TEST_ANON)] class() {
private const string TEST_ANON = 'test';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.
*/
namespace Test;

use Attribute;

#[Attribute(Attribute::|>FIELD,STATIC:TARGET_CLASS<|)]
class |>CLASS:SomeAttribute<|
{
public function |>METHOD:__construct<|(string $name) {}
}

#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_CLASS<|)]
class |>CLASS:TestClass<|
{
private const string |>FIELD,STATIC:TEST_CLASS<| = 'test';
}

#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ENUM<|)]
enum |>CLASS:TestEnum<|
{
private const string |>FIELD,STATIC:TEST_ENUM<| = 'test';
}

#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_TRAIT<|)]
trait |>CLASS:TestTrait<|
{
private const string |>FIELD,STATIC:TEST_TRAIT<| = 'test';
}

$anon = new #[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ANON<|)] class() {
private const string |>FIELD,STATIC:TEST_ANON<| = 'test';
};
50 changes: 50 additions & 0 deletions php/php.editor/test/unit/data/testfiles/semantic/gh8244_02.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.
*/
namespace Test;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class SomeAttribute
{
public function __construct(string $name) {}
}

#[SomeAttribute(name: TestClass::TEST_CLASS)]
class TestClass
{
private const string TEST_CLASS = 'test';
}

#[SomeAttribute(name: TestEnum::TEST_ENUM)]
enum TestEnum
{
private const string TEST_ENUM = 'test';
}

#[SomeAttribute(name: TestTrait::TEST_TRAIT)]
trait TestTrait
{
private const string TEST_TRAIT = 'test';
}

$anon = new #[SomeAttribute(name: self::TEST_ANON)] class() {
private const string TEST_ANON = 'test';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.
*/
namespace Test;

use Attribute;

#[Attribute(Attribute::|>FIELD,STATIC:TARGET_CLASS<|)]
class |>CLASS:SomeAttribute<|
{
public function |>METHOD:__construct<|(string $name) {}
}

#[SomeAttribute(|>CUSTOM2:name: <|TestClass::|>FIELD,STATIC:TEST_CLASS<|)]
class |>CLASS:TestClass<|
{
private const string |>FIELD,STATIC:TEST_CLASS<| = 'test';
}

#[SomeAttribute(|>CUSTOM2:name: <|TestEnum::|>FIELD,STATIC:TEST_ENUM<|)]
enum |>CLASS:TestEnum<|
{
private const string |>FIELD,STATIC:TEST_ENUM<| = 'test';
}

#[SomeAttribute(|>CUSTOM2:name: <|TestTrait::|>FIELD,STATIC:TEST_TRAIT<|)]
trait |>CLASS:TestTrait<|
{
private const string |>FIELD,STATIC:TEST_TRAIT<| = 'test';
}

$anon = new #[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ANON<|)] class() {
private const string |>FIELD,STATIC:TEST_ANON<| = 'test';
};
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,12 @@ public void testGH5551_02() throws Exception {
public void testDynamicClassConstantFetch_01() throws Exception {
checkSemantic("testfiles/semantic/dynamicClassConstantFetch_01.php");
}

public void testGH8244_01() throws Exception {
checkSemantic("testfiles/semantic/gh8244_01.php");
}

public void testGH8244_02() throws Exception {
checkSemantic("testfiles/semantic/gh8244_02.php");
}
}

0 comments on commit 45332c9

Please sign in to comment.