Skip to content

Commit

Permalink
chore: more const, better typing (PHP-CS-Fixer#8320)
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus authored Dec 17, 2024
1 parent f8ce798 commit 3b6aec0
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 156 deletions.
66 changes: 0 additions & 66 deletions dev-tools/phpstan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Console/Output/ErrorOutput.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 @@ -739,24 +733,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 @@ -769,12 +745,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 @@ -829,12 +799,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 @@ -1591,12 +1555,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 @@ -1675,12 +1633,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 @@ -1747,12 +1699,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 @@ -2083,12 +2029,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 Expand Up @@ -2917,12 +2857,6 @@
'count' => 1,
'path' => __DIR__ . '/../../tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php',
];
$ignoreErrors[] = [
'message' => '#^Offset \'operators\' might not exist on array\\<string, mixed\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../tests/Fixer/Operator/LongToShorthandOperatorFixerTest.php',
];
$ignoreErrors[] = [
'message' => '#^Only booleans are allowed in a negated boolean, string\\|false given\\.$#',
'identifier' => 'booleanNot.exprNotBoolean',
Expand Down
4 changes: 3 additions & 1 deletion src/Console/ConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ final class ConfigurationResolver
private ?array $path = null;

/**
* @var null|string
* @var null|ProgressOutputType::*
*/
private $progress;

Expand Down Expand Up @@ -409,6 +409,8 @@ static function (string $rawPath) use ($cwd, $filesystem): string {
}

/**
* @return ProgressOutputType::*
*
* @throws InvalidConfigurationException
*/
public function getProgressType(): string
Expand Down
12 changes: 9 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,10 @@ public function create(string $outputType, OutputContext $context): ProgressOutp
);
}

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

// @phpstan-ignore-next-line new.noConstructor
return new $outputClass($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
8 changes: 5 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,9 @@ 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)) {
\assert(isset(self::ARGUMENT_COUNTS[$functionIdentity])); // for PHPStan

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

Expand Down Expand Up @@ -130,7 +132,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
13 changes: 9 additions & 4 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 @@ -177,17 +177,22 @@ private function isStaticMethodCall(Tokens $tokens, int $index): bool
return $tokens[$tokens->getNextMeaningfulToken($index)]->equals('(');
}

/**
* @phpstan-assert-if-true key-of<self::MAGIC_NAMES> $name
*/
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
* @param key-of<self::MAGIC_NAMES> $name name of a magic method
*
* @return value-of<self::MAGIC_NAMES>
*/
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
Loading

0 comments on commit 3b6aec0

Please sign in to comment.