Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ProphecySubjectPatch.php

function ProphecySubjectPatch::apply

Apply Prophecy functionality to class node.

Parameters

ClassNode $node:

Overrides ClassPatchInterface::apply

File

vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/ProphecySubjectPatch.php, line 45

Class

ProphecySubjectPatch
Add Prophecy functionality to the double. This is a core class patch for Prophecy.

Namespace

Prophecy\Doubler\ClassPatch

Code

public function apply(ClassNode $node) {
    $node->addInterface('Prophecy\\Prophecy\\ProphecySubjectInterface');
    $node->addProperty('objectProphecyClosure', 'private');
    foreach ($node->getMethods() as $name => $method) {
        if ('__construct' === strtolower($name)) {
            continue;
        }
        if (!$method->getReturnTypeNode()
            ->hasReturnStatement()) {
            $method->setCode('$this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());');
        }
        else {
            $method->setCode('return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());');
        }
    }
    $prophecySetter = new MethodNode('setProphecy');
    $prophecyArgument = new ArgumentNode('prophecy');
    $prophecyArgument->setTypeNode(new ArgumentTypeNode('Prophecy\\Prophecy\\ProphecyInterface'));
    $prophecySetter->addArgument($prophecyArgument);
    $prophecySetter->setCode(<<<PHP
if (null === \$this->objectProphecyClosure) {
    \$this->objectProphecyClosure = static function () use (\$prophecy) {
        return \$prophecy;
    };
}
PHP
);
    $prophecyGetter = new MethodNode('getProphecy');
    $prophecyGetter->setCode('return \\call_user_func($this->objectProphecyClosure);');
    if ($node->hasMethod('__call')) {
        $__call = $node->getMethod('__call');
        \assert($__call !== null);
    }
    else {
        $__call = new MethodNode('__call');
        $__call->addArgument(new ArgumentNode('name'));
        $__call->addArgument(new ArgumentNode('arguments'));
        $node->addMethod($__call, true);
    }
    $__call->setCode(<<<PHP
throw new \\Prophecy\\Exception\\Doubler\\MethodNotFoundException(
    sprintf('Method `%s::%s()` not found.', get_class(\$this), func_get_arg(0)),
    get_class(\$this), func_get_arg(0)
);
PHP
);
    $node->addMethod($prophecySetter, true);
    $node->addMethod($prophecyGetter, true);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal