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

Breadcrumb

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

function CallCenter::makeCall

Makes and records specific method call for object prophecy.

Parameters

ObjectProphecy<object> $prophecy:

string $methodName:

array<mixed> $arguments:

Return value

mixed Returns null if no promise for prophecy found or promise return value.

Throws

\Prophecy\Exception\Call\UnexpectedCallException If no appropriate method prophecy found

File

vendor/phpspec/prophecy/src/Prophecy/Call/CallCenter.php, line 63

Class

CallCenter
Calls receiver & manager.

Namespace

Prophecy\Call

Code

public function makeCall(ObjectProphecy $prophecy, $methodName, array $arguments) {
    // For efficiency exclude 'args' from the generated backtrace
    // Limit backtrace to last 3 calls as we don't use the rest
    $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
    $file = $line = null;
    if (isset($backtrace[2]) && isset($backtrace[2]['file']) && isset($backtrace[2]['line'])) {
        $file = $backtrace[2]['file'];
        $line = $backtrace[2]['line'];
    }
    // If no method prophecies defined, then it's a dummy, so we'll just return null
    if ('__destruct' === strtolower($methodName) || 0 == count($prophecy->getMethodProphecies())) {
        $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line);
        return null;
    }
    // There are method prophecies, so it's a fake/stub. Searching prophecy for this call
    $matches = $this->findMethodProphecies($prophecy, $methodName, $arguments);
    // If fake/stub doesn't have method prophecy for this call - throw exception
    if (!count($matches)) {
        $this->unexpectedCalls
            ->attach(new Call($methodName, $arguments, null, null, $file, $line), $prophecy);
        $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line);
        return null;
    }
    // Sort matches by their score value
    @usort($matches, function ($match1, $match2) {
        return $match2[0] - $match1[0];
    });
    $score = $matches[0][0];
    // If Highest rated method prophecy has a promise - execute it or return null instead
    $methodProphecy = $matches[0][1];
    $returnValue = null;
    $exception = null;
    if ($promise = $methodProphecy->getPromise()) {
        try {
            $returnValue = $promise->execute($arguments, $prophecy, $methodProphecy);
        } catch (\Exception $e) {
            $exception = $e;
        }
    }
    if ($methodProphecy->hasReturnVoid() && $returnValue !== null) {
        throw new MethodProphecyException("The method \"{$methodName}\" has a void return type, but the promise returned a value", $methodProphecy);
    }
    $this->recordedCalls[] = $call = new Call($methodName, $arguments, $returnValue, $exception, $file, $line);
    $call->addScore($methodProphecy->getArgumentsWildcard(), $score);
    if (null !== $exception) {
        throw $exception;
    }
    return $returnValue;
}
RSS feed
Powered by Drupal