Skip to content

Commit

Permalink
Accept only Closures
Browse files Browse the repository at this point in the history
this removes a bunch of unnecessary code.
the PM case only uses closures anyway, and for the cases where callables are needed, using Closure::fromCallable() is better anyway, since that ensures the scope is resolved as early as possible.
  • Loading branch information
dktapps committed Mar 29, 2024
1 parent cb9f88f commit c39d7e6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 54 deletions.
52 changes: 3 additions & 49 deletions src/CallbackType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,6 @@ final class CallbackType{

private int $requiredParameterCount;

/**
* Given a callable, create the appropriate reflection
*
* This will accept things the PHP would fail to invoke due to scoping, but we can reflect them anyway. Do not add
* a callable type-hint or this behaviour will break!
*
* @param callable $target
*
* @return \ReflectionFunction|\ReflectionMethod
* @throws \ReflectionException
*/
private static function reflectCallable($target){
if($target instanceof \Closure){
return new \ReflectionFunction($target);
}

if(\is_array($target) && isset($target[0], $target[1])){
return new \ReflectionMethod($target[0], $target[1]);
}

if(\is_object($target) && \method_exists($target, '__invoke')){
return new \ReflectionMethod($target, '__invoke');
}

if(\is_string($target)){
return \strpos($target, '::') !== false
? new \ReflectionMethod($target)
: new \ReflectionFunction($target);
}

throw new \UnexpectedValueException("Unknown callable type");
}

/**
* @param \ReflectionType[] $types
*
Expand Down Expand Up @@ -79,17 +46,8 @@ private static function convertReflectionType(?\ReflectionType $type) : ?BaseTyp
return $type === null ? null : self::convertReflectionTypeInner($type);
}

/**
* @param callable $callable
*
* @throws InvalidCallbackException
*/
public static function createFromCallable($callable) : CallbackType{
try{
$reflection = self::reflectCallable($callable);
}catch(\ReflectionException $e){
throw new InvalidCallbackException('Failed to reflect the supplied callable', 0, $e);
}
public static function createFromCallable(\Closure $callable) : CallbackType{
$reflection = new \ReflectionFunction($callable);

$returnType = new ReturnInfo(self::convertReflectionType($reflection->getReturnType()), $reflection->returnsReference());

Expand Down Expand Up @@ -119,11 +77,7 @@ public function __construct(ReturnInfo $returnType, ParameterInfo ...$parameters
}
}

/**
* @param callable $callable
* @throws InvalidCallbackException
*/
public function isSatisfiedBy($callable) : bool{
public function isSatisfiedBy(\Closure $callable) : bool{
$other = self::createFromCallable($callable);

if(!$this->returnInfo->isSatisfiedBy($other->returnInfo)){
Expand Down
5 changes: 0 additions & 5 deletions src/InvalidCallbackException.php

This file was deleted.

0 comments on commit c39d7e6

Please sign in to comment.