-
Notifications
You must be signed in to change notification settings - Fork 1
FEATURE: typed enum access and strict enum matches #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5fc4bf1
TASK: WIP AccessTypeResolver
mhsdesign 7c4879b
TASK: WIP add EnumMemberType
mhsdesign 86e1e61
FEATURE: WIP Enums ensure that match is complete
mhsdesign e56b821
WIP: TASK Fix enum member access on instance
mhsdesign 7cd48f8
WIP: TASK Simplify MatchTypeResolver::resolveTypeOfEnumMatch
mhsdesign 11b81e6
WIP: TASK Remove odd delegation in EnumMemberType::is, as we dont nee…
mhsdesign b310c5a
WIP: TASK more satisfied enum->is type check with module id taken int…
mhsdesign eb5cf4e
WIP: TASK rename EnumType to EnumInstanceType
mhsdesign 5e1dd20
WIP: TASK add unit tests for Enum Types
mhsdesign 42224b5
WIP: TASK remove unneeded EnumMemberType::memberBackedValueType
mhsdesign 3c164cb
WIP: TASK combine EnumMemberType into EnumInstanceType
mhsdesign b4c48bc
WIP: Task: test providesTheEnumInstanceTypeWhenAnStaticEnumTypeIsRefe…
mhsdesign 5a33cc5
WIP: Task: add EnumInstanceType::isUnspecified and make getMemberName…
mhsdesign fc8f09f
WIP: Task: Matching enum value should be referenced statically
mhsdesign bf76048
Merge branch 'main' into feature/typedEnumAccessAndStrictEnumMatches
grebaldi 3d55c0c
TASK: Update deprecated data provider methods in tests/
grebaldi f374216
TASK: Satisfy phpstan
grebaldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
<?php | ||
|
||
/** | ||
* PackageFactory.ComponentEngine - Universal View Components for PHP | ||
* Copyright (C) 2022 Contributors of PackageFactory.ComponentEngine | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PackageFactory\ComponentEngine\TypeSystem\Resolver\Access; | ||
|
||
|
||
use PackageFactory\ComponentEngine\Parser\Ast\AccessNode; | ||
use PackageFactory\ComponentEngine\TypeSystem\Resolver\Expression\ExpressionTypeResolver; | ||
use PackageFactory\ComponentEngine\TypeSystem\ScopeInterface; | ||
use PackageFactory\ComponentEngine\TypeSystem\Type\EnumType\EnumInstanceType; | ||
use PackageFactory\ComponentEngine\TypeSystem\Type\EnumType\EnumStaticType; | ||
use PackageFactory\ComponentEngine\TypeSystem\Type\StructType\StructType; | ||
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface; | ||
use PackageFactory\ComponentEngine\Definition\AccessType; | ||
|
||
final class AccessTypeResolver | ||
{ | ||
public function __construct( | ||
private readonly ScopeInterface $scope | ||
) { | ||
} | ||
|
||
public function resolveTypeOf(AccessNode $accessNode): TypeInterface | ||
{ | ||
$expressionResolver = new ExpressionTypeResolver(scope: $this->scope); | ||
$rootType = $expressionResolver->resolveTypeOf($accessNode->root); | ||
|
||
return match ($rootType::class) { | ||
EnumStaticType::class => $this->createEnumInstanceMemberType($accessNode, $rootType), | ||
StructType::class => throw new \Exception('@TODO: StructType Access is not implemented'), | ||
default => throw new \Exception('@TODO Error: Cannot access on type ' . $rootType::class) | ||
}; | ||
} | ||
|
||
private function createEnumInstanceMemberType(AccessNode $accessNode, EnumStaticType $enumType): EnumInstanceType | ||
{ | ||
if (!( | ||
count($accessNode->chain->items) === 1 | ||
&& $accessNode->chain->items[0]->accessType === AccessType::MANDATORY | ||
)) { | ||
throw new \Error('@TODO Error: Enum access malformed, only one level member access is allowed.'); | ||
} | ||
|
||
$enumMemberName = $accessNode->chain->items[0]->accessor->value; | ||
|
||
return $enumType->getMemberType($enumMemberName); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,75 @@ | ||
<?php | ||
|
||
/** | ||
* PackageFactory.ComponentEngine - Universal View Components for PHP | ||
* Copyright (C) 2022 Contributors of PackageFactory.ComponentEngine | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PackageFactory\ComponentEngine\TypeSystem\Type\EnumType; | ||
|
||
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface; | ||
|
||
final class EnumInstanceType implements TypeInterface | ||
{ | ||
private function __construct( | ||
public readonly EnumStaticType $enumStaticType, | ||
private readonly ?string $memberName | ||
) { | ||
if ($memberName !== null && !$enumStaticType->hasMember($memberName)) { | ||
throw new \Exception('@TODO cannot access member ' . $memberName . ' of enum ' . $enumStaticType->enumName); | ||
} | ||
} | ||
|
||
public static function createUnspecifiedEnumInstanceType(EnumStaticType $enumStaticType): self | ||
{ | ||
return new self( | ||
enumStaticType: $enumStaticType, | ||
memberName: null | ||
); | ||
} | ||
|
||
public static function fromStaticEnumTypeAndMemberName(EnumStaticType $enumStaticType, string $enumMemberName): self | ||
{ | ||
return new self( | ||
enumStaticType: $enumStaticType, | ||
memberName: $enumMemberName | ||
); | ||
} | ||
|
||
public function isUnspecified(): bool | ||
{ | ||
return $this->memberName === null; | ||
} | ||
|
||
public function getMemberName(): string | ||
{ | ||
return $this->memberName ?? throw new \Exception('@TODO Error cannot access memberName of unspecified instance'); | ||
} | ||
|
||
public function is(TypeInterface $other): bool | ||
{ | ||
if ($other === $this) { | ||
return true; | ||
} | ||
if ($other instanceof EnumInstanceType) { | ||
return $other->enumStaticType->is($other->enumStaticType) | ||
&& $other->memberName === $this->memberName; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.