Set up a core function to be mocked.

@param string $function The name of the function to be mocked

@return MockedFunction
761696
public static function mock(string $function): MockedFunction
    {
        $mock = new MockedFunction($function);

        self::$mocks[] = $mock;

        $closure = new ClosureGenerator($function);

        uopz_set_return($function, $closure->generate(), true);

        return $mock;
    }
650403
public function setConvertValueFunction(callable $function)
    {
        $validator = $this->getCallableValidatorConvertValue();
        $validator->setCallableFunction($function);

        if ($validator->isValid() === false) {
            throw $validator->getException();
        }

        $this->convertValueFunction = $function;

        return $this;
    }
1344937
public function setFunc($func)
    {
        if ($func === null) {
            $this->func = $func;
            return $this;
        }

        if (!is_string($func)) {
            throw new InvalidArgumentException(
                'Function name should be a string.'
            );
        }

        $func = strtoupper($func);
        if (!in_array($func, $this->validFunc())) {
            throw new InvalidArgumentException(sprintf(
                'Function "%s" not allowed in this context.',
                $func
            ));
        }

        $this->func = $func;
        return $this;
    }
303115
public function defineFunction()
    {
        $name = $this->mock->getName();

        $parameterBuilder = new ParameterBuilder();
        $parameterBuilder->build($name);

        $data = [
            "namespace" => $this->mock->getNamespace(),
            "name"      => $name,
            "fqfn"      => $this->mock->getFQFN(),
            "signatureParameters"   => $parameterBuilder->getSignatureParameters(),
            "bodyParameters"        => $parameterBuilder->getBodyParameters(),
        ];
        $this->template->setVar($data, false);
        $definition = $this->template->render();

        eval($definition);
    }
132940
private static function forkCallMapClass(\ReflectionFunction $function): \ReflectionClass
    {
        try {
            $compile = self::$compile;
            $compile(self::createProxyCode($function));
        } catch (\ParseError $pe) {
            throw new ProxyCreationFailure(
                    'Failure while creating callable CallMap instance of '
                    . $function->getName() . '(): ' . $pe->getMessage(),
                    $pe
            );
        }

        return new \ReflectionClass($function->getName() . 'CallMapProxy');
    }
1194452
public function setHashFunction($hashFunction)
    {
        $hashFunction = strtolower($hashFunction);
        if (!in_array($hashFunction, hash_algos())) {
            throw new \InvalidArgumentException("$hashFunction is not a supported hash function");
        }
        $this->hashFunction = $hashFunction;

        return $this;
    }
1221832
public function filterWithIndex(callable $function): Seq
    {
        $result = new Seq();

        foreach ($this->data as $key => $value) {
            if (!!$function($key, $value)) {
                $result = $result->append($value);
            }
        }

        return $result;
    }
1146458
public function build($functionName)
    {
        if (!function_exists($functionName)) {
            return;
        }
        $function            = new \ReflectionFunction($functionName);
        $signatureParameters = [];
        $bodyParameters      = [];
        foreach ($function->getParameters() as $reflectionParameter) {
            if ($this->isVariadic($reflectionParameter)) {
                break;
            }
            $parameter = $reflectionParameter->isPassedByReference()
                ? "&$$reflectionParameter->name"
                : "$$reflectionParameter->name";
            
            $signatureParameter = $reflectionParameter->isOptional()
                ? sprintf("%s = '%s'", $parameter, MockFunctionGenerator::DEFAULT_ARGUMENT)
                : $parameter;

            $signatureParameters[] = $signatureParameter;
            $bodyParameters[]      = $parameter;
        }
        $this->signatureParameters = implode(", ", $signatureParameters);
        $this->bodyParameters      = implode(", ", $bodyParameters);
    }
