Skip to content

Commit

Permalink
chore: more const, better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Dec 17, 2024
1 parent a3726ef commit 1730bfe
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 141 deletions.
60 changes: 0 additions & 60 deletions dev-tools/phpstan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Console/Output/Progress/DotsOutput.php',
];
$ignoreErrors[] = [
'message' => '#^Offset string might not exist on array\\<string, class\\-string\\<PhpCsFixer\\\\Console\\\\Output\\\\Progress\\\\ProgressOutputInterface\\>\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Console/Output/Progress/ProgressOutputFactory.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Console\\\\Report\\\\FixReport\\\\CheckstyleReporter\\:\\:generate\\(\\) should return string but returns string\\|false\\.$#',
'identifier' => 'return.type',
Expand Down Expand Up @@ -745,24 +739,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/AbstractPhpUnitFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset 0 might not exist on array\\<int, string\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Alias/EregToPregFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset 1 might not exist on array\\<int, string\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Alias/EregToPregFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset 2 might not exist on array\\<int, string\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Alias/EregToPregFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset 2 might not exist on non\\-empty\\-list\\<int\\<0, max\\>\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
Expand All @@ -775,12 +751,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Alias/PowToExponentiationFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset string might not exist on array\\<string, array\\<int, int\\>\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Alias/RandomApiMigrationFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset 0 might not exist on list\\<int\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
Expand Down Expand Up @@ -835,12 +805,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Casing/MagicConstantCasingFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset string might not exist on array\\<string, string\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Casing/MagicMethodCasingFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Cannot access offset \'elements\' on array\\{index\\: int, open\\: int\\|null, close\\: int\\<0, max\\>, elements\\: list\\<array\\{token\\: PhpCsFixer\\\\Tokenizer\\\\Token, type\\: \'case\'\\|\'const\'\\|\'method\'\\|\'property\'\\|\'trait_import\', index\\: int, start\\: int, end\\: int\\}\\>\\}\\|false\\.$#',
'identifier' => 'offsetAccess.nonOffsetAccessible',
Expand Down Expand Up @@ -1597,12 +1561,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Operator/BinaryOperatorSpacesFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset string might not exist on array\\<string, array\\{int, string\\}\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Operator/LongToShorthandOperatorFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Fixer\\\\Operator\\\\TernaryToElvisOperatorFixer\\:\\:getAfterOperator\\(\\) should return array\\{start\\: int, end\\: int\\} but returns array\\{start\\: int\\|null, end\\?\\: int\\|null\\}\\.$#',
'identifier' => 'return.type',
Expand Down Expand Up @@ -1681,12 +1639,6 @@
'count' => 2,
'path' => __DIR__ . '/../../src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset string might not exist on array\\<string, array\\{positive\\: string, negative\\: string\\|false, argument_count\\?\\: int, swap_arguments\\?\\: true\\}\\|true\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset \'expectExceptionMessageRegExp\' might not exist on array\\<string, string\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
Expand Down Expand Up @@ -1753,12 +1705,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/PhpUnit/PhpUnitNoExpectationAnnotationFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset \'assertAttributeEquals\'\\|\'assertAttributeNotEquals\'\\|\'assertEquals\'\\|\'assertNotEquals\' might not exist on array\\<string, string\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/PhpUnit/PhpUnitStrictFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Fixer\\\\PhpUnit\\\\PhpUnitTestAnnotationFixer\\:\\:updateLines\\(\\) should return list\\<PhpCsFixer\\\\DocBlock\\\\Line\\> but returns array\\<int\\<0, max\\>, PhpCsFixer\\\\DocBlock\\\\Line\\>\\.$#',
'identifier' => 'return.type',
Expand Down Expand Up @@ -2089,12 +2035,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Whitespace/ArrayIndentationFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset \'break\'\\|\'case\'\\|\'continue\'\\|\'declare\'\\|\'default\'\\|\'do\'\\|\'exit\'\\|\'for\'\\|\'foreach\'\\|\'goto\'\\|\'if\'\\|\'include\'\\|\'include_once\'\\|\'phpdoc\'\\|\'require\'\\|\'require_once\'\\|\'return\'\\|\'switch\'\\|\'throw\'\\|\'try\'\\|\'while\'\\|\'yield\'\\|\'yield_from\' might not exist on array\\<string, int\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset int\\<0, max\\> might not exist on list\\<int\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
Expand Down
9 changes: 6 additions & 3 deletions src/Console/Output/Progress/ProgressOutputFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
final class ProgressOutputFactory
{
/**
* @var array<string, class-string<ProgressOutputInterface>>
* @var array<ProgressOutputType::*, class-string<ProgressOutputInterface>>
*/
private static array $outputTypeMap = [
private const OUTPUT_TYPE_MAP = [
ProgressOutputType::NONE => NullOutput::class,
ProgressOutputType::DOTS => DotsOutput::class,
ProgressOutputType::BAR => PercentageBarOutput::class,
];

/**
* @param ProgressOutputType::* $outputType
*/
public function create(string $outputType, OutputContext $context): ProgressOutputInterface
{
if (null === $context->getOutput()) {
Expand All @@ -45,7 +48,7 @@ public function create(string $outputType, OutputContext $context): ProgressOutp
);
}

return new self::$outputTypeMap[$outputType]($context);
return new (self::OUTPUT_TYPE_MAP[$outputType])($context);
}

private function isBuiltInType(string $outputType): bool
Expand Down
6 changes: 3 additions & 3 deletions src/DocBlock/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class Annotation
*
* @var list<string>
*/
private static array $tags = [
private const TAGS = [
'method',
'param',
'property',
Expand Down Expand Up @@ -127,7 +127,7 @@ public function __toString(): string
*/
public static function getTagsWithTypes(): array
{
return self::$tags;
return self::TAGS;
}

/**
Expand Down Expand Up @@ -291,7 +291,7 @@ public function getContent(): string

public function supportTypes(): bool
{
return \in_array($this->getTag()->getName(), self::$tags, true);
return \in_array($this->getTag()->getName(), self::TAGS, true);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Fixer/Alias/EregToPregFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class EregToPregFixer extends AbstractFixer
* @var list<array<int, string>> the list of the ext/ereg function names, their preg equivalent and the preg modifier(s), if any
* all condensed in an array of arrays
*/
private static array $functions = [
private const FUNCTIONS = [
['ereg', 'preg_match', ''],
['eregi', 'preg_match', 'i'],
['ereg_replace', 'preg_replace', ''],
Expand Down Expand Up @@ -82,7 +82,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$end = $tokens->count() - 1;
$functionsAnalyzer = new FunctionsAnalyzer();

foreach (self::$functions as $map) {
foreach (self::FUNCTIONS as $map) {
// the sequence is the function name, followed by "(" and a quoted string
$seq = [[T_STRING, $map[0]], '(', [T_CONSTANT_ENCAPSED_STRING]];
$currIndex = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/Fixer/Alias/RandomApiMigrationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer imple
/**
* @var array<string, array<int, int>>
*/
private static array $argumentCounts = [
private const ARGUMENT_COUNTS = [
'getrandmax' => [0],
'mt_rand' => [1, 2],
'rand' => [0, 2],
Expand Down Expand Up @@ -99,7 +99,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
[$functionName, $openParenthesis, $closeParenthesis] = $boundaries;
$count = $argumentsAnalyzer->countArguments($tokens, $openParenthesis, $closeParenthesis);

if (!\in_array($count, self::$argumentCounts[$functionIdentity], true)) {
if (!\in_array($count, self::ARGUMENT_COUNTS[$functionIdentity], true)) {
continue 2;
}

Expand Down Expand Up @@ -130,7 +130,7 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn
->setAllowedTypes(['array<string, string>'])
->setAllowedValues([static function (array $value): bool {
foreach ($value as $functionName => $replacement) {
if (!\array_key_exists($functionName, self::$argumentCounts)) {
if (!\array_key_exists($functionName, self::ARGUMENT_COUNTS)) {
throw new InvalidOptionsException(\sprintf(
'Function "%s" is not handled by the fixer.',
$functionName
Expand Down
16 changes: 8 additions & 8 deletions src/Fixer/Basic/NonPrintableCharacterFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,10 @@ final class NonPrintableCharacterFixer extends AbstractFixer implements Configur
/** @use ConfigurableFixerTrait<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration> */
use ConfigurableFixerTrait;

/**
* @var array<string, array{string, string}>
*/
private array $symbolsReplace;

/**
* @var list<int>
*/
private static array $tokens = [
private const TOKENS = [
T_STRING_VARNAME,
T_INLINE_HTML,
T_VARIABLE,
Expand All @@ -64,6 +59,11 @@ final class NonPrintableCharacterFixer extends AbstractFixer implements Configur
T_DOC_COMMENT,
];

/**
* @var array<string, array{string, string}>
*/
private array $symbolsReplace;

public function __construct()
{
parent::__construct();
Expand Down Expand Up @@ -102,7 +102,7 @@ public function isRisky(): bool

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound(self::$tokens);
return $tokens->isAnyTokenKindsFound(self::TOKENS);
}

protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
Expand Down Expand Up @@ -171,7 +171,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

if ($token->isGivenKind(self::$tokens)) {
if ($token->isGivenKind(self::TOKENS)) {
$newContent = strtr($content, $replacements);

// variable name cannot contain space
Expand Down
7 changes: 1 addition & 6 deletions src/Fixer/Casing/LowercaseKeywordsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
*/
final class LowercaseKeywordsFixer extends AbstractFixer
{
/**
* @var list<int>
*/
private static array $excludedTokens = [T_HALT_COMPILER];

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
Expand Down Expand Up @@ -64,7 +59,7 @@ public function isCandidate(Tokens $tokens): bool
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
if ($token->isKeyword() && !$token->isGivenKind(self::$excludedTokens)) {
if ($token->isKeyword() && !$token->isGivenKind([T_HALT_COMPILER])) {
$tokens[$index] = new Token([$token->getId(), strtolower($token->getContent())]);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Fixer/Casing/MagicMethodCasingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class MagicMethodCasingFixer extends AbstractFixer
/**
* @var array<string, string>
*/
private static array $magicNames = [
private const MAGIC_NAMES = [
'__call' => '__call',
'__callstatic' => '__callStatic',
'__clone' => '__clone',
Expand Down Expand Up @@ -179,15 +179,15 @@ private function isStaticMethodCall(Tokens $tokens, int $index): bool

private function isMagicMethodName(string $name): bool
{
return isset(self::$magicNames[$name]);
return isset(self::MAGIC_NAMES[$name]);
}

/**
* @param string $name name of a magic method
*/
private function getMagicMethodNameInCorrectCasing(string $name): string
{
return self::$magicNames[$name];
return self::MAGIC_NAMES[$name];
}

private function setTokenToCorrectCasing(Tokens $tokens, int $index, string $nameInCorrectCasing): void
Expand Down
14 changes: 7 additions & 7 deletions src/Fixer/ClassNotation/OrderedClassElementsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura
/**
* @var array<string, null|list<string>> Array containing all class element base types (keys) and their parent types (values)
*/
private static array $typeHierarchy = [
private const TYPE_HIERARCHY = [
'use_trait' => null,
'public' => null,
'protected' => null,
Expand Down Expand Up @@ -115,7 +115,7 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura
/**
* @var array<string, null> Array containing special method types
*/
private static array $specialTypes = [
private const SPECIAL_TYPES = [
'construct' => null,
'destruct' => null,
'magic' => null,
Expand Down Expand Up @@ -207,9 +207,9 @@ public function AWs(){}
],
'Accepts a subset of pre-defined element types, special element groups, and custom patterns.
Element types: `[\''.implode('\', \'', array_keys(self::$typeHierarchy)).'\']`
Element types: `[\''.implode('\', \'', array_keys(self::TYPE_HIERARCHY)).'\']`
Special element types: `[\''.implode('\', \'', array_keys(self::$specialTypes)).'\']`
Special element types: `[\''.implode('\', \'', array_keys(self::SPECIAL_TYPES)).'\']`
Custom values:
Expand Down Expand Up @@ -237,7 +237,7 @@ protected function configurePostNormalisation(): void
$this->typePosition[$type] = $position++;
}

foreach (self::$typeHierarchy as $type => $parents) {
foreach (self::TYPE_HIERARCHY as $type => $parents) {
if (isset($this->typePosition[$type])) {
continue;
}
Expand Down Expand Up @@ -297,7 +297,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void

protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
$builtIns = array_keys(array_merge(self::$typeHierarchy, self::$specialTypes));
$builtIns = array_keys(array_merge(self::TYPE_HIERARCHY, self::SPECIAL_TYPES));

return new FixerConfigurationResolver([
(new FixerOptionBuilder('order', 'List of strings defining order of elements.'))
Expand Down Expand Up @@ -519,7 +519,7 @@ private function sortElements(array $elements): array
return $this->typePosition["method:{$element['name']}"];
}

if (\array_key_exists($type, self::$specialTypes)) {
if (\array_key_exists($type, self::SPECIAL_TYPES)) {
if (isset($this->typePosition[$type])) {
$position = $this->typePosition[$type];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class SingleSpaceAfterConstructFixer extends AbstractProxyFixer implements
/**
* @var array<string, null|int>
*/
private static array $tokenMap = [
private const TOKEN_MAP = [
'abstract' => T_ABSTRACT,
'as' => T_AS,
'attribute' => CT::T_ATTRIBUTE_CLOSE,
Expand Down Expand Up @@ -193,7 +193,7 @@ protected function createProxyFixers(): array

protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
$defaults = self::$tokenMap;
$defaults = self::TOKEN_MAP;
$tokens = array_keys($defaults);

unset($defaults['type_colon']);
Expand Down
Loading

0 comments on commit 1730bfe

Please sign in to comment.