Skip to content

Commit 1a93fed

Browse files
committed
Merge branch 'depth-first-imports'
2 parents 7399699 + ea17df1 commit 1a93fed

File tree

78 files changed

+154
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+154
-126
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ c81cea1c82fb2b4c049939d0b4344ebbc29387f1
99
2c5640274a5e1f161dbe504c7d26989251400d26
1010
# Update formatting
1111
66d9a27e1c57650dc0166e42611b648c8af5e09d
12+
# Adopt depth-first sort order for imports
13+
331128cdc554df67264373f63c04611c563e53be

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
coverage: none
2828

2929
- name: Install PrettyPHP
30-
run: composer create-project --no-interaction --no-progress --no-dev lkrms/pretty-php=0.4.18 build/pretty-php
30+
run: composer create-project --no-interaction --no-progress --no-dev lkrms/pretty-php=0.4.22 build/pretty-php
3131

3232
- name: Run PrettyPHP
3333
run: build/pretty-php/bin/pretty-php --diff

lib/lk-util/Command/Concept/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Lkrms\LkUtil\Command\Concept;
44

5-
use Lkrms\Cli\CliCommand;
65
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
6+
use Lkrms\Cli\CliCommand;
77
use Lkrms\Contract\IProvider;
88
use Lkrms\Facade\File;
99
use Lkrms\LkUtil\Catalog\EnvVar;

lib/lk-util/Command/Generate/Concept/GenerateCommand.php

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace Lkrms\LkUtil\Command\Generate\Concept;
44

55
use Lkrms\Cli\Catalog\CliOptionType;
6+
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
67
use Lkrms\Cli\CliOption;
78
use Lkrms\Cli\CliOptionBuilder;
8-
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
99
use Lkrms\Facade\Composer;
1010
use Lkrms\Facade\Console;
1111
use Lkrms\Facade\File;
1212
use Lkrms\Facade\Reflect;
1313
use Lkrms\LkUtil\Command\Concept\Command;
1414
use Lkrms\Support\Catalog\RegularExpression as Regex;
15-
use Lkrms\Support\IntrospectionClass;
16-
use Lkrms\Support\Introspector;
1715
use Lkrms\Support\PhpDoc\PhpDoc;
1816
use Lkrms\Support\PhpDoc\PhpDocTag;
1917
use Lkrms\Support\PhpDoc\PhpDocTemplateTag;
18+
use Lkrms\Support\IntrospectionClass;
19+
use Lkrms\Support\Introspector;
2020
use Lkrms\Support\TokenExtractor;
2121
use Lkrms\Utility\Convert;
2222
use Lkrms\Utility\Test;
@@ -445,16 +445,26 @@ protected function generate(
445445
*/
446446
protected function generateImports(): array
447447
{
448-
$imports = [];
448+
$map = [];
449449
foreach ($this->ImportMap as $alias) {
450450
$import = $this->AliasMap[strtolower($alias)];
451451
if (!strcasecmp($alias, Convert::classToBasename($import))) {
452-
$imports[] = sprintf('use %s;', $import);
452+
$map[$import] = null;
453453
continue;
454454
}
455-
$imports[] = sprintf('use %s as %s;', $import, $alias);
455+
$map[$import] = $alias;
456+
}
457+
458+
// Sort by FQCN, depth-first
459+
uksort($map, fn($a, $b) => $this->getSortableFqcn($a) <=> $this->getSortableFqcn($b));
460+
461+
$imports = [];
462+
foreach ($map as $import => $alias) {
463+
$imports[] =
464+
$alias === null
465+
? sprintf('use %s;', $import)
466+
: sprintf('use %s as %s;', $import, $alias);
456467
}
457-
sort($imports);
458468

459469
return $imports;
460470
}
@@ -596,4 +606,39 @@ private function generatePhpDocBlock($phpDoc): array
596606
' */'
597607
];
598608
}
609+
610+
/**
611+
* Normalise a FQCN for depth-first sorting
612+
*
613+
* Before:
614+
*
615+
* ```
616+
* A
617+
* A\B\C
618+
* A\B
619+
* ```
620+
*
621+
* After:
622+
*
623+
* ```
624+
* 1A
625+
* 0A \ 0B \ 1C
626+
* 0A \ 1B
627+
* ```
628+
*/
629+
private function getSortableFqcn(string $import): string
630+
{
631+
$names = explode('\\', $import);
632+
$import = '';
633+
$prefix = 0;
634+
do {
635+
$name = array_shift($names);
636+
if (!$names) {
637+
$prefix = 1;
638+
}
639+
$import .= ($import === '' ? '' : ' \ ') . "$prefix$name";
640+
} while (!$prefix);
641+
642+
return $import;
643+
}
599644
}

lib/lk-util/Command/Generate/GenerateBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
namespace Lkrms\LkUtil\Command\Generate;
44

5-
use Closure;
65
use Lkrms\Cli\Catalog\CliOptionType;
76
use Lkrms\Cli\CliOption;
87
use Lkrms\Concept\Builder;
98
use Lkrms\Contract\IContainer;
109
use Lkrms\Facade\Reflect;
1110
use Lkrms\LkUtil\Catalog\EnvVar;
1211
use Lkrms\LkUtil\Command\Generate\Concept\GenerateCommand;
13-
use Lkrms\Support\Introspector;
1412
use Lkrms\Support\PhpDoc\PhpDoc;
1513
use Lkrms\Support\PhpDoc\PhpDocTemplateTag;
14+
use Lkrms\Support\Introspector;
1615
use Lkrms\Utility\Convert;
1716
use Lkrms\Utility\Test;
17+
use Closure;
1818
use ReflectionParameter;
1919
use ReflectionProperty;
2020

lib/lk-util/Command/Generate/GenerateFacade.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ final class GenerateFacade extends GenerateCommand
3030
'getReadable',
3131
'getWritable',
3232
'setFacade',
33-
3433
// These are displaced by Facade
3534
'isLoaded',
3635
'load',

lib/lk-util/Command/Generate/GenerateSyncEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
namespace Lkrms\LkUtil\Command\Generate;
44

5-
use DateTimeImmutable;
65
use Lkrms\Cli\Catalog\CliOptionType;
7-
use Lkrms\Cli\CliOption;
86
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
7+
use Lkrms\Cli\CliOption;
98
use Lkrms\LkUtil\Command\Generate\Concept\GenerateCommand;
109
use Lkrms\Support\Catalog\HttpRequestMethod;
1110
use Lkrms\Sync\Concept\HttpSyncProvider;
1211
use Lkrms\Sync\Concept\SyncEntity;
1312
use Lkrms\Utility\Convert;
1413
use Lkrms\Utility\Test;
14+
use DateTimeImmutable;
1515
use RuntimeException;
1616

1717
/**

lib/lk-util/Command/Generate/GenerateSyncProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Lkrms\LkUtil\Command\Generate;
44

55
use Lkrms\Cli\Catalog\CliOptionType;
6-
use Lkrms\Cli\CliOption;
76
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
7+
use Lkrms\Cli\CliOption;
88
use Lkrms\Facade\Sync;
99
use Lkrms\LkUtil\Catalog\EnvVar;
1010
use Lkrms\LkUtil\Command\Generate\Concept\GenerateCommand;

src/Auth/AccessToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Lkrms\Auth;
44

5-
use DateTimeImmutable;
6-
use DateTimeInterface;
75
use Lkrms\Concern\TFullyReadable;
86
use Lkrms\Contract\IReadable;
97
use Lkrms\Utility\Convert;
8+
use DateTimeImmutable;
9+
use DateTimeInterface;
1010

1111
/**
1212
* An immutable access token

src/Cli/CliApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Lkrms\Cli;
44

55
use Lkrms\Cli\Catalog\CliHelpSectionName;
6-
use Lkrms\Cli\CliCommand;
76
use Lkrms\Cli\Contract\ICliApplication;
87
use Lkrms\Cli\Exception\CliInvalidArgumentsException;
8+
use Lkrms\Cli\CliCommand;
99
use Lkrms\Console\ConsoleFormatter as Formatter;
1010
use Lkrms\Container\Application;
1111
use Lkrms\Facade\Assert;

0 commit comments

Comments
 (0)