public function it_writes_nodes_to_files(PrettyPrinterAbstract $printer, Filesystem $filesystem)
 {
     $nodes = ['foo.php' => []];
     $printer->prettyPrintFile([])->willReturn('<?php namespace Foo; class Bar{}');
     $filesystem->dumpFile('foo.php', '<?php namespace Foo; class Bar{}')->shouldBeCalled();
     $this->write($nodes);
 }
 /**
  * Generates code
  */
 function it_generates_code(File $file, PrettyPrinterAbstract $printer)
 {
     $factory = new BuilderFactory();
     $file->getNamespaces()->willReturn([new PHPNamespace('test')]);
     $printer->prettyPrintFile([$factory->namespace('test')->getNode()])->willReturn('foo')->shouldBeCalled();
     $this->generate($file)->shouldReturn('foo');
 }
 /**
  * {@inheritdoc}
  */
 public function write(array $fileNodes)
 {
     $result = [];
     foreach ($fileNodes as $fileName => $nodes) {
         $result[$fileName] = $this->printer->prettyPrintFile($nodes);
     }
     return $result;
 }
Beispiel #4
0
 /**
  * @param File $file
  *
  * @return mixed
  */
 public function generate(File $file)
 {
     $nodes = [];
     $namespaces = $file->getNamespaces();
     foreach ($namespaces as $namespace) {
         $nodes[] = $this->generateNamespace($namespace);
     }
     return $this->printer->prettyPrintFile($nodes);
 }
Beispiel #5
0
 /**
  * Take a PHP array and convert it to a string
  *
  * @param array $array
  * @return string
  * @throws RetrofitException
  */
 public function printArray(array $array)
 {
     $string = var_export($array, true);
     $string = preg_replace('/\'\\$(.+)\'/', '$' . '\\1', $string);
     $statements = $this->parser->parse($string);
     if (null === $statements) {
         throw new RetrofitException('There was an error parsing the array');
     }
     return $this->printer->prettyPrintFile($statements);
 }
Beispiel #6
0
 /**
  * Write the model to a file
  *
  * @param ClassModel $classModel
  */
 public function write(ClassModel $classModel)
 {
     // create cache directory and get filename
     $path = $this->cacheDir . $this->interfaceToPath($classModel->getInterface());
     $this->filesystem->mkdir($path);
     $filename = $path . DIRECTORY_SEPARATOR . $classModel->getName() . '.php';
     // convert class model to string
     $statements = $this->classModelTransformer->transform($classModel);
     $class = $this->printer->prettyPrintFile($statements);
     // write file
     $this->filesystem->dumpFile($filename, $class);
 }
 /**
  * Prints information about a single recursive call. Override to change format.
  *
  * @param RecursiveCall $call
  */
 public function printSingleRecursiveCall(RecursiveCall $call)
 {
     // Get the code for the recursive call, but strip out comment lines
     $code = $this->nodePrinter->prettyPrint(array($call->getUsageNode()));
     $code = implode("\n", array_filter(explode("\n", $code), function ($line) {
         return !preg_match('#^[\\t ]*//#', $line);
     }));
     // Create the output that indicates where the recursive call was found
     echo "LOCATED IN FILE: " . $call->getFile()->getRealPath() . "\n";
     echo "DECLARED ON LINE #" . $call->getDeclarationNode()->getLine() . " ";
     echo "AND CALLED ON LINE #" . $call->getUsageNode()->getLine() . "\n";
     echo "CALLING CODE: {$code}\n\n";
 }
