Skip to content

Commit

Permalink
Merge branch 'PLATFORM-9150_add_variadic_params_info'
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrosimov Kirill committed Nov 21, 2017
2 parents 4bf1e64 + d21b472 commit 5424ae8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
10 changes: 10 additions & 0 deletions src/Badoo/SoftMocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '' || '<idx_of variadic_params>'
// return eval($__softmocksvariableforcode);
// }/** @codeCoverageIgnore */
$static = new \PhpParser\Node\Arg(
Expand All @@ -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),
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Badoo/fixtures/expected/php5.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));}}
30 changes: 20 additions & 10 deletions tests/unit/Badoo/fixtures/expected/php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));}}
10 changes: 10 additions & 0 deletions tests/unit/Badoo/fixtures/original/php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 5424ae8

Please sign in to comment.