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
9 changes: 5 additions & 4 deletions src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@

use InvalidArgumentException;
use Override;
use Stringable;

final readonly class Alias implements Stringable
final readonly class Alias implements Importable
{
public string $alias;
public FullyQualified | FunctionName | NamespaceName $target;
public Importable $target;

public function __construct(
string $alias,
FullyQualified | FunctionName | NamespaceName $target,
Importable $target,
) {
$alias = trim($alias);

Expand All @@ -37,13 +36,15 @@ public function __toString() : string
return sprintf('%s as %s', $this->target, $this->alias);
}

#[Override]
public function equals(object $other) : bool
{
return $other instanceof self
&& $this->alias === $other->alias
&& $this->target->equals($other->target);
}

#[Override]
public function compare(object $other) : int
{
// Aliases should sort by their target, not their alias name
Expand Down
9 changes: 5 additions & 4 deletions src/ClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use InvalidArgumentException;
use Override;
use Stringable;

final readonly class ClassName implements Stringable
final readonly class ClassName implements Importable
{
/**
* @var non-empty-string
Expand All @@ -34,7 +33,7 @@ public function __construct(
/**
* @phpstan-return ($input is null ? null : self)
*/
public static function maybeFromString(null | self | string $input) : ?self
public static function maybeFromString(null | Importable | self | string $input) : ?self
{
if ($input === null) {
return null;
Expand All @@ -44,7 +43,7 @@ public static function maybeFromString(null | self | string $input) : ?self
return $input;
}

return new self($input);
return new self((string) $input);
}

#[Override]
Expand All @@ -53,11 +52,13 @@ public function __toString() : string
return $this->name;
}

#[Override]
public function equals(object $other) : bool
{
return $other instanceof self && $this->name === $other->name;
}

#[Override]
public function compare(object $other) : int
{
if ($other instanceof self) {
Expand Down
8 changes: 4 additions & 4 deletions src/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function maybeDump(
/**
* Finds an available alias for a type, appending numbers if the alias is already taken
*/
private function findAvailableAlias(Alias | FullyQualified | FunctionName | NamespaceName $type, string $alias, int $i = 1) : string
private function findAvailableAlias(Importable $type, string $alias, int $i = 1) : string
{
$aliasToCheck = $i === 1 ? $alias : sprintf('%s%d', $alias, $i);

Expand Down Expand Up @@ -188,7 +188,7 @@ public function importEnum(UnitEnum $enum) : string
/**
* Imports a class, namespace, or function and returns the alias to use in the generated code
*/
public function import(FullyQualified | FunctionName | NamespaceName | string $fqcnOrEnum) : string
public function import(Importable | string $fqcnOrEnum) : string
{
if ($fqcnOrEnum instanceof FunctionName) {
$alias = $this->findAvailableAlias($fqcnOrEnum, $fqcnOrEnum->shortName);
Expand All @@ -214,7 +214,7 @@ public function import(FullyQualified | FunctionName | NamespaceName | string $f
/**
* Imports a class by importing its parent namespace and returning the relative path
*/
public function importByParent(FullyQualified | string $name) : string
public function importByParent(Importable | string $name) : string
{
$fqcn = FullyQualified::maybeFromString($name);

Expand All @@ -231,7 +231,7 @@ public function importByParent(FullyQualified | string $name) : string
// Import the namespace and return the alias with class name
return (string) new FullyQualified(
$this->import($fqcn->namespace),
(string) $fqcn->className,
$fqcn->className,
);
}

Expand Down
25 changes: 15 additions & 10 deletions src/FullyQualified.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@

use InvalidArgumentException;
use Override;
use Stringable;

final readonly class FullyQualified implements Stringable
final readonly class FullyQualified implements Importable
{
public ClassName $className;
public ?NamespaceName $namespace;

public function __construct(
string $part,
string ...$parts,
Importable | string $part,
Importable | string ...$parts,
) {
$flattened = array_filter(
explode('\\', implode('\\', [$part, ...$parts])),
explode(
'\\',
implode(
'\\',
array_map(strval(...), [$part, ...$parts]),
),
),
fn($p) => $p !== '',
);

Expand All @@ -29,15 +34,13 @@ public function __construct(
$classNamePart = array_pop($flattened);
$this->className = new ClassName($classNamePart);

$this->namespace = $flattened !== []
? new NamespaceName(implode('\\', $flattened))
: null;
$this->namespace = $flattened !== [] ? new NamespaceName(implode('\\', $flattened)) : null;
}

/**
* @phpstan-return ($input is null ? null : self)
*/
public static function maybeFromString(null | self | string $input) : ?self
public static function maybeFromString(null | Importable | self | string $input) : ?self
{
if ($input === null) {
return null;
Expand All @@ -47,7 +50,7 @@ public static function maybeFromString(null | self | string $input) : ?self
return $input;
}

return new self($input);
return new self((string) $input);
}

#[Override]
Expand All @@ -60,11 +63,13 @@ public function __toString() : string
return $this->namespace . '\\' . $this->className;
}

#[Override]
public function equals(object $other) : bool
{
return $other instanceof self && (string) $this === (string) $other;
}

#[Override]
public function compare(object $other) : int
{
$thisStr = str_replace('\\', ' ', (string) $this);
Expand Down
9 changes: 5 additions & 4 deletions src/FunctionName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use InvalidArgumentException;
use Override;
use Stringable;

final class FunctionName implements Stringable
final class FunctionName implements Importable
{
public readonly string $name;

Expand Down Expand Up @@ -38,7 +37,7 @@ public function __construct(
/**
* @phpstan-return ($input is null ? null : self)
*/
public static function maybeFromString(null | self | string $input) : ?self
public static function maybeFromString(null | Importable | self | string $input) : ?self
{
if ($input === null) {
return null;
Expand All @@ -48,7 +47,7 @@ public static function maybeFromString(null | self | string $input) : ?self
return $input;
}

return new self($input);
return new self((string) $input);
}

#[Override]
Expand All @@ -57,11 +56,13 @@ public function __toString() : string
return 'function ' . $this->name;
}

#[Override]
public function equals(object $other) : bool
{
return $other instanceof self && (string) $this === (string) $other;
}

#[Override]
public function compare(object $other) : int
{
$thisStr = str_replace('\\', ' ', $this->name);
Expand Down
23 changes: 23 additions & 0 deletions src/Importable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Ruudk\CodeGenerator;

use Stringable;

/**
* Interface for classes that can be imported in code generation
*/
interface Importable extends Stringable
{
/**
* Check if this importable is equal to another object
*/
public function equals(object $other) : bool;

/**
* Compare this importable with another object for sorting
*/
public function compare(object $other) : int;
}
9 changes: 5 additions & 4 deletions src/NamespaceName.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use InvalidArgumentException;
use Override;
use Stringable;

final class NamespaceName implements Stringable
final class NamespaceName implements Importable
{
/**
* @var non-empty-string
Expand Down Expand Up @@ -45,7 +44,7 @@ public function __construct(
/**
* @phpstan-return ($input is null ? null : self)
*/
public static function maybeFromString(null | self | string $input) : ?self
public static function maybeFromString(null | Importable | self | string $input) : ?self
{
if ($input === null) {
return null;
Expand All @@ -55,7 +54,7 @@ public static function maybeFromString(null | self | string $input) : ?self
return $input;
}

return new self($input);
return new self((string) $input);
}

#[Override]
Expand All @@ -72,11 +71,13 @@ public function with(string $part, string ...$parts) : self
return new self($this->namespace, $part, ...$parts);
}

#[Override]
public function equals(object $other) : bool
{
return $other instanceof self && $this->namespace === $other->namespace;
}

#[Override]
public function compare(object $other) : int
{
$thisStr = str_replace('\\', ' ', $this->namespace);
Expand Down
Loading