fix ref function to work properly with array syntax
This commit is contained in:
parent
c11a615169
commit
0b58a4c620
14
src/Exception/NoAccessException.php
Normal file
14
src/Exception/NoAccessException.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Copyright 2018 Kacper Donat
|
||||||
|
*
|
||||||
|
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
||||||
|
*
|
||||||
|
* Full license available in separate LICENSE file
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Kadet\Functional\Exception;
|
||||||
|
|
||||||
|
class NoAccessException extends FunctionalException
|
||||||
|
{
|
||||||
|
}
|
@ -7,4 +7,4 @@
|
|||||||
* Full license available in separate LICENSE file
|
* Full license available in separate LICENSE file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
require_once 'objects.php';
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Kadet\Functional;
|
namespace Kadet\Functional;
|
||||||
|
|
||||||
|
use Kadet\Functional\Exception\NoAccessException;
|
||||||
use Kadet\Functional\Predicate\AllOfPredicate;
|
use Kadet\Functional\Predicate\AllOfPredicate;
|
||||||
use Kadet\Functional\Predicate\AnyOfPredicate;
|
use Kadet\Functional\Predicate\AnyOfPredicate;
|
||||||
use Kadet\Functional\Predicate\ClosurePredicate;
|
use Kadet\Functional\Predicate\ClosurePredicate;
|
||||||
@ -32,8 +33,24 @@ function predicate($predicate): Predicate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function ref(callable $callable)
|
function ref($callable)
|
||||||
{
|
{
|
||||||
|
if (is_array($callable)) {
|
||||||
|
list($object, $method) = $callable;
|
||||||
|
$reflection = new \ReflectionObject($object);
|
||||||
|
$method = $reflection->getMethod($method);
|
||||||
|
|
||||||
|
if (!$method->isPublic()) {
|
||||||
|
$class = debug_backtrace()[1]['class'] ?? null;
|
||||||
|
|
||||||
|
if ($reflection->name !== $class) {
|
||||||
|
throw new NoAccessException(sprintf('Method "%s" can only be accessed from inside of object.', $method->name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $method->getClosure($object);
|
||||||
|
}
|
||||||
|
|
||||||
return \Closure::fromCallable($callable);
|
return \Closure::fromCallable($callable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +117,7 @@ function reflect(callable $callable): \ReflectionFunction
|
|||||||
case $callable instanceof Decorator:
|
case $callable instanceof Decorator:
|
||||||
return new ReflectionDecoratedFunction($callable->getDecorated());
|
return new ReflectionDecoratedFunction($callable->getDecorated());
|
||||||
default:
|
default:
|
||||||
return new \ReflectionFunction($callable);
|
return new \ReflectionFunction(ref($callable));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
57
tests/RandomTest.php
Normal file
57
tests/RandomTest.php
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use function Kadet\Functional\apply;
|
||||||
|
use function Kadet\Functional\ref;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 2018 Kacper Donat
|
||||||
|
*
|
||||||
|
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
||||||
|
*
|
||||||
|
* Full license available in separate LICENSE file
|
||||||
|
*/
|
||||||
|
|
||||||
|
class TestObject
|
||||||
|
{
|
||||||
|
public function identity($x)
|
||||||
|
{
|
||||||
|
return $x;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function priv()
|
||||||
|
{
|
||||||
|
return 'foo';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test()
|
||||||
|
{
|
||||||
|
return ref([$this, 'priv']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class RandomTest extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
public function testApplicationOfArrayNotation()
|
||||||
|
{
|
||||||
|
$object = new TestObject();
|
||||||
|
$this->assertSame(10, apply([$object, 'identity'], 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testReferencingPrivateFunctionsFromInsideOfObject()
|
||||||
|
{
|
||||||
|
$object = new TestObject();
|
||||||
|
$reference = $object->test();
|
||||||
|
|
||||||
|
$this->assertSame('foo', $reference());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Kadet\Functional\Exception\NoAccessException
|
||||||
|
*/
|
||||||
|
public function testReferencingPrivateFunctionsFromOutsideOfObject()
|
||||||
|
{
|
||||||
|
$object = new TestObject();
|
||||||
|
|
||||||
|
ref([$object, 'priv']);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user