Skip to content

Commit b95f7fd

Browse files
authored
Printer: added $linesBetweenUseTypes (#114)
1 parent f19b797 commit b95f7fd

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

src/PhpGenerator/PhpNamespace.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ public function addUseConstant(string $name, ?string $alias = null): static
160160

161161

162162
/** @return string[] */
163-
public function getUses(string $of = self::NameNormal): array
163+
public function getUses(string $of = self::NameNormal, bool $psrSort = false): array
164164
{
165-
asort($this->aliases[$of]);
165+
$psrSort ? uasort($this->aliases[$of], fn (string $first, string $second): int => strcasecmp(str_replace('\\', ' ', $first), str_replace('\\', ' ', $second))) : asort($this->aliases[$of]);
166166
return array_filter(
167167
$this->aliases[$of],
168168
fn($name, $alias) => strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name),

src/PhpGenerator/Printer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Printer
2424
public string $indentation = "\t";
2525
public int $linesBetweenProperties = 0;
2626
public int $linesBetweenMethods = 2;
27+
public int $linesBetweenNamespaceTypes = 0;
28+
public bool $psrSortNamespaces = false;
2729
public string $returnTypeColon = ': ';
2830
public bool $bracesOnNextLine = true;
2931
protected ?PhpNamespace $namespace = null;
@@ -256,7 +258,9 @@ public function printNamespace(PhpNamespace $namespace): string
256258
$this->namespace = $this->resolveTypes ? $namespace : null;
257259
$name = $namespace->getName();
258260
$uses = $this->printUses($namespace)
261+
. str_repeat("\n", $this->linesBetweenNamespaceTypes)
259262
. $this->printUses($namespace, PhpNamespace::NameFunction)
263+
. str_repeat("\n", $this->linesBetweenNamespaceTypes)
260264
. $this->printUses($namespace, PhpNamespace::NameConstant);
261265

262266
$items = [];
@@ -305,9 +309,8 @@ protected function printUses(PhpNamespace $namespace, string $of = PhpNamespace:
305309
PhpNamespace::NameFunction => 'function ',
306310
PhpNamespace::NameConstant => 'const ',
307311
][$of];
308-
$name = $namespace->getName();
309312
$uses = [];
310-
foreach ($namespace->getUses($of) as $alias => $original) {
313+
foreach ($namespace->getUses($of, $this->psrSortNamespaces) as $alias => $original) {
311314
$uses[] = Helpers::extractShortName($original) === $alias
312315
? "use $prefix$original;\n"
313316
: "use $prefix$original as $alias;\n";

src/PhpGenerator/PsrPrinter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ final class PsrPrinter extends Printer
1717
{
1818
public string $indentation = ' ';
1919
public int $linesBetweenMethods = 1;
20+
public int $linesBetweenNamespaceTypes = 1;
21+
public bool $psrSortNamespaces = true;
2022
}

0 commit comments

Comments
 (0)