forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle local type aliasing in class definition
Type aliases can now be added to a class definition. Both PHPStan and Psalm syntax are handled. ```php /** * @phpstan-type SomeTypeAlias = array{foo: string} * @psalm-type SomeOtherTypeAlias = array{bar: int} */ final class SomeClass { /** @var SomeTypeAlias */ public array $someTypeAlias; /** @var SomeOtherTypeAlias */ public array $someOtherTypeAlias; } ```
- Loading branch information
Showing
5 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Definition\Exception; | ||
|
||
use LogicException; | ||
|
||
use function implode; | ||
|
||
final class ClassTypeAliasesDuplication extends LogicException | ||
{ | ||
/** | ||
* @param class-string $className | ||
*/ | ||
public function __construct(string $className, string ...$names) | ||
{ | ||
$names = implode('`, `', $names); | ||
|
||
parent::__construct( | ||
"The following type aliases already exist in class `$className`: `$names`.", | ||
1638477604 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/Integration/Mapping/Type/LocalTypeAliasMappingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Integration\Mapping\Type; | ||
|
||
use CuyZ\Valinor\Mapper\MappingError; | ||
use CuyZ\Valinor\Tests\Integration\IntegrationTest; | ||
|
||
final class LocalTypeAliasMappingTest extends IntegrationTest | ||
{ | ||
public function test_values_are_mapped_properly(): void | ||
{ | ||
$source = [ | ||
'aliasWithEqualsSign' => 42, | ||
'aliasWithoutEqualsSign' => 42, | ||
'aliasShapedArray' => [ | ||
'foo' => 'foo', | ||
'bar' => 1337, | ||
], | ||
'aliasGeneric' => [42, 1337], | ||
]; | ||
|
||
foreach ([PhpStanLocalAliases::class, PsalmLocalAliases::class] as $class) { | ||
try { | ||
$result = $this->mapperBuilder | ||
->mapper() | ||
->map($class, $source); | ||
|
||
self::assertSame(42, $result->aliasWithEqualsSign); | ||
self::assertSame(42, $result->aliasWithoutEqualsSign); | ||
self::assertSame($source['aliasShapedArray'], $result->aliasShapedArray); | ||
self::assertSame($source['aliasGeneric'], $result->aliasGeneric->aliasArray); | ||
} catch (MappingError $error) { | ||
$this->mappingFail($error); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @template T | ||
* @phpstan-type AliasArray = T[] | ||
*/ | ||
class GenericObjectWithPhpStanLocalAlias | ||
{ | ||
/** @var AliasArray */ | ||
public array $aliasArray; | ||
} | ||
|
||
/** | ||
* @phpstan-type AliasWithEqualsSign = int | ||
* @phpstan-type AliasWithoutEqualsSign int | ||
* @phpstan-type AliasShapedArray = array{foo: string, bar: int} | ||
* @phpstan-type AliasGeneric = GenericObjectWithPhpStanLocalAlias<int> | ||
*/ | ||
class PhpStanLocalAliases | ||
{ | ||
/** @var AliasWithEqualsSign */ | ||
public int $aliasWithEqualsSign; | ||
|
||
/** @var AliasWithoutEqualsSign */ | ||
public int $aliasWithoutEqualsSign; | ||
|
||
/** @var AliasShapedArray */ | ||
public array $aliasShapedArray; | ||
|
||
/** @var AliasGeneric */ | ||
public GenericObjectWithPhpStanLocalAlias $aliasGeneric; | ||
} | ||
|
||
/** | ||
* @template T | ||
* @psalm-type AliasArray = T[] | ||
*/ | ||
class GenericObjectWithPsalmLocalAlias | ||
{ | ||
/** @var AliasArray */ | ||
public array $aliasArray; | ||
} | ||
|
||
/** | ||
* @psalm-type AliasWithEqualsSign = int | ||
* @psalm-type AliasWithoutEqualsSign int | ||
* @psalm-type AliasShapedArray = array{foo: string, bar: int} | ||
* @psalm-type AliasGeneric = GenericObjectWithPsalmLocalAlias<int> | ||
*/ | ||
class PsalmLocalAliases | ||
{ | ||
/** @var AliasWithEqualsSign */ | ||
public int $aliasWithEqualsSign; | ||
|
||
/** @var AliasWithoutEqualsSign */ | ||
public int $aliasWithoutEqualsSign; | ||
|
||
/** @var AliasShapedArray */ | ||
public array $aliasShapedArray; | ||
|
||
/** @var AliasGeneric */ | ||
public GenericObjectWithPsalmLocalAlias $aliasGeneric; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters