Skip to content

[PostRector] Skip re-import name on callable name node#5022

Merged
TomasVotruba merged 19 commits intomasterfrom
fix-3383
Jan 7, 2021
Merged

[PostRector] Skip re-import name on callable name node#5022
TomasVotruba merged 19 commits intomasterfrom
fix-3383

Conversation

@samsonasik
Copy link
Copy Markdown
Member

Fixes #3383

@samsonasik samsonasik changed the title [PostRector] Skip import name on callable node [PostRector] Skip import name on callable name node Dec 28, 2020
@samsonasik samsonasik changed the title [PostRector] Skip import name on callable name node [PostRector] Skip re-import name on callable name node Dec 28, 2020
@TomasVotruba
Copy link
Copy Markdown
Member

This will need a failing test case first, so we're sure it's fixed

@samsonasik
Copy link
Copy Markdown
Member Author

yes, it works functional use function exists like in #3383, but it cause failure on not exists with auto-import in Rector\Renaming\Tests\Rector\Name\RenameClassRector\FunctionAutoImportNamesParameterTest::test

@samsonasik
Copy link
Copy Markdown
Member Author

we may need to replicate Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper::isFoundInUse():

private function isFoundInUse(Name $name): bool
{
/** @var Use_[] $uses */
$uses = (array) $name->getAttribute(AttributeKey::USE_NODES);
foreach ($uses as $use) {
$useUses = $use->uses;
foreach ($useUses as $useUse) {
if ($useUse->name instanceof Name && $useUse->name->getLast() === $name->getLast()) {
return true;
}
}
}
return false;
}

@samsonasik
Copy link
Copy Markdown
Member Author

still have 1 error:

1) Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector\ImportFullyQualifiedNamesRectorTest::test with data set #14 (Symplify\SmartFileSystem\SmartFileInfo Object (...))
rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Fixture/bootstrap_names.php.inc
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
 <?php
 
 namespace {
-    use function SomeNamespace\gc_disable;
     function run_me_never()
     {
         // silent deprecations, since we test them
@@ @@
         error_reporting(E_ALL ^ E_DEPRECATED);
 
         // performance boost
-        gc_disable();
+        \SomeNamespace\gc_disable();
     }
 }

/home/runner/work/rector/rector/packages/testing/src/PHPUnit/AbstractRectorTestCase.php:486
/home/runner/work/rector/rector/packages/testing/src/PHPUnit/AbstractRectorTestCase.php:308
/home/runner/work/rector/rector/rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/ImportFullyQualifiedNamesRectorTest.php:27

@samsonasik
Copy link
Copy Markdown
Member Author

finally fixed 🎉

@samsonasik
Copy link
Copy Markdown
Member Author

All checks have passed 🎉 @TomasVotruba it is ready for review.

Comment on lines +9 to +14
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::SETS, [SetList::DEAD_CODE]);
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@TomasVotruba TomasVotruba merged commit 524b578 into master Jan 7, 2021
@TomasVotruba TomasVotruba deleted the fix-3383 branch January 7, 2021 18:27
@bendavies
Copy link
Copy Markdown
Contributor

Still getting the error from #3383 unfortunately. I'll try to debug it now i can see your fix.

@samsonasik
Copy link
Copy Markdown
Member Author

@bendavies I tried with rector-functional-php repository and it working ok:

Screen Shot 2021-01-08 at 16 25 03

You can use dev-master as in the screenshot.

@bendavies
Copy link
Copy Markdown
Contributor

Thanks @samsonasik .

Definitely doesn't work on another project of mine, using the master version of rector.
I'll look into it.

@samsonasik
Copy link
Copy Markdown
Member Author

Just in case it by cache, you can run with --clear-cache:

vendor/bin/rector --clear-cache process

@TomasVotruba
Copy link
Copy Markdown
Member

@bendavies To save both yours and ours time, a GitHub repository with failing GitHub Action would be helpful.
That way, we'll see exactly where the problem is. And we all would know if the problem is solved or not.

@bendavies
Copy link
Copy Markdown
Contributor

bendavies commented Jan 8, 2021

hi @samsonasik i've replicated the bug again here:
https://github.com/bendavies/rector-functional-php

@TomasVotruba i have tried to reproduce in github actions in that repo but strangely it didn't replicate.

It errors locally to me.
I'd appreciate if someone could try locally.

I'll continue to try and work out why it errors locally for me.

@bendavies
Copy link
Copy Markdown
Contributor

I've also added a stack trace on that repo readme

@bendavies
Copy link
Copy Markdown
Contributor

well, adding

        if (function_exists($classLike)) {
            return true;
        }

to

public static function doesClassLikeExist(string $classLike): bool
fixes the issue for me.

@bendavies
Copy link
Copy Markdown
Contributor

bendavies commented Jan 8, 2021

I think the problem might be here?

&& ! function_exists($name->getLast())) {

Functional\each will do function_exists('each') which is always true, since each is a built in php function

@samsonasik
Copy link
Copy Markdown
Member Author

@bendavies I did a lot of simulation in that part :), that's used in rules, that's why that check was needed, could you provide a patch and add a fixture to https://github.com/rectorphp/rector/tree/master/tests/Issues/Issue3383/DoNotReImportFunction/Fixture for reproduce it? thank you.

@samsonasik
Copy link
Copy Markdown
Member Author

@bendavies I cloned your repo, re-try run vendor/bin/rector process and it working ok:

Screen Shot 2021-01-08 at 20 12 28

That maybe related with cache somewhere, please try clear cache composer as well.

@samsonasik
Copy link
Copy Markdown
Member Author

@bendavies you may need to remove vendor first too, as that something-something cache sometime there, remove composer.lock, composer clearcache, remove re-try composer install, and re-run vendor/bin/rector --clear-cache process

That's it, I can't reproduce it by using your repo. If you can provide reproduceable fixture, and provide a fix, that will be awesome ;)

@bendavies
Copy link
Copy Markdown
Contributor

@samsonasik thanks for trying. i'll try to get a reproduced in this repo.

TomasVotruba added a commit that referenced this pull request Sep 15, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP Fatal Error: Cannot redeclare Functional\*

3 participants