240211
protected function get_virtual_method_code($function) {
        $function = external_api::external_function_info($function);

        // Parameters and their defaults for the method signature.
        $paramanddefaults = array();
        // Parameters for external lib call.
        $params = array();
        $paramdesc = array();
        // The method's input parameters and their respective types.
        $inputparams = array();
        // The method's output parameters and their respective types.
        $outputparams = array();

        foreach ($function->parameters_desc->keys as $name => $keydesc) {
            $param = '$' . $name;
            $paramanddefault = $param;
            if ($keydesc->required == VALUE_OPTIONAL) {
                // It does not make sense to declare a parameter VALUE_OPTIONAL. VALUE_OPTIONAL is used only for array/object key.
                throw new moodle_exception('erroroptionalparamarray', 'webservice', '', $name);
            } else if ($keydesc->required == VALUE_DEFAULT) {
                // Need to generate the default, if there is any.
                if ($keydesc instanceof external_value) {
                    if ($keydesc->default === null) {
                        $paramanddefault .= ' = null';
                    } else {
                        switch ($keydesc->type) {
                            case PARAM_BOOL:
                                $default = (int)$keydesc->default;
                                break;
                            case PARAM_INT:
                                $default = $keydesc->default;
                                break;
                            case PARAM_FLOAT;
                                $default = $keydesc->default;
                                break;
                            default:
                                $default = "'$keydesc->default'";
                        }
                        $paramanddefault .= " = $default";
                    }
                } else {
                    // Accept empty array as default.
                    if (isset($keydesc->default) && is_array($keydesc->default) && empty($keydesc->default)) {
                        $paramanddefault .= ' = array()';
                    } else {
                        // For the moment we do not support default for other structure types.
                        throw new moodle_exception('errornotemptydefaultparamarray', 'webservice', '', $name);
                    }
                }
            }

            $params[] = $param;
            $paramanddefaults[] = $paramanddefault;
            $type = $this->get_phpdoc_type($keydesc);
            $inputparams[$name]['type'] = $type;

            $paramdesc[] = '* @param ' . $type . ' $' . $name . ' ' . $keydesc->desc;
        }
        $paramanddefaults = implode(', ', $paramanddefaults);
        $paramdescstr = implode("\n ", $paramdesc);

        $serviceclassmethodbody = $this->service_class_method_body($function, $params);

        if (empty($function->returns_desc)) {
            $return = '* @return void';
        } else {
            $type = $this->get_phpdoc_type($function->returns_desc);
            $outputparams['return']['type'] = $type;
            $return = '* @return ' . $type . ' ' . $function->returns_desc->desc;
        }

        // Now create the virtual method that calls the ext implementation.
        $code = <<<EOD
/**
 * $function->description.
 *
 $paramdescstr
 $return
 */
public function $function->name($paramanddefaults) {
$serviceclassmethodbody
}
EOD;

        // Prepare the method information.
        $methodinfo = new stdClass();
        $methodinfo->name = $function->name;
        $methodinfo->inputparams = $inputparams;
        $methodinfo->outputparams = $outputparams;
        $methodinfo->description = $function->description;
        // Add the method information into the list of service methods.
        $this->servicemethods[] = $methodinfo;

        return $code;
    }
4776
protected function sqlFunction(SQLFunction $function) {
		//connect to database
		$this->mapper->connect();
	
		//build query
		$query = $this->mapper->newQuery($this->entityProfile)
		->from($this->entityProfile->getEntityTable(), Schema::DEFAULT_ALIAS)
		->select($function->getFunctionInstance());
	
		//set query condition
		if ($this->hasOption('query.filter'))
			$query->where($this->getFilter());
	
		list($sql, $args) = $query->build();
		return $this->mapper->merge($this->clean(['map.type' => $function->getDefaultType()]))->execute($sql, $args);
	}
427834
public static function compose(/* $function1, $function2, ... */)
    {
        $functions = func_get_args();

        if (count($functions) === 1) {
            static::validate($functions[0]);
            return $functions[0];
        }

        $firstFunction = array_shift($functions);

        static::validate($firstFunction);

        return function() use($firstFunction, $functions) {

            $partial = call_user_func_array(array('\Functionals\Functionals', 'compose'), $functions);

            return call_user_func($firstFunction, call_user_func_array($partial, func_get_args()));
        };
    }
1188844
public function getFunction($functionName)
    {
        $lowFunc  = strtolower($functionName);
        $cacheKey = self::CACHE_KEY_FUNCS . $lowFunc;

        $reflectedFunc = $this->cache->fetch($cacheKey);

        if (false === $reflectedFunc) {
            $reflectedFunc = new ReflectionFunction($functionName);
            $this->cache->store($cacheKey, $reflectedFunc);
        }

        return $reflectedFunc;
    }
44978
public function bind(callable $function)
    {
        // Theoretical here should be call like this:
        //  call_user_func($function, $this->run())
        // But this do not make things lazy, to cheat little bit
        // IO monad is returned and inside of it is little switch
        return static::of(function () use ($function) {
            $m = $function($this->run());

            return $m instanceof IO
                ? $m->run()
                : $m;
        });
    }
1200281
public function addFunction(&$function)
    {
        if (isset($this->_functions[$function->name()])) {
            $phpdoctor =& $this->_root->phpdoctor();
            echo "\n";
            $phpdoctor->warning('Found function '.$function->name().' again, overwriting previous version');
        }
        $this->_functions[$function->name()] =& $function;
    }
876092
public static function createCustomFunction(
        string $function,
        float $weight = self::DEFAULT_WEIGHT,
        Filter $filter = null,
        string $scoreMode = self::SCORE_MODE_AVG
    ): ScoreStrategy {
        $scoreStrategy = self::createDefault();
        $scoreStrategy->type = self::CUSTOM_FUNCTION;
        $scoreStrategy->configuration['function'] = $function;
        $scoreStrategy->weight = $weight;
        $scoreStrategy->filter = self::fixFilterFieldPath($filter);
        $scoreStrategy->scoreMode = $scoreMode;

        return $scoreStrategy;
    }
92932
protected function visitFunction(Node $function)
    {
        $this->metrics['functions']++;

        if (count($function->stmts) === 0) {
            // without implementation
            return;
        }

        $lines = $this->getLinesOfCode($function->stmts);

        foreach ($lines as $key => $count) {
            $this->metrics[$key] += $count;
        }

        $this->metrics['llocFunctions'] += $lines['lloc'];
    }
1041068
public function addTypeAuthFunction($type, callable $function)
    {
        $this->appendToArrayOrCallForEachAlias($type, function() use ($type, $function){
            $this->tAuthFunctions[$type][] = $function;
        }, function($target) use ( $function){
            $this->addTypeAuthFunction($target, $function);
        });

    }