-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ordered_overrides_fixer'
- Loading branch information
Showing
17 changed files
with
900 additions
and
9 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
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
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,132 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ErickSkrauch\PhpCsFixer\Analyzer; | ||
|
||
use PhpCsFixer\Tokenizer\CT; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
/** | ||
* Taken from the \PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer and simplified | ||
* | ||
* @phpstan-type AnalyzedClassElementType 'use_trait'|'case'|'constant'|'property'|'method' | ||
* @phpstan-type AnalyzedClassElement array{ | ||
* start: int, | ||
* visibility: string, | ||
* abstract: bool, | ||
* static: bool, | ||
* readonly: bool, | ||
* type: AnalyzedClassElementType, | ||
* name: string, | ||
* end: int, | ||
* } | ||
*/ | ||
final class ClassElementsAnalyzer { | ||
|
||
/** | ||
* @return list<AnalyzedClassElement> | ||
*/ | ||
public function getClassElements(Tokens $tokens, int $classOpenBraceIndex): array { | ||
static $elementTokenKinds = [CT::T_USE_TRAIT, T_CASE, T_CONST, T_VARIABLE, T_FUNCTION]; | ||
|
||
$startIndex = $classOpenBraceIndex + 1; | ||
$elements = []; | ||
|
||
while (true) { | ||
$element = [ | ||
'start' => $startIndex, | ||
'visibility' => 'public', | ||
'abstract' => false, | ||
'static' => false, | ||
'readonly' => false, | ||
]; | ||
|
||
for ($i = $startIndex; ; ++$i) { | ||
$token = $tokens[$i]; | ||
|
||
// class end | ||
if ($token->equals('}')) { | ||
return $elements; // @phpstan-ignore return.type | ||
} | ||
|
||
if ($token->isGivenKind(T_ABSTRACT)) { | ||
$element['abstract'] = true; | ||
|
||
continue; | ||
} | ||
|
||
if ($token->isGivenKind(T_STATIC)) { | ||
$element['static'] = true; | ||
|
||
continue; | ||
} | ||
|
||
if (defined('T_READONLY') && $token->isGivenKind(T_READONLY)) { | ||
$element['readonly'] = true; | ||
} | ||
|
||
if ($token->isGivenKind([T_PROTECTED, T_PRIVATE])) { | ||
$element['visibility'] = strtolower($token->getContent()); | ||
|
||
continue; | ||
} | ||
|
||
if (!$token->isGivenKind($elementTokenKinds)) { | ||
continue; | ||
} | ||
|
||
$element['type'] = $this->detectElementType($tokens, $i); | ||
if ($element['type'] === 'property') { | ||
$element['name'] = $tokens[$i]->getContent(); | ||
} elseif (in_array($element['type'], ['use_trait', 'case', 'constant', 'method', 'magic', 'construct', 'destruct'], true)) { | ||
$element['name'] = $tokens[$tokens->getNextMeaningfulToken($i)]->getContent(); | ||
} | ||
|
||
$element['end'] = $this->findElementEnd($tokens, $i); | ||
|
||
break; | ||
} | ||
|
||
$elements[] = $element; | ||
$startIndex = $element['end'] + 1; // @phpstan-ignore offsetAccess.notFound | ||
} | ||
} | ||
|
||
/** | ||
* @phpstan-return AnalyzedClassElementType | ||
*/ | ||
private function detectElementType(Tokens $tokens, int $index): string { | ||
$token = $tokens[$index]; | ||
if ($token->isGivenKind(CT::T_USE_TRAIT)) { | ||
return 'use_trait'; | ||
} | ||
|
||
if ($token->isGivenKind(T_CASE)) { | ||
return 'case'; | ||
} | ||
|
||
if ($token->isGivenKind(T_CONST)) { | ||
return 'constant'; | ||
} | ||
|
||
if ($token->isGivenKind(T_VARIABLE)) { | ||
return 'property'; | ||
} | ||
|
||
return 'method'; | ||
} | ||
|
||
private function findElementEnd(Tokens $tokens, int $index): int { | ||
$endIndex = $tokens->getNextTokenOfKind($index, ['{', ';']); | ||
if ($tokens[$endIndex]->equals('{')) { | ||
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $endIndex); | ||
} | ||
|
||
for (++$endIndex; $tokens[$endIndex]->isWhitespace(" \t") || $tokens[$endIndex]->isComment(); ++$endIndex); | ||
|
||
--$endIndex; | ||
|
||
return $tokens[$endIndex]->isWhitespace() ? $endIndex - 1 : $endIndex; | ||
} | ||
|
||
} |
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,65 @@ | ||
<?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; | ||
|
||
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 { | ||
$className .= $tokens[$index]->getContent(); | ||
} while ($tokens[++$index]->isGivenKind([T_STRING, T_NS_SEPARATOR])); | ||
|
||
return $className; | ||
} | ||
|
||
} |
Oops, something went wrong.