You are here

class LazyDouble in Zircon Profile 8

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

Lazy double. Gives simple interface to describe double before creating it.

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

Hierarchy

Expanded class hierarchy of LazyDouble

2 files declare their use of LazyDouble
ObjectProphecy.php in vendor/phpspec/prophecy/src/Prophecy/Prophecy/ObjectProphecy.php
Prophet.php in vendor/phpspec/prophecy/src/Prophecy/Prophet.php

File

vendor/phpspec/prophecy/src/Prophecy/Doubler/LazyDouble.php, line 25

Namespace

Prophecy\Doubler
View source
class LazyDouble {
  private $doubler;
  private $class;
  private $interfaces = array();
  private $arguments = null;
  private $double;

  /**
   * Initializes lazy double.
   *
   * @param Doubler $doubler
   */
  public function __construct(Doubler $doubler) {
    $this->doubler = $doubler;
  }

  /**
   * Tells doubler to use specific class as parent one for double.
   *
   * @param string|ReflectionClass $class
   *
   * @throws \Prophecy\Exception\Doubler\ClassNotFoundException
   * @throws \Prophecy\Exception\Doubler\DoubleException
   */
  public function setParentClass($class) {
    if (null !== $this->double) {
      throw new DoubleException('Can not extend class with already instantiated double.');
    }
    if (!$class instanceof ReflectionClass) {
      if (!class_exists($class)) {
        throw new ClassNotFoundException(sprintf('Class %s not found.', $class), $class);
      }
      $class = new ReflectionClass($class);
    }
    $this->class = $class;
  }

  /**
   * Tells doubler to implement specific interface with double.
   *
   * @param string|ReflectionClass $interface
   *
   * @throws \Prophecy\Exception\Doubler\InterfaceNotFoundException
   * @throws \Prophecy\Exception\Doubler\DoubleException
   */
  public function addInterface($interface) {
    if (null !== $this->double) {
      throw new DoubleException('Can not implement interface with already instantiated double.');
    }
    if (!$interface instanceof ReflectionClass) {
      if (!interface_exists($interface)) {
        throw new InterfaceNotFoundException(sprintf('Interface %s not found.', $interface), $interface);
      }
      $interface = new ReflectionClass($interface);
    }
    $this->interfaces[] = $interface;
  }

  /**
   * Sets constructor arguments.
   *
   * @param array $arguments
   */
  public function setArguments(array $arguments = null) {
    $this->arguments = $arguments;
  }

  /**
   * Creates double instance or returns already created one.
   *
   * @return DoubleInterface
   */
  public function getInstance() {
    if (null === $this->double) {
      if (null !== $this->arguments) {
        return $this->double = $this->doubler
          ->double($this->class, $this->interfaces, $this->arguments);
      }
      $this->double = $this->doubler
        ->double($this->class, $this->interfaces);
    }
    return $this->double;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LazyDouble::$arguments private property
LazyDouble::$class private property
LazyDouble::$double private property
LazyDouble::$doubler private property
LazyDouble::$interfaces private property
LazyDouble::addInterface public function Tells doubler to implement specific interface with double.
LazyDouble::getInstance public function Creates double instance or returns already created one.
LazyDouble::setArguments public function Sets constructor arguments.
LazyDouble::setParentClass public function Tells doubler to use specific class as parent one for double.
LazyDouble::__construct public function Initializes lazy double.