protected function getTraverser() { $traverser = new NodeTraverser(); $traverser->addVisitor(new DirVisitor(true)); $traverser->addVisitor(new FileVisitor(true)); return $traverser; }
/** * Get the node traverser to use. * * @param bool $dir * @param bool $file * @param bool $skip * @param bool $strict * * @return \ClassPreloader\Parser\NodeTraverser */ protected function getTraverser($dir, $file, $skip, $strict) { $traverser = new NodeTraverser(); if ($dir) { $traverser->addVisitor(new DirVisitor($skip)); } if ($file) { $traverser->addVisitor(new FileVisitor($skip)); } if (!$strict) { $traverser->addVisitor(new StrictTypesVisitor()); } return $traverser; }
protected function getFullyQualifiedClassNameFromFile(string $path) : string { $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); $traverser = new NodeTraverser(); $traverser->addVisitor(new NameResolver()); $code = file_get_contents($path); $statements = $parser->parse($code); $statements = $traverser->traverse($statements); return collect($statements[0]->stmts)->filter(function ($statement) { return $statement instanceof Class_; })->map(function (Class_ $statement) { return $statement->namespacedName->toString(); })->first(); }
/** * 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); }
/** * Get the node traverser used by the command. * * @return \ClassPreloader\Parser\NodeTraverser */ protected function getTraverser() { if (!$this->traverser) { $this->traverser = new NodeTraverser(); if ($this->input->getOption('fix_dir')) { $this->traverser->addVisitor(new DirVisitor($this->input->getOption('skip_dir_file'))); } if ($this->input->getOption('fix_file')) { $this->traverser->addVisitor(new FileVisitor($this->input->getOption('skip_dir_file'))); } } return $this->traverser; }
/** * Get the class preloader used by the command. * * @return \ClassPreloader\ClassPreloader */ protected function getClassPreloader() { // Class Preloader 3.x if (class_exists(Factory::class)) { return (new Factory())->create(['skip' => true]); } $traverser = new NodeTraverser(); $traverser->addVisitor(new DirVisitor(true)); $traverser->addVisitor(new FileVisitor(true)); return new ClassPreloader(new PrettyPrinter(), new Parser(new Lexer()), $traverser); }
/** * Get the node traverser used by the command. * * @param \Symfony\Component\Console\Input\InputInterface $input * * @return \ClassPreloader\Parser\NodeTraverser */ protected function getTraverser(InputInterface $input) { $traverser = new NodeTraverser(); $skip = $input->getOption('skip_dir_file'); if ($input->getOption('fix_dir')) { $traverser->addVisitor(new DirVisitor($skip)); } if ($input->getOption('fix_file')) { $traverser->addVisitor(new FileVisitor($skip)); } return $traverser; }