/** * Get a pretty printed string of code from a file while applying visitors. * * @param string $file * * @throws \RuntimeException * * @return string */ public function getCode($file, $comments = true) { if (!is_string($file) || empty($file)) { throw new RuntimeException('Invalid filename provided.'); } if (!is_readable($file)) { throw new RuntimeException("Cannot open {$file} for reading."); } if ($comments) { $content = file_get_contents($file); } else { $content = php_strip_whitespace($file); } $parsed = $this->parser->parse($content); $stmts = $this->traverser->traverseFile($parsed, $file); $pretty = $this->printer->prettyPrint($stmts); if (substr($pretty, 30) === '<?php declare(strict_types=1);' || substr($pretty, 30) === "<?php\ndeclare(strict_types=1);") { $pretty = substr($pretty, 32); } elseif (substr($pretty, 31) === "<?php\r\ndeclare(strict_types=1);") { $pretty = substr($pretty, 33); } elseif (substr($pretty, 5) === '<?php') { $pretty = substr($pretty, 7); } return $this->getCodeWrappedIntoNamespace($parsed, $pretty); }