|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace phpmock\phpunit; |
| 4 | + |
| 5 | +use phpmock\generator\MockFunctionGenerator; |
| 6 | +use PHPUnit\Framework\MockObject\Invocation; |
| 7 | +use PHPUnit\Framework\MockObject\Rule\InvocationOrder; |
| 8 | +use ReflectionClass; |
| 9 | + |
| 10 | +/** |
| 11 | + * Removes default arguments from the invocation. |
| 12 | + * |
| 13 | + * @author Markus Malkusch <[email protected]> |
| 14 | + * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations |
| 15 | + * @license http://www.wtfpl.net/txt/copying/ WTFPL |
| 16 | + * @internal |
| 17 | + */ |
| 18 | +class DefaultArgumentRemoverReturnTypes120 extends InvocationOrder |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @SuppressWarnings(PHPMD) |
| 22 | + */ |
| 23 | + public function invokedDo(Invocation $invocation): void |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @SuppressWarnings(PHPMD) |
| 29 | + */ |
| 30 | + public function matches(Invocation $invocation) : bool |
| 31 | + { |
| 32 | + $iClass = class_exists(Invocation::class); |
| 33 | + |
| 34 | + if ($iClass |
| 35 | + || $invocation instanceof Invocation\StaticInvocation |
| 36 | + ) { |
| 37 | + $this->removeDefaultArguments( |
| 38 | + $invocation, |
| 39 | + $iClass ? Invocation::class : Invocation\StaticInvocation::class |
| 40 | + ); |
| 41 | + } elseif (!$this->shouldRemoveDefaultArgumentsWithReflection($invocation)) { |
| 42 | + MockFunctionGenerator::removeDefaultArguments($invocation->parameters); |
| 43 | + } |
| 44 | + |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + public function verify() : void |
| 49 | + { |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * This method is not defined in the interface, but used in |
| 54 | + * PHPUnit_Framework_MockObject_InvocationMocker::hasMatchers(). |
| 55 | + * |
| 56 | + * @return boolean |
| 57 | + * @see \PHPUnit_Framework_MockObject_InvocationMocker::hasMatchers() |
| 58 | + */ |
| 59 | + public function hasMatchers() |
| 60 | + { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + public function toString() : string |
| 65 | + { |
| 66 | + return __CLASS__; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Remove default arguments from StaticInvocation or its children (hack) |
| 71 | + * |
| 72 | + * @SuppressWarnings(PHPMD) |
| 73 | + */ |
| 74 | + private function removeDefaultArguments(Invocation $invocation, string $class) |
| 75 | + { |
| 76 | + if ($this->shouldRemoveDefaultArgumentsWithReflection($invocation)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + $remover = function () { |
| 81 | + MockFunctionGenerator::removeDefaultArguments($this->parameters); |
| 82 | + }; |
| 83 | + |
| 84 | + $remover->bindTo($invocation, $class)(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Alternative to remove default arguments from StaticInvocation or its children (hack) |
| 89 | + * |
| 90 | + * @SuppressWarnings(PHPMD.StaticAccess) |
| 91 | + */ |
| 92 | + public static function removeDefaultArgumentsWithReflection(Invocation $invocation): Invocation |
| 93 | + { |
| 94 | + if (!(new self())->shouldRemoveDefaultArgumentsWithReflection($invocation)) { |
| 95 | + return $invocation; |
| 96 | + } |
| 97 | + |
| 98 | + $reflection = new ReflectionClass($invocation); |
| 99 | + |
| 100 | + $reflectionReturnType = $reflection->getProperty('returnType'); |
| 101 | + $reflectionReturnType->setAccessible(true); |
| 102 | + |
| 103 | + $reflectionIsOptional = $reflection->getProperty('isReturnTypeNullable'); |
| 104 | + $reflectionIsOptional->setAccessible(true); |
| 105 | + |
| 106 | + $returnType = $reflectionReturnType->getValue($invocation); |
| 107 | + |
| 108 | + if ($reflectionIsOptional->getValue($invocation)) { |
| 109 | + $returnType = '?' . $returnType; |
| 110 | + } |
| 111 | + |
| 112 | + $parameters = $invocation->parameters(); |
| 113 | + MockFunctionGenerator::removeDefaultArguments($parameters); |
| 114 | + |
| 115 | + return new Invocation( |
| 116 | + $invocation->className(), |
| 117 | + $invocation->methodName(), |
| 118 | + $parameters, |
| 119 | + $returnType, |
| 120 | + $invocation->object() |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + protected function shouldRemoveDefaultArgumentsWithReflection(Invocation $invocation) |
| 125 | + { |
| 126 | + return method_exists($invocation, 'parameters'); |
| 127 | + } |
| 128 | +} |
0 commit comments