Skip to content

Add remove imports method#59

Open
artengin wants to merge 14 commits into
masterfrom
add-remove-imports-method
Open

Add remove imports method#59
artengin wants to merge 14 commits into
masterfrom
add-remove-imports-method

Conversation

@artengin

@artengin artengin commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

refs: #60

@artengin

artengin commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

It inherits from this commit: #51

@artengin artengin self-assigned this Apr 9, 2026
@coveralls

coveralls commented Apr 9, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 27782496828

Coverage increased (+0.1%) to 98.801%

Details

  • Coverage increased (+0.1%) from the base build.
  • Patch coverage: 39 of 39 lines across 4 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 417
Covered Lines: 412
Line Coverage: 98.8%
Coverage Strength: 10.81 hits per line

💛 - Coveralls

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3253334ff4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/RemoveImports.php Outdated
Comment thread src/Visitors/RemoveImports.php Outdated
@artengin artengin force-pushed the add-remove-imports-method branch from 3253334 to adf1204 Compare April 10, 2026 07:22

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: adf1204014

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/RemoveImports.php Outdated

protected function hasUsageOf(string $name, array $nodes): bool
{
return !empty($this->nodeFinder->find($nodes, fn (Node $node) => $node instanceof Name && $node->toString() === $name));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Match import usages without case sensitivity

PHP class-name resolution through use aliases is case-insensitive, but this check only treats an import as used when the Name::toString() value matches exactly. In files that reference the alias with different casing (for example use App\Models\User; and later user in a type hint), removeImports() will incorrectly drop a still-used import, which can change resolution to the current namespace and lead to runtime class-not-found failures.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid edge case per PHP spec, but in practice it's a non-issue - writing USER or user instead of User violates every coding standard and no real codebase does this.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: efab0d1f94

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/RemoveImports.php Outdated
}
}

$targetNodes = array_filter($targetNodes, fn ($node) => !($node instanceof Use_) || !empty($node->uses));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reindex namespace statements after filtering imports

array_filter() preserves numeric keys, so this assignment can leave $targetNodes sparse after imports are removed. In workflows that chain removeImports() before another visitor that inserts top-level statements (for example addImports()), getInsertIndex()/array_splice() treat those stale keys as positional offsets and can place new use statements after the class, producing invalid statement ordering. Reindex the filtered list (e.g., with array_values) before returning.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array_values is not needed here, testRemoveImportsThenAddImports confirms this

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 996ae46e48

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +210 to +213
$targetNamespace = array_find($nodes, fn ($node) => $node instanceof Namespace_);

if (!is_null($targetNamespace)) {
return $targetNamespace->stmts;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Traverse every namespace when collecting import statements

getNamespaceStatements returns only the first Namespace_ block, so removeImports() (and any visitor using this helper) skips imports declared in later namespace blocks of the same file. In files that use multiple namespaces (e.g., bracketed namespace syntax), requested removals in non-first namespaces are silently ignored, which makes the new API behave inconsistently and leaves stale imports behind.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PSR-1 requires one namespace per file.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8de82a8221

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/RemoveImports.php Outdated
@artengin

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 19e86cb28e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

}
}

$targetNodes = array_filter($targetNodes, fn ($node) => !$this->isEmptyImportNode($node));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reindex filtered namespace statements

afterTraverse() assigns array_filter(...) directly to $targetNodes, but array_filter keeps original numeric keys. When this visitor is chained with addImports(), BaseNodeVisitorAbstract::getInsertIndex() uses those sparse keys as if they were positional indexes, so array_splice can insert a new use after the class (or below the import separator) if an earlier import was removed. This can produce invalid PHP in workflows like removeImports(...)->addImports(...); reindexing the filtered array (e.g., array_values) avoids the offset mismatch.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array_values is not needed here, testRemoveImportsThenAddImports confirms this

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 054bddcf1d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/RemoveImport.php
@artengin artengin assigned DenTray and unassigned artengin May 7, 2026
Comment thread src/Builders/PHPFileBuilder.php Outdated
@DenTray DenTray assigned artengin and unassigned DenTray May 21, 2026
@artengin

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e6475ddf93

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/Visitors/RemoveImport.php
{
return !empty($this->nodeFinder->findFirst(
$nodes,
fn (Node $node) => $node instanceof Name && get_class($node) === Name::class && $node->getFirst() === $name,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict usage scan to class-like name contexts

The usage detector matches any plain Name node with the same first segment, regardless of context. This includes function and constant references, so a class import can be treated as "used" just because code calls a same-named function/constant, preventing cleanup of actually unused imports. That violates the method contract to remove unused imports safely and makes results depend on unrelated identifiers.

Useful? React with 👍 / 👎.

@artengin artengin assigned DenTray and unassigned artengin May 22, 2026
@DenTray

DenTray commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

@artengin please resolve conflicts

@DenTray DenTray assigned artengin and unassigned DenTray Jun 4, 2026
@artengin

artengin commented Jun 5, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e76faa2338

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +77 to +79
$nodesWithoutImports = array_filter($targetNodes, fn ($node) => !($node instanceof Use_) && !($node instanceof GroupUse));

if ($this->hasUsageOf($importName, $nodesWithoutImports)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve docblock-only imports

When a requested import is referenced only from PHPDoc, such as @return UserService or @var Collection<UserService>, the default removeImports(..., false) still deletes it because usage detection only scans AST nodes and never inspects doc comments. That breaks static-analysis and IDE type resolution even though the non-forced mode is documented as safe unless the import is unused; please include doc comments in the usage check before removing.

Useful? React with 👍 / 👎.

@artengin artengin assigned DenTray and unassigned artengin Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants