Rector: Use placements?

Created on 18 Nov 2019  路  6Comments  路  Source: rectorphp/rector

Question

Is it possible to have rector respect the current placements of the use

e.g.

<?php
namespace AOE\Crawler\Api;

use Exception;
use PDO;
/***************************************************************
 *  Copyright notice
 ***************************************************************/

use AOE\Crawler\Controller\CrawlerController;

We can discuss where the right location for use is, but I would prefer having the missing use added to the already places uses.

Is this possible ?

All 6 comments

It should be possible. Rector just didn't refactor use statements with comments.

Rector now adds new use imports as the first nodes, here:

https://github.com/rectorphp/rector/blob/47025a95ee58562ec9b477ce9e8cd4deb453e2d6/packages/CodingStyle/src/Application/UseImportsAdder.php#L80

To add it as last after existing use statements, you'd need to foreach $namespace->stmts and instanceof Use_:

$wasAdded = false;

foreach ($namespace->stmts as $key => $stmt)
{
    if (! $stmt instanceof Use_) {
        continue;
    }

    // add use statements now
    array_splice($namespce->stmts, $key, 0, $newUses);
    $wasAdded = true;
}

// fallback to previous approach, if no stmts were added
if ($wasAdded === false) {
    $namespace->stmts = array_merge($newUses, $namespace->stmts);
}

This is just almsot pseudo code, from top my head. If you test it, tune it and it works, please send PR.
Thank you :+1:

Thanks will test it out one of the following days.

Hi @TomasVotruba
I have tested your example, with a few changes.

$wasAdded = false;

foreach ($namespace->stmts as $key => $stmt) {
    if (! $stmt instanceof Use_) {
        continue;
    }

    // add use statements now
    array_splice($namespace->stmts, $key + 1, 0, $newUses);
    $wasAdded = true;
}

// fallback to previous approach, if no stmts were added
if ($wasAdded === false) {
    $namespace->stmts = array_merge($newUses, $namespace->stmts);
}

The $key + 1 ensures that they are added after the current use statements, but I'm facing two issue atm.

  1. The use-statements are not sorted, would prefer them alphabetically
  2. Classes like the fixture packages/NetteToSymfony/tests/Rector/ClassMethod/RouterListToControllerAnnotationsRetor/Fixture/new_route_to_annotation.php.inc with to classes in the file, leads to problems with the placement of the use-statements

Do you have any suggestions on how to fix these two issues?

  1. foreach stmts that are Use_ and sort them by name

  2. If you send PR and test fails, I could tell you more

I have pushed my WIP, looks like I'm wrong regarding sort, it looks like it already happens correctly.

And only the fixtures need to be adjusted, will check this out.

The second part with the more classes in one file, I'll need to check as well.. Input will be welcome though :)

Thanks for helping out.

I'm looking at it now more closely. After bit of work, I've found this is not possible to handle for the user.

The comment belong to first use statements. Imagine this case:

/***************************************************************
 *  First use commetn
 ***************************************************************/
use AOE\Crawler\Controller\CrawlerController;
/***************************************************************
 * Second use commetn
 ***************************************************************/
use AOE\Crawler\Controller\CrawlerController;

Here adding use in specific position would not help..

The comment has to bellong to unique elements, like class or namespace, e.g.:

/***************************************************************
 *  First use commetn
 ***************************************************************/
namespace App;

/***************************************************************
 * Second use commetn
 ***************************************************************/
class SomeClass
{
}

Anything else will screw coments above use statments one way or another.

Saying that, I'm closing this issue and PR. Thank you for you work though, it helped me to discover this problem :+1:

Was this page helpful?
0 / 5 - 0 ratings