Chris@0: Chris@0: * Marcello Duarte Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Prophecy\Doubler\ClassPatch; Chris@0: Chris@0: use Prophecy\Doubler\Generator\Node\ClassNode; Chris@0: use Prophecy\Doubler\Generator\Node\MethodNode; Chris@0: use Prophecy\Doubler\Generator\Node\ArgumentNode; Chris@0: Chris@0: /** Chris@0: * Add Prophecy functionality to the double. Chris@0: * This is a core class patch for Prophecy. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: class ProphecySubjectPatch implements ClassPatchInterface Chris@0: { Chris@0: /** Chris@0: * Always returns true. Chris@0: * Chris@0: * @param ClassNode $node Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function supports(ClassNode $node) Chris@0: { Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Apply Prophecy functionality to class node. Chris@0: * Chris@0: * @param ClassNode $node Chris@0: */ Chris@0: public function apply(ClassNode $node) Chris@0: { Chris@0: $node->addInterface('Prophecy\Prophecy\ProphecySubjectInterface'); Chris@0: $node->addProperty('objectProphecy', 'private'); Chris@0: Chris@0: foreach ($node->getMethods() as $name => $method) { Chris@0: if ('__construct' === strtolower($name)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if ($method->getReturnType() === 'void') { Chris@0: $method->setCode( Chris@0: '$this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());' Chris@0: ); Chris@0: } else { Chris@0: $method->setCode( Chris@0: 'return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());' Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: $prophecySetter = new MethodNode('setProphecy'); Chris@0: $prophecyArgument = new ArgumentNode('prophecy'); Chris@0: $prophecyArgument->setTypeHint('Prophecy\Prophecy\ProphecyInterface'); Chris@0: $prophecySetter->addArgument($prophecyArgument); Chris@0: $prophecySetter->setCode('$this->objectProphecy = $prophecy;'); Chris@0: Chris@0: $prophecyGetter = new MethodNode('getProphecy'); Chris@0: $prophecyGetter->setCode('return $this->objectProphecy;'); Chris@0: Chris@0: if ($node->hasMethod('__call')) { Chris@0: $__call = $node->getMethod('__call'); Chris@0: } else { Chris@0: $__call = new MethodNode('__call'); Chris@0: $__call->addArgument(new ArgumentNode('name')); Chris@0: $__call->addArgument(new ArgumentNode('arguments')); Chris@0: Chris@17: $node->addMethod($__call, true); Chris@0: } Chris@0: Chris@0: $__call->setCode(<<getProphecy(), func_get_arg(0) Chris@0: ); Chris@0: PHP Chris@0: ); Chris@0: Chris@17: $node->addMethod($prophecySetter, true); Chris@17: $node->addMethod($prophecyGetter, true); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns patch priority, which determines when patch will be applied. Chris@0: * Chris@0: * @return int Priority number (higher - earlier) Chris@0: */ Chris@0: public function getPriority() Chris@0: { Chris@0: return 0; Chris@0: } Chris@0: }