-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #4. Implemented ErickSkrauch/ordered_overrides fixer
- Loading branch information
1 parent
4f379ec
commit bb0bac3
Showing
12 changed files
with
638 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ErickSkrauch\PhpCsFixer\Analyzer; | ||
|
||
use LogicException; | ||
use PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer; | ||
use PhpCsFixer\Tokenizer\Analyzer\NamespaceUsesAnalyzer; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
// TODO: better naming | ||
// TODO: cover with tests | ||
final class ClassNameAnalyzer { | ||
|
||
private NamespacesAnalyzer $namespacesAnalyzer; | ||
|
||
private NamespaceUsesAnalyzer $namespacesUsesAnalyzer; | ||
|
||
public function __construct() { | ||
$this->namespacesAnalyzer = new NamespacesAnalyzer(); | ||
$this->namespacesUsesAnalyzer = new NamespaceUsesAnalyzer(); | ||
} | ||
|
||
/** | ||
* @see https://www.php.net/manual/en/language.namespaces.rules.php | ||
* | ||
* @phpstan-return class-string | ||
*/ | ||
public function getFqn(Tokens $tokens, int $classNameIndex): string { | ||
$firstPart = $tokens[$classNameIndex]; | ||
if (!$firstPart->isGivenKind([T_STRING, T_NS_SEPARATOR])) { | ||
throw new LogicException(sprintf('No T_STRING or T_NS_SEPARATOR at given index %d, got "%s".', $classNameIndex, $firstPart->getName())); | ||
} | ||
|
||
$relativeClassName = $this->readClassName($tokens, $classNameIndex); | ||
if (str_starts_with($relativeClassName, '\\')) { | ||
return $relativeClassName; // @phpstan-ignore return.type | ||
} | ||
|
||
$namespace = $this->namespacesAnalyzer->getNamespaceAt($tokens, $classNameIndex); | ||
$uses = $this->namespacesUsesAnalyzer->getDeclarationsInNamespace($tokens, $namespace); | ||
$parts = explode('\\', $relativeClassName, 2); | ||
/** @var \PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis $use */ | ||
foreach ($uses as $use) { | ||
if ($use->getShortName() !== $parts[0]) { | ||
continue; | ||
} | ||
|
||
// @phpstan-ignore return.type | ||
return '\\' . $use->getFullName() . (isset($parts[1]) ? ('\\' . $parts[1]) : ''); | ||
} | ||
|
||
// @phpstan-ignore return.type | ||
return ($namespace->getFullName() !== '' ? '\\' : '') . $namespace->getFullName() . '\\' . $relativeClassName; | ||
} | ||
|
||
private function readClassName(Tokens $tokens, int $classNameStart): string { | ||
$className = ''; | ||
$index = $classNameStart; | ||
do { | ||
$token = $tokens[$index]; | ||
if ($token->isWhitespace()) { | ||
continue; | ||
} | ||
|
||
$className .= $token->getContent(); | ||
} while ($tokens[++$index]->isGivenKind([T_STRING, T_NS_SEPARATOR, T_WHITESPACE])); | ||
|
||
return $className; | ||
} | ||
|
||
} |
Oops, something went wrong.