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

Feature request: When sorting use statements, match the sort order typically used by php-cs-fixer #105

ezzatron opened this Issue Sep 20, 2017 · 3 comments


None yet
3 participants
Copy link

ezzatron commented Sep 20, 2017

When adding use statements to a file as a result of find_use, PHP Companion inserts the use statement where it should appear in alphabetical order (seemingly using the top-most namespace only?).

This is pretty good already, but it's not a perfect fit to how use statements are ordered by PHP-CS-Fixer's "ordered_imports" fixer.

The exact logic they use is (from here):

    private function sortAlphabetically(array $first, array $second)
        if ($first['importType'] !== $second['importType']) {
            return $first['importType'] > $second['importType'] ? 1 : -1;

        // Replace backslashes by spaces before sorting for correct sort order
        $firstNamespace = str_replace('\\', ' ', $this->prepareNamespace($first['namespace']));
        $secondNamespace = str_replace('\\', ' ', $this->prepareNamespace($second['namespace']));

        return strcasecmp($firstNamespace, $secondNamespace);

So the important details here are:

  1. The sort is case-insensitive, whereas PHP Companion appears to sort in a case-sensitive manner, putting uppercase letters above lowercase.
  2. Rather than sorting just the first part of a namespace, backslashes are replaced with spaces before sorting the entire string.
  3. Different import "types" (use, use function, use const) are treated as different lists for sorting. Another way of saying this is that the use statements are sorted by type first. This is irrelevant to PHP Companion right now, but would relate to #104 if implemented.

This comment has been minimized.

Copy link

erichard commented Jan 10, 2018

I think PHP Companion should not care of the use sorting. php-cs-fixer does that pretty well, no need to reinvent the wheel.

@erichard erichard added the wontfix label Jan 10, 2018

@erichard erichard closed this Jan 10, 2018


This comment has been minimized.

Copy link

arxeiss commented Jan 18, 2019

@erichard I don't understand why this is closed... In our new project, company set rules and one of them is sorting use alphabetically but case insensitive. Now I cannot use your tool, because there are permanent changes in git.

Why this package should not contain option to turn on/off case insensitive sorting?


This comment has been minimized.

Copy link

erichard commented Jan 18, 2019

@arxeiss PHP Companion need a default sorting algorithm but not a full sorting feature that php-cs-fixer does very well.

You might want to try PHPCS plugin to execute php-cs-fixer on save and get rid of your git problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment