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

Conversation

Wirone
Copy link
Member

@Wirone Wirone commented Jan 10, 2024

Fixes #7652

@Wirone Wirone added the topic/fqcn Fully Qualified Class Name usage and conversions label Jan 10, 2024
@Wirone Wirone self-assigned this Jan 10, 2024
@mvorisek
Copy link
Contributor

in #7704 there is a fix already :)

@Wirone Wirone marked this pull request as ready for review January 10, 2024 00:21
@Wirone
Copy link
Member Author

Wirone commented Jan 10, 2024

@mvorisek Are you telling me I wasted my time for implementing this 😩? Eeehhhh....

@coveralls
Copy link

coveralls commented Jan 10, 2024

Coverage Status

coverage: 95.63% (-0.007%) from 95.637%
when pulling 375464b on Wirone:codito/7652-ignore-class-resolution-for-function
into 08bc02c on PHP-CS-Fixer:master.

if ($this->cacheUsesLast === $uses) {
return;
}
foreach ($uses as $kind => $kindUses) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the foreach must be put right before the other foreach

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you explain a bit more?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is evident, the lines after the first foreach are unrelated to the loop, ie. must be placed before the loop.

Copy link
Member Author

Choose a reason for hiding this comment

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

Still don't get it, sorry. Can you provide it as a suggestion (diff)?

This comment was marked as outdated.

Previously all imports in the processed namespace were flattened to one list and use later for shortening. We need to have imports list sliced by import kind, so we can use proper imports in the context of currently processed part of code (e.g. use only class imports when processing function arguments).
@Wirone Wirone force-pushed the codito/7652-ignore-class-resolution-for-function branch from 68491f7 to 2811185 Compare May 30, 2024 15:19
Comment on lines +206 to +207
/** @var class-string $fqn */
$fqn = $fullName;
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 😕.

if ($this->cacheUsesLast === $uses) {
return;
}
foreach ($uses as $kind => $kindUses) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Can you explain a bit more?

@Wirone Wirone force-pushed the codito/7652-ignore-class-resolution-for-function branch from e8bf8bb to 375464b Compare May 30, 2024 15:36
@Wirone
Copy link
Member Author

Wirone commented May 30, 2024

@kubawerlos do you have an idea why this fails? It imports and shortens CoversClass like here, but at the same time leaves FQCN like here 😕. I need to go now, will be AFK until late evening, if anyone can take a look I will be grateful.

@mvorisek
Copy link
Contributor

@kubawerlos do you have an idea why this fails? It imports and shortens CoversClass like here, but at the same time leaves FQCN like here 😕. I need to go now, will be AFK until late evening, if anyone can take a look I will be grateful.

@Wirone I guess the PHPUnit48MigrationRiskySetTest does not include the FullyQualifiedStrictTypes fixer rule :)

@Wirone
Copy link
Member Author

Wirone commented May 30, 2024

@mvorisek I've added this rule explicitly to the "highest" config, but it sometimes works and sometimes not 🤔.

@kubawerlos
Copy link
Contributor

Well, it looks fine on my PC 😁

I see in the job's log:

2024-05-30T15:37:38.9384682Z   79) tests/RuleSet/Sets/PSR12RiskySetTest.php
2024-05-30T15:37:38.9384841Z       ---------- begin diff ----------
2024-05-30T15:37:38.9385237Z --- /home/runner/work/PHP-CS-Fixer/PHP-CS-Fixer/tests/RuleSet/Sets/PSR12RiskySetTest.php
2024-05-30T15:37:38.9385637Z +++ /home/runner/work/PHP-CS-Fixer/PHP-CS-Fixer/tests/RuleSet/Sets/PSR12RiskySetTest.php
2024-05-30T15:37:38.9385766Z @@ -14,9 +14,10 @@
2024-05-30T15:37:38.9385856Z  
2024-05-30T15:37:38.9386008Z  namespace PhpCsFixer\Tests\RuleSet\Sets;
2024-05-30T15:37:38.9386097Z  
2024-05-30T15:37:38.9386246Z +use PhpCsFixer\RuleSet\Sets\PSR12RiskySet;
2024-05-30T15:37:38.9386337Z +
2024-05-30T15:37:38.9386427Z  /**
2024-05-30T15:37:38.9386531Z   * @internal
2024-05-30T15:37:38.9386642Z - *
2024-05-30T15:37:38.9386854Z - * @covers \PhpCsFixer\RuleSet\Sets\PSR12RiskySet
2024-05-30T15:37:38.9386948Z   */
2024-05-30T15:37:38.9387193Z +#[\PHPUnit\Framework\Attributes\CoversClass(PSR12RiskySet::class)]
2024-05-30T15:37:38.9387421Z  final class PSR12RiskySetTest extends AbstractSetTestCase {}
2024-05-30T15:37:38.9387426Z 
2024-05-30T15:37:38.9387577Z       ----------- end diff -----------

but when I run locally from the branch I have:

kub@:~/code/friendsofphp/PHP-CS-Fixer(codito/7652-ignore-class-resolution-for-function)$ php php-cs-fixer fix --diff --config .php-cs-fixer.php-highest.php tests/RuleSet/Sets/PSR12RiskySetTest.php --dry-run
PHP CS Fixer 3.58.2-DEV 7th Gear by Fabien Potencier, Dariusz Ruminski and contributors.
PHP runtime: 8.3.7
Running analysis on 8 cores with 10 files per process.
Parallel runner is an experimental feature and may be unstable, use it at your own risk. Feedback highly appreciated!
Loaded config default from ".php-cs-fixer.php-highest.php".
Using cache file ".php-cs-fixer.cache".
Paths from configuration file have been overridden by paths provided as command arguments.
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

   1) tests/RuleSet/Sets/PSR12RiskySetTest.php
      ---------- begin diff ----------
--- /home/kuba/code/friendsofphp/PHP-CS-Fixer/tests/RuleSet/Sets/PSR12RiskySetTest.php
+++ /home/kuba/code/friendsofphp/PHP-CS-Fixer/tests/RuleSet/Sets/PSR12RiskySetTest.php
@@ -14,9 +14,11 @@

 namespace PhpCsFixer\Tests\RuleSet\Sets;

+use PhpCsFixer\RuleSet\Sets\PSR12RiskySet;
+use PHPUnit\Framework\Attributes\CoversClass;
+
 /**
  * @internal
- *
- * @covers \PhpCsFixer\RuleSet\Sets\PSR12RiskySet
  */
+#[CoversClass(PSR12RiskySet::class)]
 final class PSR12RiskySetTest extends AbstractSetTestCase {}

      ----------- end diff -----------

and I would expect that in CI here too

@kubawerlos
Copy link
Contributor

I guess we can move updating .php-cs-fixer.php-highest.php to separate PR and get the fix done.

@Wirone
Copy link
Member Author

Wirone commented May 30, 2024

@kubawerlos it works differently when you apply the "highest" ruleset to a single file or to the whole codebase. The CI outcome can be reproduced locally by running the exact command that is run in the CI, and the running composer cs:check - some files have FQCNs in attributes 😩. I don't understand why, PHPUnit's rule has higher priority and is applied before fully_qualified_strict_types, so the added attribute should be fixed. It does not make sense because some files are fixed fully, some skip the FQCN import step 🤷‍♂️.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic/fqcn Fully Qualified Class Name usage and conversions
Projects
None yet
4 participants