From a11cdd0958187d3a023241df34bd81464a586d7e Mon Sep 17 00:00:00 2001 From: gggeek Date: Sun, 17 May 2015 02:17:04 +0100 Subject: [PATCH] Improve generation of methods signature by the Wrapper class --- demo/server/server.php | 8 +++--- src/Wrapper.php | 59 ++++++++++++++++++++++++------------------ 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/demo/server/server.php b/demo/server/server.php index beae193..6e2f771 100644 --- a/demo/server/server.php +++ b/demo/server/server.php @@ -168,6 +168,11 @@ $findstate10_sig = array( "docstring" => $findstate_doc, ); +$c = new xmlrpcServerMethodsContainer; +$moreSignatures = $wrapper->wrap_php_class($c, array('prefix' => 'tests.', 'method_type' => 'all')); +var_dump($moreSignatures); +die(); + $addtwo_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcInt, Value::$xmlrpcInt)); $addtwo_doc = 'Add two integers together and return the result'; function addTwo($req) @@ -893,9 +898,6 @@ $signatures = array( ); -$wrapper = new \PhpXmlRpc\Wrapper(); -$c = new xmlrpcServerMethodsContainer; -$moreSignatures = $wrapper->wrap_php_class($c, array('prefix' => 'tests.', 'method_type' => 'all')); $signatures = array_merge($signatures, $moreSignatures); $s = new PhpXmlRpc\Server($signatures, false); diff --git a/src/Wrapper.php b/src/Wrapper.php index 6631aa7..fe4fae7 100644 --- a/src/Wrapper.php +++ b/src/Wrapper.php @@ -208,7 +208,7 @@ class Wrapper 'function' => $callable, 'signature' => $funcSigs['sigs'], 'docstring' => $funcDesc['desc'], - 'signature_docs' => $funcSigs['pSigs'], + 'signature_docs' => $funcSigs['sigsDocs'], 'source' => $code ); @@ -289,23 +289,26 @@ class Wrapper } elseif (strpos($doc, '@param') === 0) { // syntax: @param type [$name] desc if (preg_match('/@param\s+(\S+)(\s+\$\S+)?\s+(.+)/', $doc, $matches)) { - if (strpos($matches[1], '|')) { - //$paramDocs[$i]['type'] = explode('|', $matches[1]); - $paramDocs[$i]['type'] = 'mixed'; + if ($matches[2] == '' && substr($matches[3], 0, 1) == '$') { + // syntax: @param type $name + $name = strtolower(trim($matches[3])); + $paramDocs[$name]['name'] = trim($matches[3]); + $paramDocs[$name]['doc'] = ''; } else { - $paramDocs[$i]['type'] = $matches[1]; + $name = strtolower(trim($matches[2])); + $paramDocs[$name]['name'] = trim($matches[2]); + $paramDocs[$name]['doc'] = $matches[3]; } - $paramDocs[$i]['name'] = trim($matches[2]); - $paramDocs[$i]['doc'] = $matches[3]; + + $paramDocs[$name]['type'] = $matches[1]; } $i++; } elseif (strpos($doc, '@return') === 0) { - // syntax: @return type desc - //$returns = preg_split('/\s+/', $doc); - if (preg_match('/@return\s+(\S+)\s+(.+)/', $doc, $matches)) { - $returns = $this->php_2_xmlrpc_type($matches[1]); + // syntax: @return type [desc] + if (preg_match('/@return\s+(\S+)(\s+.+)?/', $doc, $matches)) { + $returns = $matches[1]; if (isset($matches[2])) { - $returnsDocs = $matches[2]; + $returnsDocs = trim($matches[2]); } } } @@ -326,7 +329,7 @@ class Wrapper 'desc' => $desc, 'docs' => $docs, 'params' => $params, - 'paramsDocs' => $paramDocs, + 'paramDocs' => $paramDocs, 'returns' => $returns, 'returnsDocs' =>$returnsDocs, ); @@ -335,6 +338,8 @@ class Wrapper /** * Given the method description given by introspection, create method signature data * + * @todo support better docs with multiple types separated by pipes by creating multiple signatures + * * @param array $funcDesc as generated by self::introspectFunction() * * @return array @@ -346,11 +351,14 @@ class Wrapper $pars = array(); $pNum = count($funcDesc['params']); foreach ($funcDesc['params'] as $param) { - if (isset($funcDesc['paramDocs'][$i]['name']) && $funcDesc['paramDocs'][$i]['name'] && - strtolower($funcDesc['paramDocs'][$i]['name']) != strtolower($param['name'])) { - // param name from phpdoc info does not match param definition! - $funcDesc['paramDocs'][$i]['type'] = 'mixed'; + /* // match by name real param and documented params + $name = strtolower($param['name']); + if (!isset($funcDesc['paramDocs'][$name])) { + $funcDesc['paramDocs'][$name] = array(); } + if (!isset($funcDesc['paramDocs'][$name]['type'])) { + $funcDesc['paramDocs'][$name]['type'] = 'mixed'; + }*/ if ($param['isoptional']) { // this particular parameter is optional. save as valid previous list of parameters @@ -371,26 +379,27 @@ class Wrapper } $sigs = array(); - $pSigs = array(); + $sigsDocs = array(); foreach ($parsVariations as $pars) { - // build a 'generic' signature (only use an appropriate return type) - $sig = array($funcDesc['returns']); + // build a signature + $sig = array($this->php_2_xmlrpc_type($funcDesc['returns'])); $pSig = array($funcDesc['returnsDocs']); for ($i = 0; $i < count($pars); $i++) { - if (isset($funcDesc['paramDocs'][$i]['type'])) { - $sig[] = $this->php_2_xmlrpc_type($funcDesc['paramDocs'][$i]['type']); + $name = strtolower($funcDesc['params'][$i]['name']); + if (isset($funcDesc['paramDocs'][$name]['type'])) { + $sig[] = $this->php_2_xmlrpc_type($funcDesc['paramDocs'][$name]['type']); } else { $sig[] = Value::$xmlrpcValue; } - $pSig[] = isset($funcDesc['paramDocs'][$i]['doc']) ? $funcDesc['paramDocs'][$i]['doc'] : ''; + $pSig[] = isset($funcDesc['paramDocs'][$name]['doc']) ? $funcDesc['paramDocs'][$name]['doc'] : ''; } $sigs[] = $sig; - $pSigs[] = $pSig; + $sigsDocs[] = $pSig; } return array( 'sigs' => $sigs, - 'pSigs' => $pSigs + 'sigsDocs' => $sigsDocs ); } -- 2.43.0