| Conditions | 13 |
| Paths | 66 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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' === $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]; }); |
||
|
|
|||
| 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 | |||
| 247 |
If you suppress an error, we recommend checking for the error condition explicitly: