Skip to content

Commit

Permalink
Moved object related code to object interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Amjad Mohamed committed Sep 3, 2012
1 parent b4fbfe0 commit 78ed711
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
30 changes: 0 additions & 30 deletions src/PHPSpec/Specification/Interceptor.php
Expand Up @@ -162,10 +162,6 @@ public function __call($method, $args)

}

if ($this->interceptedHasAMagicCall()) {
return $this->invokeInterceptedMagicCall($method, $args);
}

if ($this->callingExpectationsAsMethods($method)) {
$this->throwErrorExpectationsAreProperties();
}
Expand Down Expand Up @@ -327,32 +323,6 @@ protected function performMatchingWithUserDefinedMatcher($matcher,
$this->performMatching();
}

/**
* Checks if intercepted has a magic __call
*
* @return boolean
*/
protected function interceptedHasAMagicCall()
{
return !$this->_actualValue instanceof ExampleGroup &&
method_exists($this->_actualValue, '__call');
}

/**
* Invokes intercepted magic call
*
* @param string $method
* @param array $args
* @return mixed
*/
protected function invokeInterceptedMagicCall($method, $args)
{
$intercepted = new \ReflectionMethod($this->_actualValue, '__call');
return InterceptorFactory::create($intercepted->invokeArgs(
$this->_actualValue, array($method, $args)
));
}

/**
* Checks if intercepted is an object
*
Expand Down
56 changes: 44 additions & 12 deletions src/PHPSpec/Specification/Interceptor/Object.php
Expand Up @@ -22,6 +22,7 @@
namespace PHPSpec\Specification\Interceptor;

use PHPSpec\Specification\Interceptor;
use PHPSpec\Specification\Interceptor\InterceptorFactory;
use PHPSpec\Matcher\InvalidMatcher;

/**
Expand Down Expand Up @@ -53,17 +54,6 @@ class Object extends Interceptor
*/
public function __call($method, $args)
{
$dslResult = parent::__call($method, $args);
if (!is_null($dslResult)) {
return $dslResult;
}

if ($this->isPredicate('have', $method, $args) ||
$this->isPredicate('be', $method, $args)) {
$this->performPredicateMatching();
return true;
}

$object = $this->getActualValue();
if (method_exists($object, $method)) {
return InterceptorFactory::create(
Expand All @@ -76,6 +66,21 @@ public function __call($method, $args)
return $this->accessProperty($args[0]);
}

$dslResult = parent::__call($method, $args);
if (!is_null($dslResult)) {
return $dslResult;
}

if ($this->interceptedHasAMagicCall()) {
return $this->invokeInterceptedMagicCall($method, $args);
}

if ($this->isPredicate('have', $method, $args) ||
$this->isPredicate('be', $method, $args)) {
$this->performPredicateMatching();
return true;
}

$class = get_class($object);
throw new InvalidMatcher(
"Call to undefined method {$class}::{$method}"
Expand Down Expand Up @@ -219,7 +224,7 @@ private function accessProperty($property)
if (array_key_exists($protected, $objectAsArray)) {
return InterceptorFactory::create($objectAsArray[$protected]);
}

foreach ($classes as $class) {
$private = sprintf("\0%s\0%s", $class, $property);

Expand Down Expand Up @@ -248,4 +253,31 @@ private function getClassAndParents()
}
return $classes;
}

/**
* Checks if intercepted has a magic __call
*
* @return boolean
*/
protected function interceptedHasAMagicCall()
{
return !$this->getActualValue() instanceof ExampleGroup &&
method_exists($this->getActualValue(), '__call');
}

/**
* Invokes intercepted magic call
*
* @param string $method
* @param array $args
* @return mixed
*/
protected function invokeInterceptedMagicCall($method, $args)
{
$intercepted = new \ReflectionMethod($this->getActualValue(), '__call');
return InterceptorFactory::create($intercepted->invokeArgs(
$this->getActualValue(), array($method, $args)
));
}

}

0 comments on commit 78ed711

Please sign in to comment.