Skip to content
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

Refactor code to use Parameters::accept() instead of Routine::accepts() #9

Merged
merged 3 commits into from
Feb 7, 2021

Conversation

thekid
Copy link
Member

@thekid thekid commented Feb 7, 2021

On top of #8, which had this:

$method= ...
if ($method->accepts($args)) {
  // ...
}

...now the code would look like this:

$method= ...
if ($method->parameters()->accept($args)) {
  // ...
}

...because when testing the parameters accept the given arguments, it might be clearer that only the type constraints are checked and the method can still raise an error.

@thekid thekid added the enhancement New feature or request label Feb 7, 2021
@thekid
Copy link
Member Author

thekid commented Feb 7, 2021

The optional count parameter makes it easier to test for usecases such as one-arg constructors and testing whether the given arguments would be accepted all in the same method call:

if ($type->constructor()?->parameters()->accept($args, count: 1)) {
  // ...
}

Here's an example of the simplification (written with PHP 7 support in mind):

-      } else if (($c= $reflect->constructor()) && 1 === $c->parameters()->size() && $c->accepts([$value])) {
+      } else if (($c= $reflect->constructor()) && $c->parameters()->accept([$value], 1)) {

@thekid thekid merged commit e91f17b into main Feb 7, 2021
@thekid thekid deleted the refactor/parameter-accepts branch February 7, 2021 11:27
thekid added a commit that referenced this pull request Feb 7, 2021
@thekid
Copy link
Member Author

thekid commented Feb 7, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant