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

General pretty printing guidance #6146

Closed
jdart opened this issue Apr 15, 2021 · 9 comments · Fixed by #6159
Closed

General pretty printing guidance #6146

jdart opened this issue Apr 15, 2021 · 9 comments · Fixed by #6159
Labels

Comments

@jdart
Copy link

jdart commented Apr 15, 2021

Question

I saw the part of the readme with a link to your ecs.php and tried it out, but it didn't fix most of the unwanted changes and added more. Do you know of a fixer that will remove leading slashes on global functions and things like parent, and true? Do you know of a fixer that will replace a fqn with a class name if it's imported? It would be really great to provide a minimal ecs config that fixes common things php parser changes, maybe that's impossible.

        \parent::setUp();
        \app();
        // not using imported class, instead using fqn
        \true

@TomasVotruba
Copy link
Member

Hi, thanks for reporting.

The parent and true should not be prefixed with \\ by Rector.

Could you provide minimal failing code snippet that is causing it? https://getrector.org/demo

@jdart
Copy link
Author

jdart commented Apr 16, 2021

I'm not able to reproduce there but I should mention I'm using the prefixed build (RectorPrefix20210414).

@jdart
Copy link
Author

jdart commented Apr 16, 2021

I tried reproducing with a fresh composer.json with the prefixed build and was again unable to reproduce so there must be something funny with my dependencies. Any ideas for troubleshooting?

@TomasVotruba
Copy link
Member

Hi, I would copy paste the latest patch version from your test repository to you main repository.

E.g.

-rector/rector:^0.10
+rector/rector:^0.10.5

for all other packages too

@jdart
Copy link
Author

jdart commented Apr 16, 2021

I believe I'm able to reproduce now...

composer.json

{
    "name": "jdart/rekt",
    "authors": [
    ],
    "require": {},
    "require-dev": {
        "rector/rector-prefixed": "^0.10.6"
    },
    "autoload": {
        "psr-4": {
          "App\\": "app/"
        }
    }
}

app/ThingTest.php

<?php

declare(strict_types=1);

class BaseC {
    public function setup(): void
    {
    }
}

class Extender extends BaseC
{
    public function setup(): void
    {
        parent::setup();
    }
}

rector.php

<?php

declare(strict_types=1);

return static function (\RectorPrefix20210414\Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services();
    $services->set(\Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector::class);
};

Output:

❯ ./vendor/bin/rector process --debug --clear-cache app/ThingTest.php
[parsing] app/ThingTest.php
[refactoring] app/ThingTest.php
    [applying] Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector
arstarstarst[post rectors] app/ThingTest.php
[printing] app/ThingTest.php


1 file with changes
===================

1) app/ThingTest.php

    ---------- begin diff ----------
--- Original
+++ New
@@ @@
 <?php

 declare(strict_types=1);
+namespace App;

 class BaseC {
     public function setup(): void
@@ @@
     {
     }
 }
-class Extender extends BaseC
+class Extender extends \BaseC
 {
     public function setup(): void
     {
-        parent::setup();
+        \parent::setup();
     }
 }
    ----------- end diff -----------


Applied rules:

 * NormalizeNamespaceByPSR4ComposerAutoloadRector



 [OK] 1 file has been changed by Rector

@TomasVotruba
Copy link
Member

I see, thanks for tests. I've added a test fixture to NormalizeNamespaceByPSR4ComposerAutoloadRector

@TomasVotruba
Copy link
Member

Fixed in #6159 👍

@jdart
Copy link
Author

jdart commented Apr 17, 2021

Thanks @TomasVotruba

BTW would you happen to know of a fixer that will replace a fqn with a class name if it's imported? I.e. after rector process runs it seems like all class names are replaced with the FQN even if the class is imported.

@TomasVotruba
Copy link
Member

Try auto import parameter

TomasVotruba added a commit that referenced this issue Jul 14, 2024
rectorphp/rector-src@a9ddf8f  [Reflection] Clean up unnecessary loop parents on ReflectionResolver  (#6146)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants