Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/PhpGenerator/PhpNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public function addUseConstant(string $name, ?string $alias = null): static


/** @return string[] */
public function getUses(string $of = self::NameNormal): array
public function getUses(string $of = self::NameNormal, bool $psrSort = false): array
{
asort($this->aliases[$of]);
$psrSort ? uasort($this->aliases[$of], fn (string $first, string $second): int => strcasecmp(str_replace('\\', ' ', $first), str_replace('\\', ' ', $second))) : asort($this->aliases[$of]);
return array_filter(
$this->aliases[$of],
fn($name, $alias) => strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name),
Expand Down
7 changes: 5 additions & 2 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Printer
public string $indentation = "\t";
public int $linesBetweenProperties = 0;
public int $linesBetweenMethods = 2;
public int $linesBetweenNamespaceTypes = 0;
public bool $psrSortNamespaces = false;
public string $returnTypeColon = ': ';
public bool $bracesOnNextLine = true;
protected ?PhpNamespace $namespace = null;
Expand Down Expand Up @@ -256,7 +258,9 @@ public function printNamespace(PhpNamespace $namespace): string
$this->namespace = $this->resolveTypes ? $namespace : null;
$name = $namespace->getName();
$uses = $this->printUses($namespace)
. str_repeat("\n", $this->linesBetweenNamespaceTypes)
. $this->printUses($namespace, PhpNamespace::NameFunction)
. str_repeat("\n", $this->linesBetweenNamespaceTypes)
. $this->printUses($namespace, PhpNamespace::NameConstant);

$items = [];
Expand Down Expand Up @@ -305,9 +309,8 @@ protected function printUses(PhpNamespace $namespace, string $of = PhpNamespace:
PhpNamespace::NameFunction => 'function ',
PhpNamespace::NameConstant => 'const ',
][$of];
$name = $namespace->getName();
$uses = [];
foreach ($namespace->getUses($of) as $alias => $original) {
foreach ($namespace->getUses($of, $this->psrSortNamespaces) as $alias => $original) {
$uses[] = Helpers::extractShortName($original) === $alias
? "use $prefix$original;\n"
: "use $prefix$original as $alias;\n";
Expand Down
2 changes: 2 additions & 0 deletions src/PhpGenerator/PsrPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ final class PsrPrinter extends Printer
{
public string $indentation = ' ';
public int $linesBetweenMethods = 1;
public int $linesBetweenNamespaceTypes = 1;
public bool $psrSortNamespaces = true;
}