Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not shorten FQN for class resolution if imported symbol is not a class #7705

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ jobs:
if: env.PHP_MAX == matrix.php-version && matrix.run-migration-rules == 'yes'
env:
PHP_CS_FIXER_FUTURE_MODE: 1
run: php php-cs-fixer fix --config .php-cs-fixer.php-highest.php -q
run: php php-cs-fixer fix --diff --config .php-cs-fixer.php-highest.php

- name: Custom PHPUnit config with v10 alignments
if: matrix.collect-code-coverage == 'yes' || matrix.run-mutation-tests
Expand Down
1 change: 1 addition & 0 deletions .php-cs-fixer.php-highest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
$config->setRules(array_merge($config->getRules(), [
'@PHP83Migration' => true,
'@PHP80Migration:risky' => true,
'fully_qualified_strict_types' => ['import_symbols' => true],
'php_unit_attributes' => true,
]));

Expand Down
12 changes: 0 additions & 12 deletions dev-tools/phpstan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,12 @@
'count' => 2,
'path' => __DIR__ . '/../../src/Fixer/Import/FullyQualifiedStrictTypesFixer.php',
];
$ignoreErrors[] = [
// identifier: assign.propertyType
'message' => '#^Property PhpCsFixer\\\\Fixer\\\\Import\\\\FullyQualifiedStrictTypesFixer\\:\\:\\$discoveredSymbols \\(array\\{const\\?\\: list\\<class\\-string\\>, class\\?\\: list\\<class\\-string\\>, function\\?\\: list\\<class\\-string\\>\\}\\|null\\) does not accept array\\{const\\?\\: list\\<class\\-string\\>, class\\: non\\-empty\\-list\\<string\\>, function\\?\\: list\\<class\\-string\\>\\}\\.$#',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Import/FullyQualifiedStrictTypesFixer.php',
];
$ignoreErrors[] = [
// identifier: assign.propertyType
'message' => '#^Property PhpCsFixer\\\\Fixer\\\\Import\\\\FullyQualifiedStrictTypesFixer\\:\\:\\$reservedIdentifiersByLevel \\(array\\<int\\<0, max\\>, array\\<string, true\\>\\>\\) does not accept non\\-empty\\-array\\<int, array\\<string, true\\>\\>\\.$#',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Import/FullyQualifiedStrictTypesFixer.php',
];
$ignoreErrors[] = [
// identifier: assign.propertyType
'message' => '#^Property PhpCsFixer\\\\Fixer\\\\Import\\\\FullyQualifiedStrictTypesFixer\\:\\:\\$symbolsForImport \\(array\\{const\\?\\: array\\<string, class\\-string\\>, class\\?\\: array\\<string, class\\-string\\>, function\\?\\: array\\<string, class\\-string\\>\\}\\) does not accept array\\{const\\?\\: array\\<string, string\\>, class\\?\\: array\\<string, string\\>, function\\?\\: array\\<string, string\\>\\}\\.$#',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Import/FullyQualifiedStrictTypesFixer.php',
];
$ignoreErrors[] = [
// identifier: argument.type
'message' => '#^Parameter \\#1 \\$types of method PhpCsFixer\\\\DocBlock\\\\Annotation\\:\\:setTypes\\(\\) expects list\\<string\\>, array\\<int\\<0, max\\>, string\\> given\\.$#',
Expand Down
3 changes: 3 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ parameters:
# See: https://github.com/phpstan/phpstan/discussions/9686
- '/^Parameter #1 \$configuration \(.*\) of method PhpCsFixer\\Fixer\\.*::configure\(\) should be contravariant with parameter \$configuration \(array<string, mixed>\) of method PhpCsFixer\\(AbstractFixer|Fixer\\ConfigurableFixerInterface)+::configure\(\)$/'

# We retrieve these FQNs in various ways, we process them along the way, let's assume it's always class-string
- '/^Parameter #2 \$fullName of class PhpCsFixer\\Tokenizer\\Analyzer\\Analysis\\NamespaceUseAnalysis constructor expects class-string, string given\.$/'

# PHPUnit data providers return type were not maintained originally, this exception should shrink over time (eg with help of custom, re-usable type)
-
message: '#^Method PhpCsFixer\\Tests\\.+::provide.+Cases\(\) return type has no value type specified in iterable type iterable\.$#'
Expand Down
167 changes: 107 additions & 60 deletions src/Fixer/Import/FullyQualifiedStrictTypesFixer.php

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/Tokenizer/Analyzer/Analysis/NamespaceUseAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @author Greg Korba <greg@codito.dev>
*
* @internal
*
* @phpstan-type _ImportType 'class'|'constant'|'function'
*/
final class NamespaceUseAnalysis implements StartEndTokenAwareAnalysis
{
Expand All @@ -28,6 +30,8 @@ final class NamespaceUseAnalysis implements StartEndTokenAwareAnalysis

/**
* The fully qualified use namespace.
*
* @var class-string
*/
private string $fullName;

Expand Down Expand Up @@ -73,8 +77,16 @@ final class NamespaceUseAnalysis implements StartEndTokenAwareAnalysis
*/
private int $type;

/** @var array<self::TYPE_*, _ImportType> */
private static array $typesMap = [
self::TYPE_CLASS => 'class',
self::TYPE_FUNCTION => 'function',
self::TYPE_CONSTANT => 'constant',
];

/**
* @param self::TYPE_* $type
* @param class-string $fullName
*/
public function __construct(
int $type,
Expand Down Expand Up @@ -102,6 +114,9 @@ public function __construct(
$this->chunkEndIndex = $chunkEndIndex;
}

/**
* @return class-string
*/
public function getFullName(): string
{
return $this->fullName;
Expand Down Expand Up @@ -150,6 +165,14 @@ public function getType(): int
return $this->type;
}

/**
* @return _ImportType
*/
public function getHumanFriendlyType(): string
{
return self::$typesMap[$this->type];
}

public function isClass(): bool
{
return self::TYPE_CLASS === $this->type;
Expand Down
9 changes: 6 additions & 3 deletions src/Tokenizer/Analyzer/NamespaceUsesAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function parseDeclarations(int $startIndex, int $endIndex, Tokens $token
$groupQualifiedName = $this->getNearestQualifiedName($tokens, $chunkStart);
$imports[] = new NamespaceUseAnalysis(
$type,
$qualifiedName['fullName'].$groupQualifiedName['fullName'],
$qualifiedName['fullName'].$groupQualifiedName['fullName'], // @phpstan-ignore argument.type
$groupQualifiedName['shortName'],
$groupQualifiedName['aliased'],
true,
Expand Down Expand Up @@ -171,7 +171,7 @@ private function determineImportType(Tokens $tokens, int $startIndex): int
}

/**
* @return array{fullName: string, shortName: string, aliased: bool, afterIndex: int}
* @return array{fullName: class-string, shortName: string, aliased: bool, afterIndex: int}
*/
private function getNearestQualifiedName(Tokens $tokens, int $index): array
{
Expand Down Expand Up @@ -203,8 +203,11 @@ private function getNearestQualifiedName(Tokens $tokens, int $index): array
$index = $tokens->getNextMeaningfulToken($index);
}

/** @var class-string $fqn */
$fqn = $fullName;
Comment on lines +206 to +207
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do that because of PHPStan, which expects class-string, but infers string from the code above (rightfully), and I couldn't do /** @var class-string $fullName */ before return because Fixer wants to change it to // comment and then PHPStan can't see the enforced type 😕.


return [
'fullName' => $fullName,
'fullName' => $fqn,
'shortName' => $shortName,
'aliased' => $aliased,
'afterIndex' => $index,
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixer/Import/FullyQualifiedStrictTypesFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ function test(\Foo\Bar $x) {}',
function test(int $x): void {}',
];

yield 'ignore class resolution shortening when imported symbol is a function' => [
'<?php

use function Symfony\Component\String\u;

echo Symfony\Component\String\u::class;',
];

yield 'namespace cases' => [
'<?php

Expand Down