<commit_before><?php
/**
 * Part of CI PHPUnit Test
 *
 * @author     <NAME> <https://github.com/kenjis>
 * @license    MIT License
 * @copyright  2015 <NAME>
 * @link       https://github.com/kenjis/ci-phpunit-test
 */

namespace Kenjis\MonkeyPatch\Patcher\FunctionPatcher;

class_alias('Kenjis\MonkeyPatch\Patcher\FunctionPatcher\Proxy', '__FuncProxy__');

use LogicException;
use ReflectionFunction;

use Kenjis\MonkeyPatch\Patcher\FunctionPatcher;
use Kenjis\MonkeyPatch\MonkeyPatchManager;

class Proxy
{
	private static $mocks = [];

	public static function patch__($function, $return_value)
	{
		if (FunctionPatcher::isBlacklisted($function))
		{
			throw new LogicException('Can\'t patch on ' . $function);
		}

		self::$mocks[$function] = $return_value;
	}

	public static function reset__()
	{
		self::$mocks = [];
	}

	public static function __callStatic($function, array $arguments)
	{
		if (isset(self::$mocks[$function]))
		{
			if (is_callable(self::$mocks[$function]))
			{
				$callable = self::$mocks[$function];
				return call_user_func_array($callable, $arguments);
			}

			return self::$mocks[$function];
		}

		self::checkPassedByReference($function);

		return call_user_func_array($function, $arguments);
	}

	protected static function checkPassedByReference($function)
	{
		$ref_func = new ReflectionFunction($function);

		foreach ($ref_func->getParameters() as $param)
		{
			if ($param->isPassedByReference())
			{
				// Add tmp blacklist
				$tmp_blacklist_file = MonkeyPatchManager::getTmpBlacklistFile();
				var_dump($tmp_blacklist_file);
				file_put_contents(
					$tmp_blacklist_file, $function . "\n", FILE_APPEND
				);

				// Remove cache file
				$backtrace = debug_backtrace();
				$orig_file = $backtrace[1]['file'];
				$cache = MonkeyPatchManager::getSrcCacheFilePath($orig_file);
				@unlink($cache);

				$pr_msg = '';
				if (self::isInternalFunction($function))
				{
					$pr_msg = 'Please send Pull Request to add function "' . $function . '" to default config.';
				}

				$red_begin = "\033[41m";
				$red_end   = "\033[0m";

				$msg = 
					"\n"
					. "<red>Can't patch on function \"$function\".</red>\n"
					. "It has param(s) passed by reference.\n"
					. "Added it temporary blacklist file \"$tmp_blacklist_file\".\n"
					. "And removed cache file \"$cache\"\n\n"
					. "<red>$pr_msg</red>\n"
					. "<red>Please run phpunit again.</red>\n";
				$msg = str_replace(
					['<red>', '</red>'], [$red_begin, $red_end], $msg
				);
				echo $msg;

				exit(1);
			}<commit_msg>Fix function patcher error message<commit_after><?php
/**
 * Part of CI PHPUnit Test
 *
 * @author     <NAME> <https://github.com/kenjis>
 * @license    MIT License
 * @copyright  2015 <NAME>
 * @link       https://github.com/kenjis/ci-phpunit-test
 */

namespace Kenjis\MonkeyPatch\Patcher\FunctionPatcher;

class_alias('Kenjis\MonkeyPatch\Patcher\FunctionPatcher\Proxy', '__FuncProxy__');

use LogicException;
use ReflectionFunction;

use Kenjis\MonkeyPatch\Patcher\FunctionPatcher;
use Kenjis\MonkeyPatch\MonkeyPatchManager;

class Proxy
{
	private static $mocks = [];

	public static function patch__($function, $return_value)
	{
		if (FunctionPatcher::isBlacklisted($function))
		{
			throw new LogicException('Can\'t patch on ' . $function);
		}

		self::$mocks[$function] = $return_value;
	}

	public static function reset__()
	{
		self::$mocks = [];
	}

	public static function __callStatic($function, array $arguments)
	{
		if (isset(self::$mocks[$function]))
		{
			if (is_callable(self::$mocks[$function]))
			{
				$callable = self::$mocks[$function];
				return call_user_func_array($callable, $arguments);
			}

			return self::$mocks[$function];
		}

		self::checkPassedByReference($function);

		return call_user_func_array($function, $arguments);
	}

	protected static function checkPassedByReference($function)
	{
		$ref_func = new ReflectionFunction($function);

		foreach ($ref_func->getParameters() as $param)
		{
			if ($param->isPassedByReference())
			{
				// Add tmp blacklist
				$tmp_blacklist_file = MonkeyPatchManager::getTmpBlacklistFile();
				var_dump($tmp_blacklist_file);
				file_put_contents(
					$tmp_blacklist_file, $function . "\n", FILE_APPEND
				);

				// Remove cache file
				$backtrace = debug_backtrace();
				$orig_file = $backtrace[1]['file'];
				$cache = MonkeyPatchManager::getSrcCacheFilePath($orig_file);
				@unlink($cache);

				$pr_msg = '';
				if (self::isInternalFunction($function))
				{
					$pr_msg = "<red>Please send Pull Request to add function \"$function\" to default config.</red>\n";
				}

				$red_begin = "\033[41m";
				$red_end   = "\033[0m";

				$msg = 
					"\n"
					. "<red>Can't patch on function \"$function\".</red>\n"
					. "It has param(s) passed by reference.\n"
					. "Added it temporary blacklist file \"$tmp_blacklist_file\".\n"
					. "And removed cache file \"$cache\"\n\n"
					. "$pr_msg"
					. "<red>Please run phpunit again.</red>\n";
				$msg = str_replace(
					['<red>', '</red>'], [$red_begin, $red_end], $msg
				);
				echo $msg;

				exit(1);
			}
{'fraction_non_alphanumeric': 0.15740922473012758, 'fraction_numerical': 0.005888125613346418, 'mean_word_length': 3.688132474701012, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2772154', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1723, 'n_words': 338}