-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] access callback arguments, support PHP 8 union types (#2)
- Loading branch information
Showing
12 changed files
with
312 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -27,7 +27,7 @@ jobs: | |
uses: ramsey/composer-install@v1 | ||
|
||
- name: Test | ||
run: vendor/bin/phpunit -v | ||
run: vendor/bin/simple-phpunit -v | ||
|
||
code-coverage: | ||
name: Code Coverage | ||
|
@@ -39,15 +39,15 @@ jobs: | |
- name: Setup PHP | ||
uses: shivammathur/[email protected] | ||
with: | ||
php-version: 7.4 | ||
php-version: 8.0 | ||
coverage: xdebug | ||
ini-values: xdebug.mode=coverage | ||
|
||
- name: Install dependencies | ||
uses: ramsey/composer-install@v1 | ||
|
||
- name: Test with coverage | ||
run: vendor/bin/phpunit -v --coverage-text --coverage-clover coverage.xml | ||
run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover coverage.xml | ||
|
||
- name: Publish coverage report to Codecov | ||
uses: codecov/codecov-action@v1 | ||
|
This file contains 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 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 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 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 |
---|---|---|
|
@@ -2,13 +2,14 @@ | |
|
||
namespace Zenstruck; | ||
|
||
use Zenstruck\Callback\Argument; | ||
use Zenstruck\Callback\Exception\UnresolveableArgument; | ||
use Zenstruck\Callback\Parameter; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class Callback | ||
final class Callback implements \Countable | ||
{ | ||
/** @var \ReflectionFunction */ | ||
private $function; | ||
|
@@ -56,16 +57,16 @@ public static function createFor($value): self | |
*/ | ||
public function invoke(...$arguments) | ||
{ | ||
$parameters = $this->function->getParameters(); | ||
$functionArgs = $this->arguments(); | ||
|
||
foreach ($arguments as $key => $argument) { | ||
if (!$argument instanceof Parameter) { | ||
foreach ($arguments as $key => $parameter) { | ||
if (!$parameter instanceof Parameter) { | ||
continue; | ||
} | ||
|
||
if (!\array_key_exists($key, $parameters)) { | ||
if (!$argument->isOptional()) { | ||
throw new \ArgumentCountError(\sprintf('No argument %d for callable. Expected type: "%s".', $key + 1, $argument->type())); | ||
if (!\array_key_exists($key, $functionArgs)) { | ||
if (!$parameter->isOptional()) { | ||
throw new \ArgumentCountError(\sprintf('No argument %d for callable. Expected type: "%s".', $key + 1, $parameter->type())); | ||
} | ||
|
||
$arguments[$key] = null; | ||
|
@@ -74,9 +75,9 @@ public function invoke(...$arguments) | |
} | ||
|
||
try { | ||
$arguments[$key] = $argument->resolve($parameters[$key]); | ||
$arguments[$key] = $parameter->resolve($functionArgs[$key]); | ||
} catch (UnresolveableArgument $e) { | ||
throw new UnresolveableArgument(\sprintf('Unable to resolve argument %d for callback. Expected type: "%s". (%s)', $key + 1, $argument->type(), $this), $e); | ||
throw new UnresolveableArgument(\sprintf('Unable to resolve argument %d for callback. Expected type: "%s". (%s)', $key + 1, $parameter->type(), $this), $e); | ||
} | ||
} | ||
|
||
|
@@ -96,12 +97,12 @@ public function invoke(...$arguments) | |
*/ | ||
public function invokeAll(Parameter $parameter, int $min = 0) | ||
{ | ||
$arguments = $this->function->getParameters(); | ||
|
||
if (\count($arguments) < $min) { | ||
if (\count($this) < $min) { | ||
throw new \ArgumentCountError("{$min} argument(s) of type \"{$parameter->type()}\" required ({$this})."); | ||
} | ||
|
||
$arguments = $this->arguments(); | ||
|
||
foreach ($arguments as $key => $argument) { | ||
try { | ||
$arguments[$key] = $parameter->resolve($argument); | ||
|
@@ -112,4 +113,31 @@ public function invokeAll(Parameter $parameter, int $min = 0) | |
|
||
return $this->function->invoke(...$arguments); | ||
} | ||
|
||
/** | ||
* @return Argument[] | ||
*/ | ||
public function arguments(): array | ||
{ | ||
return \array_map( | ||
static function(\ReflectionParameter $parameter) { | ||
return new Argument($parameter); | ||
}, | ||
$this->function->getParameters() | ||
); | ||
} | ||
|
||
public function argument(int $index): Argument | ||
{ | ||
if (!isset(($arguments = $this->arguments())[$index])) { | ||
throw new \OutOfRangeException(\sprintf('Argument %d does not exist for %s.', $index + 1, $this)); | ||
} | ||
|
||
return $arguments[$index]; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return $this->function->getNumberOfParameters(); | ||
} | ||
} |
This file contains 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 | ||
|
||
namespace Zenstruck\Callback; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class Argument | ||
{ | ||
/** @var \ReflectionNamedType[] */ | ||
private $types = []; | ||
|
||
public function __construct(\ReflectionParameter $parameter) | ||
{ | ||
if (!$type = $parameter->getType()) { | ||
return; | ||
} | ||
|
||
if ($type instanceof \ReflectionNamedType) { | ||
$this->types = [$type]; | ||
|
||
return; | ||
} | ||
|
||
/** @var \ReflectionUnionType $type */ | ||
$this->types = $type->getTypes(); | ||
} | ||
|
||
public function type(): ?string | ||
{ | ||
return $this->hasType() ? \implode('|', $this->types()) : null; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function types(): array | ||
{ | ||
return \array_map(static function(\ReflectionNamedType $type) { return $type->getName(); }, $this->types); | ||
} | ||
|
||
public function hasType(): bool | ||
{ | ||
return !empty($this->types); | ||
} | ||
|
||
public function isUnionType(): bool | ||
{ | ||
return \count($this->types) > 1; | ||
} | ||
|
||
public function supports(string $type): bool | ||
{ | ||
if (!$this->hasType()) { | ||
// no type-hint so any type is supported | ||
return true; | ||
} | ||
|
||
foreach ($this->types() as $t) { | ||
if ($t === $type || \is_a($t, $type, true)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
namespace Zenstruck\Callback; | ||
|
||
use Zenstruck\Callback; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
|
@@ -15,8 +17,23 @@ public function __construct(callable $factory) | |
$this->factory = $factory; | ||
} | ||
|
||
public function __invoke(?string $type) | ||
public function __invoke(Argument $argument) | ||
{ | ||
return ($this->factory)($type); | ||
$stringTypeFactory = Parameter::factory(function() use ($argument) { | ||
if ($argument->isUnionType()) { | ||
throw new \LogicException(\sprintf('ValueFactory does not support union types. Inject "%s" instead.', Argument::class)); | ||
} | ||
|
||
return $argument->type(); | ||
}); | ||
|
||
return Callback::createFor($this->factory) | ||
->invoke(Parameter::union( | ||
Parameter::typed(Argument::class, $argument), | ||
Parameter::typed('array', $argument->types()), | ||
Parameter::typed('string', $stringTypeFactory), | ||
Parameter::untyped($stringTypeFactory) | ||
)->optional()) | ||
; | ||
} | ||
} |
Oops, something went wrong.