Beispiel #8
0
 /**
  * Parses a content of the file and returns a transformed one
  *
  * @param string $content Source code to parse
  *
  * @return string Transformed source code
  */
 public static function parse($content)
 {
     $astNodes = self::$parser->parse($content);
     $astNodes = self::$traverser->traverse($astNodes);
     $content = self::$printer->prettyPrintFile($astNodes);
     return $content;
 }
 /**
  * Print files
  *
  * @param File[] $files
  * @param string $directory
  */
 public function printFiles($files, $directory)
 {
     foreach ($files as $file) {
         if (!file_exists(dirname($file->getFilename()))) {
             mkdir(dirname($file->getFilename()), 0755, true);
         }
         file_put_contents($file->getFilename(), $this->prettyPrinter->prettyPrintFile([$file->getNode()]));
     }
     $this->fix($directory);
 }
 /**
  * Returns a simple human readable output for a value.
  *
  * @param \PhpParser\Node\Expr $value The value node as provided by
  *     PHP-Parser.
  *
  * @return string
  */
 protected function getRepresentationOfValue(\PhpParser\Node\Expr $value = null)
 {
     if (null === $value) {
         return '';
     }
     if (!self::$prettyPrinter) {
         self::$prettyPrinter = new PrettyPrinter();
     }
     return self::$prettyPrinter->prettyPrintExpr($value);
 }
Beispiel #11
0
 private function recursiveStructParse(Node $stmts, $isNestedKeysName, $parentKey = null)
 {
     $pattern = [];
     $previousKey = null;
     /** @var Node $var */
     foreach ($stmts->vars as $var) {
         if ($var->getType() === 'Expr_Variable') {
             list($current, $parent) = $this->parseKeyName($var->name, $isNestedKeysName);
             $pattern[$current] = null;
             if ($isNestedKeysName && !is_null($parentKey)) {
                 $previousKey = $current;
             } else {
                 $previousKey = $var->name;
             }
         } elseif ($var->getType() === 'Expr_List' && !is_null($previousKey)) {
             $pattern[$previousKey] = $this->recursiveStructParse($var, $isNestedKeysName, $previousKey);
         } elseif ($var->getType() === 'Expr_List' && is_null($previousKey)) {
             throw new \Exception("Parsing list(): syntax error. Nested list() without key. Code: " . $this->prettyPrinter->prettyPrint($var));
         }
     }
     return $pattern;
 }
Beispiel #12
0
 public function prettyPrint(array $stmts)
 {
     $output = "";
     $output .= "digraph G {\n";
     $output .= "  rankdir=LR;\n";
     $output .= "  node [shape=record];\n";
     $output .= "\n";
     $output .= parent::prettyPrint($stmts);
     $output .= "\n";
     $output .= "\n";
     $output .= "}\n";
     return $output;
 }
Beispiel #13
0
 /**
  * Print files
  *
  * @param File[] $files
  * @param string $directory
  */
 public function printFiles($files, $directory)
 {
     foreach ($files as $file) {
         if (!file_exists(dirname($file->getFilename()))) {
             mkdir(dirname($file->getFilename()), 0755, true);
         }
         file_put_contents($file->getFilename(), $this->prettyPrinter->prettyPrintFile([$file->getNode()]));
     }
     if ($this->fixer !== null) {
         $config = Config::create()->setRiskyAllowed(true)->setRules(array('@Symfony' => true, 'empty_return' => false, 'concat_without_spaces' => false, 'double_arrow_multiline_whitespaces' => false, 'unalign_equals' => false, 'unalign_double_arrow' => false, 'align_double_arrow' => true, 'align_equals' => true, 'concat_with_spaces' => true, 'newline_after_open_tag' => true, 'ordered_use' => true, 'phpdoc_order' => true, 'short_array_syntax' => true))->finder(DefaultFinder::create()->in($directory));
         $resolver = new ConfigurationResolver();
         $resolver->setDefaultConfig($config);
         $resolver->resolve();
         $this->fixer->fix($config);
     }
 }
Beispiel #14
0
 /**
  * @param Node $node
  *
  * @return string
  */
 private function getSource(Node $node)
 {
     return $this->printer->prettyPrint([$node]);
 }
 /**
  * {@inheritdoc}
  */
 public function write(array $fileNodes)
 {
     foreach ($fileNodes as $fileName => $nodes) {
         $this->filesystem->dumpFile($fileName, $this->printer->prettyPrintFile($nodes));
     }
 }
 public function create(array $nodes, $namespace)
 {
     $content = $this->printer->prettyPrintFile($nodes);
     $context = $this->contextFactory->createForNamespace($namespace ?: '\\', $content);
     return new Generator($context, $this->resolver);
 }