Pretty prints a file of statements (includes the opening
public prettyPrintFile ( array $stmts ) : string | ||
$stmts | array | Array of statements |
return | string | Pretty printed statements |
/** * {@inheritdoc} */ public function write(array $fileNodes) { $result = []; foreach ($fileNodes as $fileName => $nodes) { $result[$fileName] = $this->printer->prettyPrintFile($nodes); } return $result; }
/** * @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); }
/** * 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); }
/** * 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); }
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); }
/** * 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; }
/** * 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'); }
/** * 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); }
/** * 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); } }
/** * {@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); }