annotate vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/ProphecySubjectPatch.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Prophecy.
Chris@0 5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 6 * Marcello Duarte <marcello.duarte@gmail.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Prophecy\Doubler\ClassPatch;
Chris@0 13
Chris@0 14 use Prophecy\Doubler\Generator\Node\ClassNode;
Chris@0 15 use Prophecy\Doubler\Generator\Node\MethodNode;
Chris@0 16 use Prophecy\Doubler\Generator\Node\ArgumentNode;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Add Prophecy functionality to the double.
Chris@0 20 * This is a core class patch for Prophecy.
Chris@0 21 *
Chris@0 22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
Chris@0 23 */
Chris@0 24 class ProphecySubjectPatch implements ClassPatchInterface
Chris@0 25 {
Chris@0 26 /**
Chris@0 27 * Always returns true.
Chris@0 28 *
Chris@0 29 * @param ClassNode $node
Chris@0 30 *
Chris@0 31 * @return bool
Chris@0 32 */
Chris@0 33 public function supports(ClassNode $node)
Chris@0 34 {
Chris@0 35 return true;
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Apply Prophecy functionality to class node.
Chris@0 40 *
Chris@0 41 * @param ClassNode $node
Chris@0 42 */
Chris@0 43 public function apply(ClassNode $node)
Chris@0 44 {
Chris@0 45 $node->addInterface('Prophecy\Prophecy\ProphecySubjectInterface');
Chris@0 46 $node->addProperty('objectProphecy', 'private');
Chris@0 47
Chris@0 48 foreach ($node->getMethods() as $name => $method) {
Chris@0 49 if ('__construct' === strtolower($name)) {
Chris@0 50 continue;
Chris@0 51 }
Chris@0 52
Chris@0 53 if ($method->getReturnType() === 'void') {
Chris@0 54 $method->setCode(
Chris@0 55 '$this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());'
Chris@0 56 );
Chris@0 57 } else {
Chris@0 58 $method->setCode(
Chris@0 59 'return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());'
Chris@0 60 );
Chris@0 61 }
Chris@0 62 }
Chris@0 63
Chris@0 64 $prophecySetter = new MethodNode('setProphecy');
Chris@0 65 $prophecyArgument = new ArgumentNode('prophecy');
Chris@0 66 $prophecyArgument->setTypeHint('Prophecy\Prophecy\ProphecyInterface');
Chris@0 67 $prophecySetter->addArgument($prophecyArgument);
Chris@0 68 $prophecySetter->setCode('$this->objectProphecy = $prophecy;');
Chris@0 69
Chris@0 70 $prophecyGetter = new MethodNode('getProphecy');
Chris@0 71 $prophecyGetter->setCode('return $this->objectProphecy;');
Chris@0 72
Chris@0 73 if ($node->hasMethod('__call')) {
Chris@0 74 $__call = $node->getMethod('__call');
Chris@0 75 } else {
Chris@0 76 $__call = new MethodNode('__call');
Chris@0 77 $__call->addArgument(new ArgumentNode('name'));
Chris@0 78 $__call->addArgument(new ArgumentNode('arguments'));
Chris@0 79
Chris@17 80 $node->addMethod($__call, true);
Chris@0 81 }
Chris@0 82
Chris@0 83 $__call->setCode(<<<PHP
Chris@0 84 throw new \Prophecy\Exception\Doubler\MethodNotFoundException(
Chris@0 85 sprintf('Method `%s::%s()` not found.', get_class(\$this), func_get_arg(0)),
Chris@0 86 \$this->getProphecy(), func_get_arg(0)
Chris@0 87 );
Chris@0 88 PHP
Chris@0 89 );
Chris@0 90
Chris@17 91 $node->addMethod($prophecySetter, true);
Chris@17 92 $node->addMethod($prophecyGetter, true);
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Returns patch priority, which determines when patch will be applied.
Chris@0 97 *
Chris@0 98 * @return int Priority number (higher - earlier)
Chris@0 99 */
Chris@0 100 public function getPriority()
Chris@0 101 {
Chris@0 102 return 0;
Chris@0 103 }
Chris@0 104 }