Skip to content

Commit

Permalink
feat(EXPERIMENTAL): ClassKeywordFixer, part 2 (PHP-CS-Fixer#7550)
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus authored Dec 12, 2023
1 parent 386a2b8 commit 698a909
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion doc/rules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ Import
Language Construct
------------------

- `class_keyword <./language_construct/class_keyword.rst>`_
- `class_keyword <./language_construct/class_keyword.rst>`_ *(risky)*

EXPERIMENTAL: Converts FQCN strings to ``*::class`` keywords. Do not use it, unless you know what you are doing.
- `class_keyword_remove <./language_construct/class_keyword_remove.rst>`_ *(deprecated)*
Expand Down
16 changes: 16 additions & 0 deletions doc/rules/language_construct/class_keyword.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ Rule ``class_keyword``
EXPERIMENTAL: Converts FQCN strings to ``*::class`` keywords. Do not use it,
unless you know what you are doing.

Description
-----------

This rule does not have an understanding of whether a class exists in the scope
of the codebase or not, relying on run-time and autoloaded classes to determine
it, which makes the rule useless when running on a single file out of codebase
context.

Warning
-------

Using this rule is risky
~~~~~~~~~~~~~~~~~~~~~~~~

Risky as EXPERIMENTAL.

Examples
--------

Expand Down
26 changes: 14 additions & 12 deletions src/Fixer/LanguageConstruct/ClassKeywordFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public function getDefinition(): FixerDefinitionInterface
$bar = "\PhpCsFixer\Tokenizer\Tokens";
'
),
]
],
'This rule does not have an understanding of whether a class exists in the scope of the codebase or not, relying on run-time and autoloaded classes to determine it, which makes the rule useless when running on a single file out of codebase context.',
'Risky as EXPERIMENTAL.'
);
}

Expand All @@ -46,6 +48,11 @@ public function isCandidate(Tokens $tokens): bool
return true;
}

public function isRisky(): bool
{
return true;
}

protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
Expand All @@ -57,18 +64,13 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$name = str_replace('\\\\', '\\', $name);

if ($this->exists($name)) {
try {
$substitution = Tokens::fromCode("<?php echo \\{$name}::class;");
$substitution->clearRange(0, 2);
$substitution->clearAt($substitution->getSize() - 1);
$substitution->clearEmptyTokens();
$substitution = Tokens::fromCode("<?php echo \\{$name}::class;");
$substitution->clearRange(0, 2);
$substitution->clearAt($substitution->getSize() - 1);
$substitution->clearEmptyTokens();

$tokens->clearAt($index);
$tokens->insertAt($index, $substitution);
} catch (\Error $e) {
var_dump('error with parsing class', $name);
var_dump($e->getMessage());
}
$tokens->clearAt($index);
$tokens->insertAt($index, $substitution);
}
}
}
Expand Down

0 comments on commit 698a909

Please sign in to comment.