From d21b47233b0111297c679213da5784aa4cc22cd6 Mon Sep 17 00:00:00 2001 From: Abrosimov Kirill Date: Tue, 21 Nov 2017 13:08:24 +0300 Subject: [PATCH] [PLATFORM-9150]: added variadic params info --- CHANGELOG.md | 6 +++++ src/Badoo/SoftMocks.php | 10 +++++++ tests/unit/Badoo/fixtures/expected/php5.php | 6 ++--- tests/unit/Badoo/fixtures/expected/php7.php | 30 ++++++++++++++------- tests/unit/Badoo/fixtures/original/php7.php | 10 +++++++ 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22f7005..2355edd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # SoftMocks v1 Change Log +## v1.3.3 + +There are next changes: + +- Added $variadic_params_idx (string, '' - no variadic params, otherwise - it's idx in function arguments) + ## master There are next changes: diff --git a/src/Badoo/SoftMocks.php b/src/Badoo/SoftMocks.php index c4a0bd0..e343b27 100644 --- a/src/Badoo/SoftMocks.php +++ b/src/Badoo/SoftMocks.php @@ -2005,6 +2005,7 @@ public function rewriteStmt_ClassMethod(\PhpParser\Node\Stmt\ClassMethod $Node) // if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked("self"::class, static::class, __FUNCTION__))) { // $params = [/* variables with references to them */]; // $mm_func_args = func_get_args(); + // $variadic_params_idx = '' || '' // return eval($__softmocksvariableforcode); // }/** @codeCoverageIgnore */ $static = new \PhpParser\Node\Arg( @@ -2019,6 +2020,11 @@ public function rewriteStmt_ClassMethod(\PhpParser\Node\Stmt\ClassMethod $Node) ); $params_arr = []; + $variadic_params_idx = null; + $last_param_idx = sizeof($Node->params) - 1; + if ($last_param_idx >= 0 && $Node->params[$last_param_idx]->variadic) { + $variadic_params_idx = $last_param_idx; + } foreach ($Node->params as $Par) { $params_arr[] = new \PhpParser\Node\Expr\ArrayItem( new \PhpParser\Node\Expr\Variable($Par->name), @@ -2036,6 +2042,10 @@ public function rewriteStmt_ClassMethod(\PhpParser\Node\Stmt\ClassMethod $Node) new \PhpParser\Node\Expr\Variable("params"), new \PhpParser\Node\Expr\Array_($params_arr) ), + new \PhpParser\Node\Expr\Assign( + new \PhpParser\Node\Expr\Variable("variadic_params_idx"), + new \PhpParser\Node\Scalar\String_($variadic_params_idx) + ), ]; // generators cannot return values, diff --git a/tests/unit/Badoo/fixtures/expected/php5.php b/tests/unit/Badoo/fixtures/expected/php5.php index 3e18811..c3cd95a 100644 --- a/tests/unit/Badoo/fixtures/expected/php5.php +++ b/tests/unit/Badoo/fixtures/expected/php5.php @@ -65,16 +65,16 @@ class SomeClass{ public $a = 1; - public static function getValue(){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array();return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public static function getValue(){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array();$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return \Badoo\SoftMocks::getClassConst(self::class, 'VALUE');} - public function method($string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public function method($string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return self::methodSelf($string);} - protected static function methodSelf($string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + protected static function methodSelf($string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return \Badoo\SoftMocks::callFunction(__NAMESPACE__, 'replaceSomething', array(&$string));}} diff --git a/tests/unit/Badoo/fixtures/expected/php7.php b/tests/unit/Badoo/fixtures/expected/php7.php index 0218237..e664b68 100644 --- a/tests/unit/Badoo/fixtures/expected/php7.php +++ b/tests/unit/Badoo/fixtures/expected/php7.php @@ -14,46 +14,56 @@ class SomeClass{ public $a = 1; - public function methodReturn() : string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array();return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public function methodReturn() : string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array();$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return self::methodSelf("string");} - protected static function methodSelf($string) : string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + protected static function methodSelf($string) : string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return \Badoo\SoftMocks::callFunction(__NAMESPACE__, 'replaceSomething', array(&$string));} - public function methodParam(string $string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public function methodParam(string $string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return $string;} - public function methodNullableParam(?string $string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public function methodNullableParam(?string $string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return $string;} - public function methodNullableReturn() : ?array{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array();return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public function methodNullableReturn() : ?array{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array();$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return null;} - public function methodVoidReturn() : void{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array();eval($__softmocksvariableforcode);return;/** @codeCoverageIgnore */} + public function methodVoidReturn() : void{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array();$variadic_params_idx = '';eval($__softmocksvariableforcode);return;/** @codeCoverageIgnore */} echo "something";} - public function methodNullableParamReturn(?string $string) : string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public function methodNullableParamReturn(?string $string) : string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return $string ?? "string";} - public function methodParamNullableReturn(string $string) : ?string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public function methodParamNullableReturn(string $string) : ?string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} return $string ? $string : null;} - public function methodNullableParamNullableReturn(?string $string) : ?string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + public function methodNullableParamNullableReturn(?string $string) : ?string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);$variadic_params_idx = '';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} - return $string;}} + return $string;} + + + public function methodWithOnlyVariadicParams(...$args){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($args);$variadic_params_idx = '0';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + + return \Badoo\SoftMocks::callFunction(__NAMESPACE__, 'sizeof', array($args));} + + + public function methodWithDifferentParamsTypes($a, $b, ...$args){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($a, $b, $args);$variadic_params_idx = '2';return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */} + + return $a . $b . \Badoo\SoftMocks::callFunction(__NAMESPACE__, 'sizeof', array($args));}} diff --git a/tests/unit/Badoo/fixtures/original/php7.php b/tests/unit/Badoo/fixtures/original/php7.php index 5be34ff..863d9fe 100644 --- a/tests/unit/Badoo/fixtures/original/php7.php +++ b/tests/unit/Badoo/fixtures/original/php7.php @@ -58,4 +58,14 @@ public function methodNullableParamNullableReturn(?string $string) : ?string { return $string; } + + public function methodWithOnlyVariadicParams( ...$args) + { + return sizeof($args); + } + + public function methodWithDifferentParamsTypes($a, $b, ...$args) + { + return $a . $b . sizeof($args); + } }