Пример #1
0
 /**
  * @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());
 }
Пример #3
0
 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`;
     }
 }
Пример #4
0
 public function testVisitMethodWithCallable()
 {
     if (PHP_VERSION_ID < 50400) {
         $this->markTestSkipped('`callable` is only supported in PHP >=5.4.0');
     }
     $method = new PhpMethod();
     $parameter = new PhpParameter('bar');
     $parameter->setType('callable');
     $method->setName('foo')->addParameter($parameter);
     $visitor = new DefaultVisitor();
     $visitor->visitMethod($method);
     $this->assertEquals($this->getContent('callable_parameter.php'), $visitor->getContent());
 }
Пример #5
0
 /**
  * @param PhpClass $class
  *
  * @return string
  */
 public function generate(PhpClass $class)
 {
     $dispatcherContainer = new PhpProperty('dispatcher');
     $dispatcherContainer->setVisibility('protected');
     $class->setProperty($dispatcherContainer);
     try {
         $method = $class->getMethod('setDispatcher');
         $method->setBody('$this->dispatcher=$dispatcher;');
     } catch (\Exception $e) {
     }
     $this->generateAopMethodProxy($class, PhpClass::fromReflection(new \ReflectionClass($class->getParentClassName())));
     $this->visitor->reset();
     $this->navigator->accept($this->visitor, $class);
     return $this->visitor->getContent();
 }