-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add extractor classes for reflection #24
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
Open
owenvoke
wants to merge
1
commit into
main
Choose a base branch
from
feature/extractor-classes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Worksome\GraphQLHelpers\Utils\Reflection; | ||
|
|
||
| use GraphQL\Type\Definition\Deprecated; | ||
| use ReflectionEnumUnitCase; | ||
|
|
||
| class DeprecationExtractor | ||
| { | ||
| public static function extract(ReflectionEnumUnitCase $reflection): string|null | ||
| { | ||
| $deprecations = $reflection->getAttributes(Deprecated::class); | ||
|
|
||
| if (count($deprecations) === 1) { | ||
| return $deprecations[0]->newInstance()->reason; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
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,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Worksome\GraphQLHelpers\Utils\Reflection; | ||
|
|
||
| use ReflectionEnumUnitCase; | ||
| use ValueError; | ||
| use Worksome\GraphQLHelpers\Definition\Attributes\CasesDescribedBy; | ||
|
|
||
| class DescribedByExtractor | ||
| { | ||
| public static function extract(ReflectionEnumUnitCase $reflection): string|null | ||
| { | ||
| $enum = $reflection->getEnum(); | ||
|
|
||
| $describers = $enum->getAttributes(CasesDescribedBy::class); | ||
|
|
||
| if (count($describers) === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| $describer = $describers[0]->newInstance()->describer; | ||
|
|
||
| if (! $enum->hasMethod($describer)) { | ||
| throw new ValueError( | ||
| sprintf( | ||
| 'The describer method `%s` does not exist on `%s`', | ||
| $describer, | ||
| $enum->name | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| $description = $enum->getMethod($describer)->invoke($reflection->getValue()); | ||
|
|
||
| if ($description === null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! is_string($description)) { | ||
| throw new ValueError( | ||
| sprintf( | ||
| 'The describer method `%s` on `%s` must return a string', | ||
| $describer, | ||
| $enum->name | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| return $description; | ||
| } | ||
| } |
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,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Worksome\GraphQLHelpers\Utils\Reflection; | ||
|
|
||
| use GraphQL\Type\Definition\Description; | ||
| use GraphQL\Utils\PhpDoc; | ||
| use ReflectionEnum; | ||
| use ReflectionEnumUnitCase; | ||
|
|
||
| class DescriptionExtractor | ||
| { | ||
| /** @phpstan-ignore-next-line */ | ||
| public static function extract( | ||
| ReflectionEnum|ReflectionEnumUnitCase $reflection, | ||
| ): string|null { | ||
| $descriptions = $reflection->getAttributes(Description::class); | ||
|
|
||
|
owenvoke marked this conversation as resolved.
|
||
| if (count($descriptions) === 1) { | ||
| return $descriptions[0]->newInstance()->description; | ||
| } | ||
|
|
||
| if ( | ||
| $reflection instanceof ReflectionEnumUnitCase | ||
| && $description = DescribedByExtractor::extract($reflection) | ||
| ) { | ||
| return $description; | ||
|
owenvoke marked this conversation as resolved.
|
||
| } | ||
|
|
||
| $comment = $reflection->getDocComment(); | ||
| $unpadded = PhpDoc::unpad($comment); | ||
|
|
||
| return PhpDoc::unwrap($unpadded); | ||
|
Comment on lines
+31
to
+34
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to fallback to the PhpDoc comments? 👀 |
||
| } | ||
| } | ||
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,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Worksome\GraphQLHelpers\Tests\Unit\Utils\Reflection; | ||
|
|
||
| use GraphQL\Type\Definition\Deprecated; | ||
| use ReflectionEnumUnitCase; | ||
| use Worksome\GraphQLHelpers\Utils\Reflection\DeprecationExtractor; | ||
|
|
||
| enum DeprecationExtractorDummyEnum | ||
| { | ||
| #[Deprecated('This is deprecated.')] | ||
| case Deprecated; | ||
|
|
||
| case NotDeprecated; | ||
| } | ||
|
|
||
| it( | ||
| 'can extract the deprecation reason from an enum case', | ||
| function (DeprecationExtractorDummyEnum $enum, string|null $reason) { | ||
| $reflection = new ReflectionEnumUnitCase($enum, $enum->name); | ||
|
|
||
| expect(DeprecationExtractor::extract($reflection))->toBe($reason); | ||
| } | ||
| )->with([ | ||
| [DeprecationExtractorDummyEnum::Deprecated, 'This is deprecated.'], | ||
| [DeprecationExtractorDummyEnum::NotDeprecated, null], | ||
| ]); |
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,74 @@ | ||
| <?php | ||
|
owenvoke marked this conversation as resolved.
|
||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Worksome\GraphQLHelpers\Tests\Unit\Utils\Reflection; | ||
|
|
||
| use ReflectionEnumUnitCase; | ||
| use ValueError; | ||
| use Worksome\GraphQLHelpers\Definition\Attributes\CasesDescribedBy; | ||
| use Worksome\GraphQLHelpers\Utils\Reflection\DescribedByExtractor; | ||
|
|
||
|
owenvoke marked this conversation as resolved.
|
||
| #[CasesDescribedBy(describer: 'description')] | ||
| enum DescribedByExtractorDummyEnum | ||
| { | ||
|
owenvoke marked this conversation as resolved.
|
||
| case PascalCase; | ||
|
|
||
| public function description(): string | ||
| { | ||
| return 'Description from the describer'; | ||
| } | ||
| } | ||
|
|
||
| #[CasesDescribedBy(describer: 'description')] | ||
| enum DescribedByExtractorDummyEnumMissingDescriber | ||
| { | ||
| case PascalCase; | ||
| } | ||
|
|
||
| #[CasesDescribedBy(describer: 'description')] | ||
| enum DescribedByExtractorDummyEnumWithNonStringReturnType | ||
| { | ||
| case PascalCase; | ||
|
|
||
| public function description(): int | ||
| { | ||
| return 123; | ||
| } | ||
| } | ||
|
owenvoke marked this conversation as resolved.
|
||
|
|
||
| it('can resolve descriptions from describer', function () { | ||
| $reflection = new ReflectionEnumUnitCase(DescribedByExtractorDummyEnum::class, 'PascalCase'); | ||
|
|
||
| expect(DescribedByExtractor::extract($reflection))->toBe('Description from the describer'); | ||
| }); | ||
|
|
||
| it('throws a ValueError when the describer method does not exist', function () { | ||
| $reflection = new ReflectionEnumUnitCase( | ||
| DescribedByExtractorDummyEnumMissingDescriber::class, | ||
| 'PascalCase' | ||
| ); | ||
|
|
||
| DescribedByExtractor::extract($reflection); | ||
| })->throws( | ||
| ValueError::class, | ||
| sprintf( | ||
| 'The describer method `description` does not exist on `%s`', | ||
| DescribedByExtractorDummyEnumMissingDescriber::class | ||
| ) | ||
| ); | ||
|
|
||
| it('throws a ValueError when the describer does not return a string', function () { | ||
| $reflection = new ReflectionEnumUnitCase( | ||
| DescribedByExtractorDummyEnumWithNonStringReturnType::class, | ||
| 'PascalCase' | ||
| ); | ||
|
|
||
| DescribedByExtractor::extract($reflection); | ||
| })->throws( | ||
| ValueError::class, | ||
| sprintf( | ||
| 'The describer method `description` on `%s` must return a string', | ||
| DescribedByExtractorDummyEnumWithNonStringReturnType::class | ||
| ) | ||
| ); | ||
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.