Kahlan\Plugin\Monkey::patch PHP Method

patch() public static method

Setup a monkey patch.
public static patch ( string $source, string $dest = null )
$source string A fully namespaced reference string.
$dest string A fully namespaced reference string.
    public static function patch($source, $dest = null)
    {
        $source = ltrim($source, '\\');
        if (is_object($source) || class_exists($source)) {
            $reference = $source;
        } else {
            $reference = null;
            if (MonkeyPatcher::blacklisted($source)) {
                throw new Exception("Monkey patching `{$source}()` is not supported by Kahlan.");
            }
        }
        $method = static::$_registered[$source] = new Method(compact('reference'));
        if (!$dest) {
            return $method;
        }
        $method->toBe($dest);
        return $method;
    }

Usage Example

Example #1
0
 /**
  * Sets the stub logic.
  *
  * @param mixed $substitute The logic.
  */
 public function toBeOK()
 {
     if (!is_string($this->_actual)) {
         throw new Exception("Error `toBeOK()` need to be applied on a fully-namespaced class or function name.");
     }
     if ($this->_isClass) {
         Monkey::patch($this->_actual, Double::classname());
     } else {
         Monkey::patch($this->_actual, function () {
         });
     }
 }
All Usage Examples Of Kahlan\Plugin\Monkey::patch