-
Notifications
You must be signed in to change notification settings - Fork 230
Default property with @NamedArgumentConstructor #402
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
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,8 +15,10 @@ | |||||
|
|
||||||
| use function array_keys; | ||||||
| use function array_map; | ||||||
| use function array_values; | ||||||
| use function class_exists; | ||||||
| use function constant; | ||||||
| use function count; | ||||||
| use function defined; | ||||||
| use function explode; | ||||||
| use function gettype; | ||||||
|
|
@@ -547,6 +549,10 @@ class_exists(NamedArgumentConstructor::class); | |||||
|
|
||||||
| if ($annotation instanceof NamedArgumentConstructor) { | ||||||
| $metadata['has_named_argument_constructor'] = $metadata['has_constructor']; | ||||||
| if ($metadata['has_named_argument_constructor']) { | ||||||
| // choose the first argument as the default property | ||||||
| $metadata['default_property'] = $constructor->getParameters()[0]->getName(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (! ($annotation instanceof Attributes)) { | ||||||
|
|
@@ -595,15 +601,15 @@ class_exists(NamedArgumentConstructor::class); | |||||
| } | ||||||
|
|
||||||
| $metadata['enum'][$property->name]['value'] = $annotation->value; | ||||||
| $metadata['enum'][$property->name]['literal'] = ( ! empty($annotation->literal)) | ||||||
| $metadata['enum'][$property->name]['literal'] = (! empty($annotation->literal)) | ||||||
| ? $annotation->literal | ||||||
| : $annotation->value; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // choose the first property as default property | ||||||
| $metadata['default_property'] = reset($metadata['properties']); | ||||||
| } elseif (PHP_VERSION_ID < 80000 && $metadata['has_named_argument_constructor']) { | ||||||
| } elseif ($metadata['has_named_argument_constructor']) { | ||||||
| foreach ($constructor->getParameters() as $parameter) { | ||||||
| $metadata['constructor_args'][$parameter->getName()] = [ | ||||||
| 'position' => $parameter->getPosition(), | ||||||
|
|
@@ -859,7 +865,8 @@ private function Annotation() | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| $values = $this->MethodCall(); | ||||||
| $arguments = $this->MethodCall(); | ||||||
| $values = $this->resolvePositionalValues($arguments, $name); | ||||||
|
|
||||||
| if (isset(self::$annotationMetadata[$name]['enum'])) { | ||||||
| // checks all declared attributes | ||||||
|
|
@@ -1054,30 +1061,20 @@ private function Values(): array | |||||
| $token = $this->lexer->lookahead; | ||||||
| $value = $this->Value(); | ||||||
|
|
||||||
| if (! is_object($value) && ! is_array($value)) { | ||||||
| throw $this->syntaxError('Value', $token); | ||||||
| } | ||||||
|
|
||||||
| $values[] = $value; | ||||||
| } | ||||||
|
|
||||||
| $namedArguments = []; | ||||||
| $positionalArguments = []; | ||||||
| foreach ($values as $k => $value) { | ||||||
| if (is_object($value) && $value instanceof stdClass) { | ||||||
| $values[$value->name] = $value->value; | ||||||
| } elseif (! isset($values['value'])) { | ||||||
| $values['value'] = $value; | ||||||
| $namedArguments[$value->name] = $value->value; | ||||||
| } else { | ||||||
| if (! is_array($values['value'])) { | ||||||
| $values['value'] = [$values['value']]; | ||||||
| } | ||||||
|
|
||||||
| $values['value'][] = $value; | ||||||
| $positionalArguments[$k] = $value; | ||||||
| } | ||||||
|
|
||||||
| unset($values[$k]); | ||||||
| } | ||||||
|
|
||||||
| return $values; | ||||||
| return ['named_arguments' => $namedArguments, 'positional_arguments' => $positionalArguments]; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -1103,9 +1100,9 @@ private function Constant() | |||||
| case ! empty($this->namespaces): | ||||||
| foreach ($this->namespaces as $ns) { | ||||||
| if (class_exists($ns . '\\' . $className) || interface_exists($ns . '\\' . $className)) { | ||||||
| $className = $ns . '\\' . $className; | ||||||
| $found = true; | ||||||
| break; | ||||||
| $className = $ns . '\\' . $className; | ||||||
| $found = true; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -1132,7 +1129,7 @@ private function Constant() | |||||
| } | ||||||
|
|
||||||
| if ($found) { | ||||||
| $identifier = $className . '::' . $const; | ||||||
| $identifier = $className . '::' . $const; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -1405,4 +1402,57 @@ private function isIgnoredAnnotation(string $name): bool | |||||
|
|
||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Resolve positional arguments (without name) to named ones | ||||||
| * | ||||||
| * @param array<string,mixed> $arguments | ||||||
| * | ||||||
| * @return array<string,mixed> | ||||||
| */ | ||||||
| private function resolvePositionalValues(array $arguments, string $name): array | ||||||
| { | ||||||
| $positionalArguments = $arguments['positional_arguments'] ?? []; | ||||||
| $values = $arguments['named_arguments'] ?? []; | ||||||
|
|
||||||
| if ( | ||||||
| self::$annotationMetadata[$name]['has_named_argument_constructor'] | ||||||
| && self::$annotationMetadata[$name]['default_property'] !== null | ||||||
| ) { | ||||||
| // We must ensure that we don't have positional arguments after named ones | ||||||
| $positions = array_keys($positionalArguments); | ||||||
| $lastPosition = null; | ||||||
| foreach ($positions as $position) { | ||||||
| if ( | ||||||
| ($lastPosition === null && $position !== 0 ) || | ||||||
| ($lastPosition !== null && $position !== $lastPosition + 1) | ||||||
| ) { | ||||||
| throw $this->syntaxError('Positional arguments after named arguments is not allowed'); | ||||||
|
Member
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.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| $lastPosition = $position; | ||||||
| } | ||||||
|
|
||||||
| foreach (self::$annotationMetadata[$name]['constructor_args'] as $property => $parameter) { | ||||||
| $position = $parameter['position']; | ||||||
| if (isset($values[$property]) || ! isset($positionalArguments[$position])) { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| $values[$property] = $positionalArguments[$position]; | ||||||
| } | ||||||
| } else { | ||||||
| if (count($positionalArguments) > 0 && ! isset($values['value'])) { | ||||||
| if (count($positionalArguments) === 1) { | ||||||
| $value = $positionalArguments[0]; | ||||||
|
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. See #405 |
||||||
| } else { | ||||||
| $value = array_values($positionalArguments); | ||||||
| } | ||||||
|
|
||||||
| $values['value'] = $value; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return $values; | ||||||
| } | ||||||
| } | ||||||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
resolvePositionalValues, you validate$positionalArguments. Can't we do this here already? I don't like that we pass around potentially invalid metadata.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Values()method is part of the parsing process. If we move the validation inside, it will have to know what kind of annotation we are talking about. The validation is different is you use@NamedArgumentConstructoror not.This is valid without
@Name("1", foo="bar", "2")but should not be valid with@NamedArgumentConstructoras it's supposed to mimic a regular constructor call with named arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All right, thank you for clarifying. 👍🏻