/** * @dataProvider visitFunctionWithPhp7FeaturesDataProvider */ public function testVisitFunctionWithPhp7Features($filename, $function) { if (PHP_VERSION_ID < 70000) { $this->markTestSkipped('Test only valid for PHP >=7.0'); } $visitor = new DefaultVisitor(); $visitor->visitFunction($function); $this->assertEquals($this->getContent($filename . '.php'), $visitor->getContent()); }
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`; } }