in testing/sample_helpers.php [7:60]
function execute_sample(string $file, string $namespace, ?array $argv)
{
// Return if sample file is not being executed via CLI
if (is_null($argv)) {
return null;
}
// Return if sample file is being included via PHPUnit
$argvFile = array_shift($argv);
if ('.php' != substr($argvFile, -4)) {
return null;
}
// Determine the name of the function to execute
$functionName = sprintf('%s\\%s', $namespace, basename($file, '.php'));
// Verify the user has supplied the correct number of arguments
$functionReflection = new ReflectionFunction($functionName);
if (
count($argv) < $functionReflection->getNumberOfRequiredParameters()
|| count($argv) > $functionReflection->getNumberOfParameters()
) {
print(get_usage(basename($file), $functionReflection));
return null;
}
// Require composer autoload for the user
$autoloadDir = dirname(dirname($functionReflection->getFileName()));
if (!file_exists($autoloadFile = $autoloadDir . '/vendor/autoload.php')) {
printf(
'You must run "composer install" in the sample root (%s/)' . PHP_EOL,
$autoloadDir
);
return null;
}
require_once $autoloadFile;
// If any parameters are typehinted as "array", explode user input on ","
$validArrayTypes = ['array', 'array<string>', 'string[]'];
$parameterReflections = $functionReflection->getParameters();
foreach (array_values($argv) as $i => $val) {
$parameterReflection = $parameterReflections[$i];
if ($parameterReflection->hasType()) {
$parameterType = $parameterReflection->getType()->getName();
if (in_array($parameterType, $validArrayTypes) && !is_array($val)) {
$key = array_search($val, $argv);
$argv[$key] = explode(',', $argv[$key]);
}
}
}
// Run the function
return call_user_func_array($functionName, $argv);
}