public function testSetIsReferenceReturned() { $func = new PhpFunction(); $this->assertFalse($func->isReferenceReturned()); $this->assertSame($func, $func->setReferenceReturned(true)); $this->assertTrue($func->isReferenceReturned()); $this->assertSame($func, $func->setReferenceReturned(false)); $this->assertFalse($func->isReferenceReturned()); }
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()); }
protected function execute(InputInterface $input, OutputInterface $output) { $pieces = array(); /** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */ $dialog = $this->getHelper('dialog'); $end = $input->getArgument('end-line'); $start = $input->getArgument('start-line'); $lines = file($input->getArgument('filename')); $slice = array_slice($lines, $start - 1, $end - $start + 1); $slice = join("", $slice); if ($input->getOption('pieces')) { // only needed when we assign pieces $output->writeln('<comment>Analyzing slice:</comment>'); $output->writeln($slice); list($pieces, $slice) = $this->processPieces($output, $dialog, $slice); } $code = new Writer(); if (!$input->getOption('function')) { foreach (array_keys($pieces) as $piece) { $code->writeln("{$piece} = null;"); } } $variable = $input->getOption('variable'); if ($input->getOption('heredoc')) { $code->writeln($this->escapeHeredoc($variable, $slice)); } else { $code->writeln($this->escapeArray($variable, $slice)); } if (null !== $input->getOption('function')) { $function = new PhpFunction($input->getOption('function')); foreach ($pieces as $piece) { $function->addParameter(new PhpParameter($piece)); } $code->writeln("return \${$variable};"); $function->setBody($code->getContent()); $visitor = new DefaultVisitor(); $visitor->visitFunction($function); $content = $visitor->getContent(); $content = preg_replace_callback('/EOF\\n.*EOF;\\n/ms', function ($m) { return preg_replace('/^\\ /ms', '', $m[0]); }, $content); } else { $content = $code->getContent(); } if ($input->getOption('pieces')) { $content = str_replace(md5(__CLASS__), '$', $content); } $output->writeln("\n{$content}"); if ($input->getOption('copy')) { $content = escapeshellarg(strtr($content, array("\t" => '\\t', '\\' => '\\\\'))); `echo {$content} | pbcopy`; } }
public function introspect(\ReflectionExtension $extension) { $classes = $functions = $constants = array(); foreach ($extension->getClasses() as $class) { assert($class instanceof \ReflectionClass); $phpClass = PhpClass::fromReflection($class); $classes[] = $phpClass; } foreach ($extension->getFunctions() as $function) { assert($function instanceof \ReflectionFunction); $phpFunction = PhpFunction::fromReflection($function); $functions[] = $phpFunction; } foreach ($extension->getConstants() as $name => $value) { $phpConstant = new PhpConstant($name); $phpConstant->setValue($value); $constants[] = $phpConstant; } return array('classes' => $classes, 'functions' => $functions, 'constants' => $constants); }
public function visitFunctionWithPhp7FeaturesDataProvider() { $builtinReturn = PhpFunction::create('foo')->setReturnType('bool'); $nonbuiltinReturn = PhpFunction::create('foo')->setReturnType('\\Foo'); return array(array('php7_builtin_return', $builtinReturn), array('php7_func_nonbuiltin_return', $nonbuiltinReturn)); }
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); } }