Skip to content

Commit

Permalink
fix: allow any constant in class constant type
Browse files Browse the repository at this point in the history
The following annotation is now allowed:

 ```php
 final class SomeClass
 {
     public const FOO = 'foo';
     public const BAR = 42;
 }

 (new \CuyZ\Valinor\MapperBuilder())
     ->mapper()
     ->map('SomeClass::*', 'foo'); // ✅
```
  • Loading branch information
romm committed Apr 7, 2024
1 parent 86fb7b6 commit 6942755
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 38 deletions.

This file was deleted.

5 changes: 0 additions & 5 deletions src/Type/Parser/Lexer/Token/ClassNameToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use CuyZ\Valinor\Definition\Repository\Reflection\TypeResolver\ClassTemplatesResolver;
use CuyZ\Valinor\Type\Parser\Exception\Constant\ClassConstantCaseNotFound;
use CuyZ\Valinor\Type\Parser\Exception\Constant\MissingClassConstantCase;
use CuyZ\Valinor\Type\Parser\Exception\Constant\MissingSpecificClassConstantCase;
use CuyZ\Valinor\Type\Parser\Exception\Generic\CannotAssignGeneric;
use CuyZ\Valinor\Type\Parser\Exception\Generic\GenericClosingBracketMissing;
use CuyZ\Valinor\Type\Parser\Exception\Generic\GenericCommaMissing;
Expand Down Expand Up @@ -81,10 +80,6 @@ private function classConstant(TokenStream $stream): ?Type

$symbol = $stream->forward()->symbol();

if ($symbol === '*') {
throw new MissingSpecificClassConstantCase($this->reflection->name);
}

$cases = [];

if (! preg_match('/\*\s*\*/', $symbol)) {
Expand Down
10 changes: 0 additions & 10 deletions tests/Functional/Type/Parser/LexingParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use CuyZ\Valinor\Type\IntegerType;
use CuyZ\Valinor\Type\Parser\Exception\Constant\ClassConstantCaseNotFound;
use CuyZ\Valinor\Type\Parser\Exception\Constant\MissingClassConstantCase;
use CuyZ\Valinor\Type\Parser\Exception\Constant\MissingSpecificClassConstantCase;
use CuyZ\Valinor\Type\Parser\Exception\Enum\EnumCaseNotFound;
use CuyZ\Valinor\Type\Parser\Exception\Enum\MissingEnumCase;
use CuyZ\Valinor\Type\Parser\Exception\Enum\MissingSpecificEnumCase;
Expand Down Expand Up @@ -1537,15 +1536,6 @@ public function test_no_class_constant_case_found_with_several_wildcards_in_a_ro
$this->parser->parse(ObjectWithConstants::class . '::F**O');
}

public function test_missing_specific_class_constant_case_throws_exception(): void
{
$this->expectException(MissingSpecificClassConstantCase::class);
$this->expectExceptionCode(1664904636);
$this->expectExceptionMessage('Missing specific case for class constant `' . ObjectWithConstants::class . '::?` (cannot be `*`).');

$this->parser->parse(ObjectWithConstants::class . '::*');
}

public function test_missing_generics_throws_exception(): void
{
$genericClassName = SomeClassWithThreeTemplates::class;
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Mapping/Object/ConstantValuesMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class ConstantValuesMappingTest extends IntegrationTestCase
public function test_values_are_mapped_properly(): void
{
$source = [
'anyConstantWithStringValue' => 'some string value',
'anyConstantWithIntegerValue' => 1653398289,
'constantStringValue' => 'another string value',
'constantIntegerValue' => 1653398289,
'constantFloatValue' => 404.512,
Expand All @@ -39,6 +41,8 @@ public function test_values_are_mapped_properly(): void
$this->mappingFail($error);
}

self::assertSame('some string value', $result->anyConstantWithStringValue);
self::assertSame(1653398289, $result->anyConstantWithIntegerValue);
self::assertSame('another string value', $result->constantStringValue);
self::assertSame(1653398289, $result->constantIntegerValue);
self::assertSame(404.512, $result->constantFloatValue);
Expand Down Expand Up @@ -90,6 +94,12 @@ public function test_constant_not_matching_pattern_cannot_be_mapped(): void

class ClassWithConstantValues
{
/** @var ObjectWithConstants::* */
public mixed $anyConstantWithStringValue;

/** @var ObjectWithConstants::* */
public mixed $anyConstantWithIntegerValue;

/** @var ObjectWithConstants::CONST_WITH_STRING_* */
public string $constantStringValue;

Expand All @@ -112,6 +122,8 @@ class ClassWithConstantValues
final class ClassWithConstantValuesWithConstructor extends ClassWithConstantValues
{
/**
* @param ObjectWithConstants::* $anyConstantWithStringValue
* @param ObjectWithConstants::* $anyConstantWithIntegerValue
* @param ObjectWithConstants::CONST_WITH_STRING_* $constantStringValue
* @param ObjectWithConstants::CONST_WITH_INTEGER_* $constantIntegerValue
* @param ObjectWithConstants::CONST_WITH_FLOAT_* $constantFloatValue
Expand All @@ -120,13 +132,17 @@ final class ClassWithConstantValuesWithConstructor extends ClassWithConstantValu
* @param ObjectWithConstants::CONST_WITH_NESTED_ARRAY_VALUE_* $constantNestedArrayValue
*/
public function __construct(
mixed $anyConstantWithStringValue,
mixed $anyConstantWithIntegerValue,
string $constantStringValue,
int $constantIntegerValue,
float $constantFloatValue,
BackedIntegerEnum $constantEnumValue,
array $constantArrayValue,
array $constantNestedArrayValue
) {
$this->anyConstantWithStringValue = $anyConstantWithStringValue;
$this->anyConstantWithIntegerValue = $anyConstantWithIntegerValue;
$this->constantStringValue = $constantStringValue;
$this->constantIntegerValue = $constantIntegerValue;
$this->constantFloatValue = $constantFloatValue;
Expand Down

0 comments on commit 6942755

Please sign in to comment.