4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Doubler\ClassPatch;
14 use Prophecy\Doubler\Generator\Node\ClassNode;
15 use Prophecy\Doubler\Generator\Node\MethodNode;
16 use Prophecy\Doubler\Generator\Node\ArgumentNode;
19 * Add Prophecy functionality to the double.
20 * This is a core class patch for Prophecy.
22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
24 class ProphecySubjectPatch implements ClassPatchInterface
27 * Always returns true.
29 * @param ClassNode $node
33 public function supports(ClassNode $node)
39 * Apply Prophecy functionality to class node.
41 * @param ClassNode $node
43 public function apply(ClassNode $node)
45 $node->addInterface('Prophecy\Prophecy\ProphecySubjectInterface');
46 $node->addProperty('objectProphecy', 'private');
48 foreach ($node->getMethods() as $name => $method) {
49 if ('__construct' === strtolower($name)) {
53 if ($method->getReturnType() === 'void') {
55 '$this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());'
59 'return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());'
64 $prophecySetter = new MethodNode('setProphecy');
65 $prophecyArgument = new ArgumentNode('prophecy');
66 $prophecyArgument->setTypeHint('Prophecy\Prophecy\ProphecyInterface');
67 $prophecySetter->addArgument($prophecyArgument);
68 $prophecySetter->setCode('$this->objectProphecy = $prophecy;');
70 $prophecyGetter = new MethodNode('getProphecy');
71 $prophecyGetter->setCode('return $this->objectProphecy;');
73 if ($node->hasMethod('__call')) {
74 $__call = $node->getMethod('__call');
76 $__call = new MethodNode('__call');
77 $__call->addArgument(new ArgumentNode('name'));
78 $__call->addArgument(new ArgumentNode('arguments'));
80 $node->addMethod($__call, true);
83 $__call->setCode(<<<PHP
84 throw new \Prophecy\Exception\Doubler\MethodNotFoundException(
85 sprintf('Method `%s::%s()` not found.', get_class(\$this), func_get_arg(0)),
86 \$this->getProphecy(), func_get_arg(0)
91 $node->addMethod($prophecySetter, true);
92 $node->addMethod($prophecyGetter, true);
96 * Returns patch priority, which determines when patch will be applied.
98 * @return int Priority number (higher - earlier)
100 public function getPriority()