Skip to content

Commit

Permalink
PHP 8 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Aug 1, 2020
1 parent 3357ef3 commit 6a6f8f2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
17 changes: 3 additions & 14 deletions src/CallableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ private function resolveFromContainer($callable)
return $callable;
}

$isStaticCallToNonStaticMethod = false;

// If it's already a callable there is nothing to do
if (is_callable($callable)) {
$isStaticCallToNonStaticMethod = $this->isStaticCallToNonStaticMethod($callable);
if (! $isStaticCallToNonStaticMethod) {
// TODO with PHP 8 that should not be necessary to check this anymore
if (! $this->isStaticCallToNonStaticMethod($callable)) {
return $callable;
}
}
Expand Down Expand Up @@ -95,17 +93,8 @@ private function resolveFromContainer($callable)
if ($this->container->has($callable[0])) {
throw $e;
}
if ($isStaticCallToNonStaticMethod) {
throw new NotCallableException(sprintf(
'Cannot call %s::%s() because %s() is not a static method and "%s" is not a container entry',
$callable[0],
$callable[1],
$callable[1],
$callable[0]
));
}
throw new NotCallableException(sprintf(
'Cannot call %s on %s because it is not a class nor a valid container entry',
'Cannot call %s() on %s because it is not a class nor a valid container entry',
$callable[1],
$callable[0]
));
Expand Down
21 changes: 18 additions & 3 deletions src/ParameterResolver/Container/TypeHintContainerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Invoker\ParameterResolver\ParameterResolver;
use Psr\Container\ContainerInterface;
use ReflectionFunctionAbstract;
use ReflectionNamedType;

/**
* Inject entries from a DI container using the type-hints.
Expand Down Expand Up @@ -39,10 +40,24 @@ public function getParameters(
}

foreach ($parameters as $index => $parameter) {
$parameterClass = $parameter->getClass();
$parameterType = $parameter->getType();
if (!$parameterType) {
// No type
continue;
}
if ($parameterType->isBuiltin()) {
// Primitive types are not supported
continue;
}
if (!$parameterType instanceof ReflectionNamedType) {
// Union types are not supported
continue;
}

$parameterClass = $parameterType->getName();

if ($parameterClass && $this->container->has($parameterClass->name)) {
$resolvedParameters[$index] = $this->container->get($parameterClass->name);
if ($this->container->has($parameterClass)) {
$resolvedParameters[$index] = $this->container->get($parameterClass);
}
}

Expand Down
21 changes: 18 additions & 3 deletions src/ParameterResolver/TypeHintResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Invoker\ParameterResolver;

use ReflectionFunctionAbstract;
use ReflectionNamedType;

/**
* Inject entries using type-hints.
Expand All @@ -26,10 +27,24 @@ public function getParameters(
}

foreach ($parameters as $index => $parameter) {
$parameterClass = $parameter->getClass();
$parameterType = $parameter->getType();
if (!$parameterType) {
// No type
continue;
}
if ($parameterType->isBuiltin()) {
// Primitive types are not supported
continue;
}
if (!$parameterType instanceof ReflectionNamedType) {
// Union types are not supported
continue;
}

$parameterClass = $parameterType->getName();

if ($parameterClass && array_key_exists($parameterClass->name, $providedParameters)) {
$resolvedParameters[$index] = $providedParameters[$parameterClass->name];
if (array_key_exists($parameterClass, $providedParameters)) {
$resolvedParameters[$index] = $providedParameters[$parameterClass];
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public function positioned_parameters_have_the_highest_priority()
*/
public function should_not_invoke_statically_a_non_static_method()
{
$this->expectExceptionMessage("Cannot call Invoker\Test\InvokerTestFixture::foo() because foo() is not a static method and \"Invoker\Test\InvokerTestFixture\" is not a container entry");
$this->expectExceptionMessage("Cannot call foo() on Invoker\Test\InvokerTestFixture because it is not a class nor a valid container entry");
$this->expectException(NotCallableException::class);
$this->invoker->call([InvokerTestFixture::class, 'foo']);
}
Expand Down

0 comments on commit 6a6f8f2

Please sign in to comment.