Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created May 22, 2019 12:10
Show Gist options
  • Save kobus1998/67d80384700c415be382a3712be77023 to your computer and use it in GitHub Desktop.
Save kobus1998/67d80384700c415be382a3712be77023 to your computer and use it in GitHub Desktop.
Parse function doc block
<?php
$s = '
/**
* Send request
*
* @throws \Exception
* @param string $sEvent
* @param \com\realexpayments\remote\sdk\domain\iRequest $oRequest
* @param boolean $bReturnResponse
* @return \com\realexpayments\remote\sdk\domain\iResponse|bool
*/
';
function parseFnDoc($s)
{
preg_match_all("/\*\s.*/", $s, $matches);
$params = [];
$desc = '';
foreach(reset($matches) as $match)
{
$match = trim(str_replace("*", "", $match));
if ($match == null) {
$match = "\n";
}
if ($match[0] == '@') {
$parameters = explode(' ', $match);
$name = $parameters[0];
unset($parameters[0]);
$type = $parameters[1];
unset($parameters[1]);
$extra = implode(' ', $parameters);
$params[$name][] = [
'type' => trim($type),
'extra' => trim($extra)
];
continue;
}
$desc .= $match;
}
$desc = trim($desc);
return [
'desc' => $desc,
'params' => $params
];
}
$a = parseFnDoc($s);
print_r($a);
/*
Array
(
[desc] => Send request
[params] => Array
(
[@throws] => Array
(
[0] => Array
(
[type] => \Exception
[extra] =>
)
)
[@param] => Array
(
[0] => Array
(
[type] => string
[extra] => $sEvent
)
[1] => Array
(
[type] => \com\realexpayments\remote\sdk\domain\iRequest
[extra] => $oRequest
)
[2] => Array
(
[type] => boolean
[extra] => $bReturnResponse
)
)
[@return] => Array
(
[0] => Array
(
[type] => \com\realexpayments\remote\sdk\domain\iResponse|bool
[extra] =>
)
)
)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment