Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement request: Closure #43

Open
ram108 opened this issue Oct 19, 2019 · 1 comment
Open

Improvement request: Closure #43

ram108 opened this issue Oct 19, 2019 · 1 comment

Comments

@ram108
Copy link

ram108 commented Oct 19, 2019

Is is better to use php Closure and not to write php code in strings? This issue is the only one that stops me to use soft-mocks.

As it is now:

\Badoo\SoftMocks::redefineFunction(
    'strlen',
    '$a',
    'return \\Badoo\\SoftMocks::callOriginal("strlen", [trim($a)]));'
);

Can be with closure:

\Badoo\SoftMocks::redefineFunction( 'strlen', function( $a ) {
    return \Badoo\SoftMocks::callOriginal('strlen', [trim($a)]);
}) ;
@YuriyNasretdinov
Copy link
Contributor

YuriyNasretdinov commented Feb 17, 2020

Is is better to use php Closure and not to write php code in strings?

Most of the time yes, it is better. There may be cases when you actually don't want closures, e.g. when you want to be able to access $this inside a method. It is fairly trivial to support closures though, you can even do it youself:

<?php
class SoftMocksClosures {
  private static $closures = [];
  public static function redefineFunction($func, $params, Closure $c) {
    self::$closures[] = $c;
    $idx = count(self::$closures)-1;
    \Badoo\SoftMocks::redefineFunction($func, $params, 'return call_user_func('.__CLASS__.'::getClosure('.$idx.'), '.$params.');');
  }
  public static function getClosure($i) {
    return self::$closures[$i];
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants