Skip to content

Write class names added by fixes relative to namespace and uses - #6924

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
fixer-new-class-names
Nov 3, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
fixer-new-class-names

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Fixes now write the class names they add the same way a person would, in PHP code and in PHPDocs.

NewClassNamesVisitor runs after all fixes in a file are applied. For each fully qualified class name the fix added (names already in the file are left as written):

  1. self for the current class, if fixer.preferSelf is enabled (not in attributes, extends, implements, or trait use),
  2. relative to an existing use or to the current namespace,
  3. its short name with a new use statement, if fixer.importNames is enabled and the short name does not already mean something in the file (an existing use alias, a declared class, a relative name in code or PHPDoc),
  4. otherwise fully qualified.

New use statements are inserted alphabetically among the existing ones, before use function/use const, or after declare with a blank line when it's the first one.

New parameters:

parameters:
	fixer:
		importNames: true
		preferSelf: false

Global-namespace classes are not imported, so #[Override] stays #[\Override] unless use Override; is already there.

migrate:neon2attributes now builds fully qualified names and lets the visitor import them; ImportScope and createName() are removed. Its existing tests pass unchanged.

Tests: AddNamesRule test rule adding native and PHPDoc types (default config, and importNames: false + preferSelf: true), plus #[Override] fixes with an existing use Override and in the global namespace.

@ondrejmirtes
ondrejmirtes force-pushed the fixer-new-class-names branch 4 times, most recently from f7d29eb to 7791cea Compare November 3, 2026 11:05
NewClassNamesVisitor runs after every fix and writes the fully qualified
class names a fix added, in PHP code and in PHPDocs, relative to the
current namespace and existing use statements. With fixer.importNames
(on by default) it adds a use statement for a name from another
namespace unless its short name already means something in the file.
fixer.preferSelf writes the current class as self.

The migrate:neon2attributes command now builds fully qualified names
and lets the visitor import them, replacing ImportScope.

Amp-Thread-ID: https://ampcode.com/threads/T-01a179f2-995f-7745-8d8c-4c174ba5c0ce
@ondrejmirtes
ondrejmirtes force-pushed the fixer-new-class-names branch from 7791cea to ec0010d Compare November 3, 2026 11:11
@ondrejmirtes
ondrejmirtes merged commit 95d9e8b into 2.3.x Nov 3, 2026
1035 of 1039 checks passed
@ondrejmirtes
ondrejmirtes deleted the fixer-new-class-names branch November 3, 2026 11:25
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.

1 participant