You are here

public function MethodProphecy::__construct in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/phpspec/prophecy/src/Prophecy/Prophecy/MethodProphecy.php \Prophecy\Prophecy\MethodProphecy::__construct()

Initializes method prophecy.

Parameters

ObjectProphecy $objectProphecy:

string $methodName:

null|Argument\ArgumentsWildcard|array $arguments:

Throws

\Prophecy\Exception\Doubler\MethodNotFoundException If method not found

File

vendor/phpspec/prophecy/src/Prophecy/Prophecy/MethodProphecy.php, line 46

Class

MethodProphecy
Method prophecy.

Namespace

Prophecy\Prophecy

Code

public function __construct(ObjectProphecy $objectProphecy, $methodName, $arguments = null) {
  $double = $objectProphecy
    ->reveal();
  if (!method_exists($double, $methodName)) {
    throw new MethodNotFoundException(sprintf('Method `%s::%s()` is not defined.', get_class($double), $methodName), get_class($double), $methodName, $arguments);
  }
  $this->objectProphecy = $objectProphecy;
  $this->methodName = $methodName;
  $reflectedMethod = new \ReflectionMethod($double, $methodName);
  if ($reflectedMethod
    ->isFinal()) {
    throw new MethodProphecyException(sprintf("Can not add prophecy for a method `%s::%s()`\n" . "as it is a final method.", get_class($double), $methodName), $this);
  }
  if (null !== $arguments) {
    $this
      ->withArguments($arguments);
  }
  if (version_compare(PHP_VERSION, '7.0', '>=') && true === $reflectedMethod
    ->hasReturnType()) {
    $type = (string) $reflectedMethod
      ->getReturnType();
    $this
      ->will(function () use ($type) {
      switch ($type) {
        case 'string':
          return '';
        case 'float':
          return 0.0;
        case 'int':
          return 0;
        case 'bool':
          return false;
        case 'array':
          return array();
        case 'callable':
        case 'Closure':
          return function () {
          };
        case 'Traversable':
        case 'Generator':

          // Remove eval() when minimum version >=5.5
          $generator = eval('return function () { yield; };');
          return $generator();
        default:
          $prophet = new Prophet();
          return $prophet
            ->prophesize($type)
            ->reveal();
      }
    });
  }
}