Skip to content

Commit

Permalink
Better handling of proxying core classes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Mar 12, 2017
1 parent 9234362 commit a4ff4f2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/Callbacks/DynamicCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ final class DynamicCallbacks implements CallbacksInterface
*/
private $callbacks = [];

/**
* @var bool
*/
private $userDefined;

/**
* @param mixed $object
* @param \Andrew\Checker\Checker $checker
Expand All @@ -66,6 +71,7 @@ public function __construct($object, Checker $checker = null)
$this->checker = $checker;
$this->object = $object;
$this->class = $class;
$this->userDefined = (new \ReflectionClass($class))->isUserDefined();
}

/**
Expand Down Expand Up @@ -148,7 +154,9 @@ private function createCallback($which)
}
};

$this->callbacks[$which] = Closure::bind($caller, $this->object, $this->class);
$this->callbacks[$which] = $this->userDefined
? Closure::bind($caller, $this->object, $this->class)
: Closure::bind($caller, $this->object);

return $this->callbacks[$which];
}
Expand Down
6 changes: 3 additions & 3 deletions src/Callbacks/StaticCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ final class StaticCallbacks implements CallbacksInterface
*/
public function __construct($class, Checker $checker = null)
{
if (ltrim($class, '\\') === 'stdClass') {
throw new ClassException('It is not possible to use static proxy with stdClass.');
}
$checker or $checker = new Checker();
$checker->assertClass($class, __CLASS__.' expects a fully qualified class name.');
if (! (new \ReflectionClass($class))->isUserDefined()) {
throw new ClassException('It is not possible to use static proxy with PHP native classes.');
}
$this->checker = $checker;
$this->class = $class;
$this->object = (new ReflectionClass($class))->newInstanceWithoutConstructor();
Expand Down
8 changes: 8 additions & 0 deletions tests/src/Unit/DynamicCallbacksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@ public function testCaller()
assertInternalType('callable', $caller);
assertSame('Private Dynamic Method Test!', $caller('privateDynamicMethod', [' Test!']));
}

public function testCallerCoreClass()
{
$callbacks = new DynamicCallbacks(new \ArrayObject());
$caller = $callbacks->caller();
assertInternalType('callable', $caller);
assertSame([], $caller('getArrayCopy'));
}
}
7 changes: 7 additions & 0 deletions tests/src/Unit/StaticCallbacksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function testConstructorFailsIfStdClass()
new StaticCallbacks(\stdClass::class);
}

public function testConstructorFailsIfNativeClass()
{
$this->expectException(ClassException::class);

new StaticCallbacks(\ArrayObject::class);
}

public function testGetter()
{
$callbacks = new StaticCallbacks(Stub::class);
Expand Down

0 comments on commit a4ff4f2

Please sign in to comment.