phpspec /
prophecy
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Prophecy. |
||
| 5 | * (c) Konstantin Kudryashov <[email protected]> |
||
| 6 | * Marcello Duarte <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Prophecy\Call; |
||
| 13 | |||
| 14 | use Prophecy\Exception\Prophecy\MethodProphecyException; |
||
| 15 | use Prophecy\Prophecy\ObjectProphecy; |
||
| 16 | use Prophecy\Argument\ArgumentsWildcard; |
||
| 17 | use Prophecy\Util\StringUtil; |
||
| 18 | use Prophecy\Exception\Call\UnexpectedCallException; |
||
| 19 | use SplObjectStorage; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Calls receiver & manager. |
||
| 23 | * |
||
| 24 | * @author Konstantin Kudryashov <[email protected]> |
||
| 25 | */ |
||
| 26 | class CallCenter |
||
| 27 | { |
||
| 28 | private $util; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Call[] |
||
| 32 | */ |
||
| 33 | private $recordedCalls = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var SplObjectStorage |
||
| 37 | */ |
||
| 38 | private $unexpectedCalls; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Initializes call center. |
||
| 42 | * |
||
| 43 | * @param StringUtil $util |
||
| 44 | */ |
||
| 45 | public function __construct(StringUtil $util = null) |
||
| 46 | { |
||
| 47 | $this->util = $util ?: new StringUtil; |
||
| 48 | $this->unexpectedCalls = new SplObjectStorage(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Makes and records specific method call for object prophecy. |
||
| 53 | * |
||
| 54 | * @param ObjectProphecy $prophecy |
||
| 55 | * @param string $methodName |
||
| 56 | * @param array $arguments |
||
| 57 | * |
||
| 58 | * @return mixed Returns null if no promise for prophecy found or promise return value. |
||
| 59 | * |
||
| 60 | * @throws \Prophecy\Exception\Call\UnexpectedCallException If no appropriate method prophecy found |
||
| 61 | */ |
||
| 62 | public function makeCall(ObjectProphecy $prophecy, $methodName, array $arguments) |
||
| 63 | { |
||
| 64 | // For efficiency exclude 'args' from the generated backtrace |
||
| 65 | if (PHP_VERSION_ID >= 50400) { |
||
| 66 | // Limit backtrace to last 3 calls as we don't use the rest |
||
| 67 | // Limit argument was introduced in PHP 5.4.0 |
||
| 68 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
||
| 69 | } elseif (defined('DEBUG_BACKTRACE_IGNORE_ARGS')) { |
||
| 70 | // DEBUG_BACKTRACE_IGNORE_ARGS was introduced in PHP 5.3.6 |
||
| 71 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
||
| 72 | } else { |
||
| 73 | $backtrace = debug_backtrace(); |
||
| 74 | } |
||
| 75 | |||
| 76 | $file = $line = null; |
||
| 77 | if (isset($backtrace[2]) && isset($backtrace[2]['file'])) { |
||
| 78 | $file = $backtrace[2]['file']; |
||
| 79 | $line = $backtrace[2]['line']; |
||
| 80 | } |
||
| 81 | |||
| 82 | // If no method prophecies defined, then it's a dummy, so we'll just return null |
||
| 83 | if ('__destruct' === strtolower($methodName) || 0 == count($prophecy->getMethodProphecies())) { |
||
| 84 | $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
||
| 85 | |||
| 86 | return null; |
||
| 87 | } |
||
| 88 | |||
| 89 | // There are method prophecies, so it's a fake/stub. Searching prophecy for this call |
||
| 90 | $matches = $this->findMethodProphecies($prophecy, $methodName, $arguments); |
||
| 91 | |||
| 92 | // If fake/stub doesn't have method prophecy for this call - throw exception |
||
| 93 | if (!count($matches)) { |
||
| 94 | $this->unexpectedCalls->attach(new Call($methodName, $arguments, null, null, $file, $line), $prophecy); |
||
| 95 | $this->recordedCalls[] = new Call($methodName, $arguments, null, null, $file, $line); |
||
| 96 | |||
| 97 | return null; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Sort matches by their score value |
||
| 101 | @usort($matches, function ($match1, $match2) { return $match2[0] - $match1[0]; }); |
||
|
0 ignored issues
–
show
|
|||
| 102 | |||
| 103 | $score = $matches[0][0]; |
||
| 104 | // If Highest rated method prophecy has a promise - execute it or return null instead |
||
| 105 | $methodProphecy = $matches[0][1]; |
||
| 106 | $returnValue = null; |
||
| 107 | $exception = null; |
||
| 108 | if ($promise = $methodProphecy->getPromise()) { |
||
| 109 | try { |
||
| 110 | $returnValue = $promise->execute($arguments, $prophecy, $methodProphecy); |
||
| 111 | } catch (\Exception $e) { |
||
| 112 | $exception = $e; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($methodProphecy->hasReturnVoid() && $returnValue !== null) { |
||
| 117 | throw new MethodProphecyException( |
||
| 118 | "The method \"$methodName\" has a void return type, but the promise returned a value", |
||
| 119 | $methodProphecy |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | $this->recordedCalls[] = $call = new Call( |
||
| 124 | $methodName, $arguments, $returnValue, $exception, $file, $line |
||
| 125 | ); |
||
| 126 | $call->addScore($methodProphecy->getArgumentsWildcard(), $score); |
||
| 127 | |||
| 128 | if (null !== $exception) { |
||
| 129 | throw $exception; |
||
| 130 | } |
||
| 131 | |||
| 132 | return $returnValue; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Searches for calls by method name & arguments wildcard. |
||
| 137 | * |
||
| 138 | * @param string $methodName |
||
| 139 | * @param ArgumentsWildcard $wildcard |
||
| 140 | * |
||
| 141 | * @return Call[] |
||
| 142 | */ |
||
| 143 | public function findCalls($methodName, ArgumentsWildcard $wildcard) |
||
| 144 | { |
||
| 145 | $methodName = strtolower($methodName); |
||
| 146 | |||
| 147 | return array_values( |
||
| 148 | array_filter($this->recordedCalls, function (Call $call) use ($methodName, $wildcard) { |
||
| 149 | return $methodName === strtolower($call->getMethodName()) |
||
| 150 | && 0 < $call->getScore($wildcard) |
||
| 151 | ; |
||
| 152 | }) |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @throws UnexpectedCallException |
||
| 158 | */ |
||
| 159 | public function checkUnexpectedCalls() |
||
| 160 | { |
||
| 161 | /** @var Call $call */ |
||
| 162 | foreach ($this->unexpectedCalls as $call) { |
||
| 163 | $prophecy = $this->unexpectedCalls[$call]; |
||
| 164 | |||
| 165 | // If fake/stub doesn't have method prophecy for this call - throw exception |
||
| 166 | if (!count($this->findMethodProphecies($prophecy, $call->getMethodName(), $call->getArguments()))) { |
||
| 167 | throw $this->createUnexpectedCallException($prophecy, $call->getMethodName(), $call->getArguments()); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | private function createUnexpectedCallException(ObjectProphecy $prophecy, $methodName, |
||
| 173 | array $arguments) |
||
| 174 | { |
||
| 175 | $classname = get_class($prophecy->reveal()); |
||
| 176 | $indentationLength = 8; // looks good |
||
| 177 | $argstring = implode( |
||
| 178 | ",\n", |
||
| 179 | $this->indentArguments( |
||
| 180 | array_map(array($this->util, 'stringify'), $arguments), |
||
| 181 | $indentationLength |
||
| 182 | ) |
||
| 183 | ); |
||
| 184 | |||
| 185 | $expected = array(); |
||
| 186 | |||
| 187 | foreach (call_user_func_array('array_merge', $prophecy->getMethodProphecies()) as $methodProphecy) { |
||
| 188 | $expected[] = sprintf( |
||
| 189 | " - %s(\n" . |
||
| 190 | "%s\n" . |
||
| 191 | " )", |
||
| 192 | $methodProphecy->getMethodName(), |
||
| 193 | implode( |
||
| 194 | ",\n", |
||
| 195 | $this->indentArguments( |
||
| 196 | array_map('strval', $methodProphecy->getArgumentsWildcard()->getTokens()), |
||
| 197 | $indentationLength |
||
| 198 | ) |
||
| 199 | ) |
||
| 200 | ); |
||
| 201 | } |
||
| 202 | |||
| 203 | return new UnexpectedCallException( |
||
| 204 | sprintf( |
||
| 205 | "Unexpected method call on %s:\n". |
||
| 206 | " - %s(\n". |
||
| 207 | "%s\n". |
||
| 208 | " )\n". |
||
| 209 | "expected calls were:\n". |
||
| 210 | "%s", |
||
| 211 | |||
| 212 | $classname, $methodName, $argstring, implode("\n", $expected) |
||
| 213 | ), |
||
| 214 | $prophecy, $methodName, $arguments |
||
| 215 | |||
| 216 | ); |
||
| 217 | } |
||
| 218 | |||
| 219 | private function indentArguments(array $arguments, $indentationLength) |
||
| 220 | { |
||
| 221 | return preg_replace_callback( |
||
| 222 | '/^/m', |
||
| 223 | function () use ($indentationLength) { |
||
| 224 | return str_repeat(' ', $indentationLength); |
||
| 225 | }, |
||
| 226 | $arguments |
||
| 227 | ); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param ObjectProphecy $prophecy |
||
| 232 | * @param string $methodName |
||
| 233 | * @param array $arguments |
||
| 234 | * |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | private function findMethodProphecies(ObjectProphecy $prophecy, $methodName, array $arguments) |
||
| 238 | { |
||
| 239 | $matches = array(); |
||
| 240 | foreach ($prophecy->getMethodProphecies($methodName) as $methodProphecy) { |
||
| 241 | if (0 < $score = $methodProphecy->getArgumentsWildcard()->scoreArguments($arguments)) { |
||
| 242 | $matches[] = array($score, $methodProphecy); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | return $matches; |
||
| 247 | } |
||
| 248 | } |
||
| 249 |
If you suppress an error, we recommend checking for the error condition explicitly: