public function testVisitFunction() { $writer = new Writer(); $function = new PhpFunction(); $function->setName('foo')->addParameter(PhpParameter::create('a'))->addParameter(PhpParameter::create('b'))->setBody($writer->writeln('if ($a === $b) {')->indent()->writeln('throw new \\InvalidArgumentException(\'$a is not allowed to be the same as $b.\');')->outdent()->write("}\n\n")->write('return $b;')->getContent()); $visitor = new DefaultVisitor(); $visitor->visitFunction($function); $this->assertEquals($this->getContent('a_b_function.php'), $visitor->getContent()); }
private function parseFunction(\SimpleXMLElement $doc) { $function = new PhpFunction((string) $doc->refnamediv->refname); $function->setName((string) $doc->refnamediv->refname); $function->setAttribute('relative_path', $this->file->getRelativePathname()); $function->setAttribute('purpose', $purpose = trim((string) $doc->refnamediv->refpurpose)); if (0 === strpos($purpose, 'Alias')) { $aliasedFunction = (string) $doc->refsect1->simpara->function; $this->aliasedFunctions[$function->getName()] = $aliasedFunction; return; } $functions = array(); foreach ($doc->refsect1->methodsynopsis as $functionElem) { $cFunction = clone $function; $cFunction->setAttribute('return_type', (string) $functionElem->type); foreach ($functionElem->methodparam as $paramElem) { $cFunction->addParameter($this->createParamForElem($paramElem)); if ('...' === (string) $paramElem->parameter) { $cFunction->setAttribute('variable_parameters', true); } } $this->functions[] = $functions[] = $cFunction; } if (count($functions) === 1) { foreach ($doc->refsect1 as $refsect) { if (isset($refsect->methodsynopsis)) { continue; } $this->parseRefsect($refsect, $functions[0]); } } foreach ($functions as $parsedFunction) { $this->typeRefiner->refineFunctionTypes($parsedFunction); } }