Skip to content

Commit

Permalink
minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Dec 17, 2024
2 parents 1730bfe + f8ce798 commit 3cc4349
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 24 deletions.
6 changes: 0 additions & 6 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 int might not exist on array\\<1\\|2\\|3\\|4\\|5\\|6, array\\{symbol\\: string, format\\: string, description\\: string\\}\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Console/Output/Progress/DotsOutput.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Console\\\\Report\\\\FixReport\\\\CheckstyleReporter\\:\\:generate\\(\\) should return string but returns string\\|false\\.$#',
'identifier' => 'return.type',
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
8 changes: 4 additions & 4 deletions src/Console/Output/Progress/DotsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class DotsOutput implements ProgressOutputInterface
*
* @var array<FileProcessed::STATUS_*, array{symbol: string, format: string, description: string}>
*/
private static array $eventStatusMap = [
private const EVENT_STATUS_MAP = [
FileProcessed::STATUS_NO_CHANGES => ['symbol' => '.', 'format' => '%s', 'description' => 'no changes'],
FileProcessed::STATUS_FIXED => ['symbol' => 'F', 'format' => '<fg=green>%s</fg=green>', 'description' => 'fixed'],
FileProcessed::STATUS_SKIPPED => ['symbol' => 'S', 'format' => '<fg=cyan>%s</fg=cyan>', 'description' => 'skipped (cached or empty file)'],
Expand Down Expand Up @@ -81,7 +81,7 @@ public function __wakeup(): void

public function onFixerFileProcessed(FileProcessed $event): void
{
$status = self::$eventStatusMap[$event->getStatus()];
$status = self::EVENT_STATUS_MAP[$event->getStatus()];
$this->getOutput()->write($this->getOutput()->isDecorated() ? \sprintf($status['format'], $status['symbol']) : $status['symbol']);

++$this->processedFiles;
Expand All @@ -108,9 +108,9 @@ public function printLegend(): void
{
$symbols = [];

foreach (self::$eventStatusMap as $status) {
foreach (self::EVENT_STATUS_MAP as $status) {
$symbol = $status['symbol'];
if ('' === $symbol || isset($symbols[$symbol])) {
if (isset($symbols[$symbol])) {
continue;
}

Expand Down
1 change: 1 addition & 0 deletions src/Console/Output/Progress/ProgressOutputFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function create(string $outputType, OutputContext $context): ProgressOutp
);
}

// @phpstan-ignore-next-line new.noConstructor
return new (self::OUTPUT_TYPE_MAP[$outputType])($context);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Fixer/Alias/RandomApiMigrationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
[$functionName, $openParenthesis, $closeParenthesis] = $boundaries;
$count = $argumentsAnalyzer->countArguments($tokens, $openParenthesis, $closeParenthesis);

\assert(isset(self::ARGUMENT_COUNTS[$functionIdentity])); // for PHPStan

if (!\in_array($count, self::ARGUMENT_COUNTS[$functionIdentity], true)) {
continue 2;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Fixer/Casing/MagicMethodCasingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,18 @@ 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::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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Fixer/Operator/LongToShorthandOperatorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ protected function isOperatorTokenCandidate(Tokens $tokens, int $index): bool

protected function getReplacementToken(Token $token): Token
{
\assert(isset(self::OPERATORS[$token->getContent()])); // for PHPStan

return new Token(self::OPERATORS[$token->getContent()]);
}
}
11 changes: 6 additions & 5 deletions src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,23 +321,24 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments
$arguments = $argumentsAnalyzer->getArguments($tokens, $testOpenIndex, $testCloseIndex);
$isPositive = 'asserttrue' === $assertCall['loweredName'];

if (\is_array(self::FIX_MAP[$content])) {
$expectedCount = self::FIX_MAP[$content]['argument_count'] ?? 1;
if (isset(self::FIX_MAP[$content]) && \is_array(self::FIX_MAP[$content])) {
$fixDetails = self::FIX_MAP[$content];
$expectedCount = $fixDetails['argument_count'] ?? 1;

if ($expectedCount !== \count($arguments)) {
return;
}

$isPositive = $isPositive ? 'positive' : 'negative';

if (false === self::FIX_MAP[$content][$isPositive]) {
if (false === $fixDetails[$isPositive]) {
return;
}

$tokens[$assertCall['index']] = new Token([T_STRING, self::FIX_MAP[$content][$isPositive]]);
$tokens[$assertCall['index']] = new Token([T_STRING, $fixDetails[$isPositive]]);
$this->removeFunctionCall($tokens, $testDefaultNamespaceTokenIndex, $testIndex, $testOpenIndex, $testCloseIndex);

if (self::FIX_MAP[$content]['swap_arguments'] ?? false) {
if ($fixDetails['swap_arguments'] ?? false) {
if (2 !== $expectedCount) {
throw new \RuntimeException('Can only swap two arguments, please update map or logic.');
}
Expand Down
9 changes: 9 additions & 0 deletions src/Runner/Event/FileProcessed.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,27 @@ final class FileProcessed extends Event
public const STATUS_EXCEPTION = 5;
public const STATUS_LINT = 6;

/**
* @var self::STATUS_*
*/
private int $status;

private ?string $fileRelativePath;
private ?string $fileHash;

/**
* @param self::STATUS_* $status
*/
public function __construct(int $status, ?string $fileRelativePath = null, ?string $fileHash = null)
{
$this->status = $status;
$this->fileRelativePath = $fileRelativePath;
$this->fileHash = $fileHash;
}

/**
* @return self::STATUS_*
*/
public function getStatus(): int
{
return $this->status;
Expand Down
9 changes: 5 additions & 4 deletions tests/Console/Output/Progress/ProgressOutputFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testValidProcessOutputIsCreated(
OutputContext $context,
string $expectedOutputClass
): void {
// @phpstan-ignore-next-line argument.type as we explicitly test non-valid $outputType
self::assertInstanceOf($expectedOutputClass, (new ProgressOutputFactory())->create($outputType, $context));
}

Expand All @@ -63,9 +64,9 @@ public function testExceptionIsThrownForUnsupportedProcessOutputType(): void
{
$this->expectException(\InvalidArgumentException::class);

(new ProgressOutputFactory())->create(
'boom',
new OutputContext(new SymfonyNullOutput(), 100, 10)
);
$outputContext = new OutputContext(new SymfonyNullOutput(), 100, 10);

// @phpstan-ignore-next-line argument.type as we explicitly test non-valid $outputType
(new ProgressOutputFactory())->create('boom', $outputContext);
}
}
4 changes: 1 addition & 3 deletions tests/Fixer/Operator/LongToShorthandOperatorFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ public static function provideFixCases(): iterable
];

// test simple with all operators

$reflection = new \ReflectionClass(LongToShorthandOperatorFixer::class);
$operators = $reflection->getStaticProperties()['operators'];
$operators = \Closure::bind(static fn (): array => LongToShorthandOperatorFixer::OPERATORS, null, LongToShorthandOperatorFixer::class)();

foreach ($operators as $operator => $info) {
$shortHand = $info[1];
Expand Down

0 comments on commit 3cc4349

Please sign in to comment.