You are here

class ProphecySubjectPatch in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpspec/prophecy/src/Prophecy/Doubler/ClassPatch/ProphecySubjectPatch.php \Prophecy\Doubler\ClassPatch\ProphecySubjectPatch

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

@author Konstantin Kudryashov <ever.zet@gmail.com>

Hierarchy

Expanded class hierarchy of ProphecySubjectPatch

File

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

Namespace

Prophecy\Doubler\ClassPatch
View source
class ProphecySubjectPatch implements ClassPatchInterface {

  /**
   * Always returns true.
   *
   * @param ClassNode $node
   *
   * @return bool
   */
  public function supports(ClassNode $node) {
    return true;
  }

  /**
   * Apply Prophecy functionality to class node.
   *
   * @param ClassNode $node
   */
  public function apply(ClassNode $node) {
    $node
      ->addInterface('Prophecy\\Prophecy\\ProphecySubjectInterface');
    $node
      ->addProperty('objectProphecy', 'private');
    foreach ($node
      ->getMethods() as $name => $method) {
      if ('__construct' === strtolower($name)) {
        continue;
      }
      $method
        ->setCode('return $this->getProphecy()->makeProphecyMethodCall(__FUNCTION__, func_get_args());');
    }
    $prophecySetter = new MethodNode('setProphecy');
    $prophecyArgument = new ArgumentNode('prophecy');
    $prophecyArgument
      ->setTypeHint('Prophecy\\Prophecy\\ProphecyInterface');
    $prophecySetter
      ->addArgument($prophecyArgument);
    $prophecySetter
      ->setCode('$this->objectProphecy = $prophecy;');
    $prophecyGetter = new MethodNode('getProphecy');
    $prophecyGetter
      ->setCode('return $this->objectProphecy;');
    if ($node
      ->hasMethod('__call')) {
      $__call = $node
        ->getMethod('__call');
    }
    else {
      $__call = new MethodNode('__call');
      $__call
        ->addArgument(new ArgumentNode('name'));
      $__call
        ->addArgument(new ArgumentNode('arguments'));
      $node
        ->addMethod($__call);
    }
    $__call
      ->setCode(<<<PHP
throw new \\Prophecy\\Exception\\Doubler\\MethodNotFoundException(
    sprintf('Method `%s::%s()` not found.', get_class(\$this), func_get_arg(0)),
    \$this->getProphecy(), func_get_arg(0)
);
PHP
);
    $node
      ->addMethod($prophecySetter);
    $node
      ->addMethod($prophecyGetter);
  }

  /**
   * Returns patch priority, which determines when patch will be applied.
   *
   * @return int Priority number (higher - earlier)
   */
  public function getPriority() {
    return 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProphecySubjectPatch::apply public function Apply Prophecy functionality to class node. Overrides ClassPatchInterface::apply
ProphecySubjectPatch::getPriority public function Returns patch priority, which determines when patch will be applied. Overrides ClassPatchInterface::getPriority
ProphecySubjectPatch::supports public function Always returns true. Overrides ClassPatchInterface::supports