本文整理汇总了PHP中PhpParser\NodeTraverser::addVisitor方法的典型用法代码示例。如果您正苦于以下问题:PHP NodeTraverser::addVisitor方法的具体用法?PHP NodeTraverser::addVisitor怎么用?PHP NodeTraverser::addVisitor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpParser\NodeTraverser
的用法示例。
在下文中一共展示了NodeTraverser::addVisitor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
protected function setUp()
{
$this->traverser = new NodeTraverser();
$this->visitor = new ClassVisitor();
$this->traverser->addVisitor($this->visitor);
$this->parser = new Parser(new Lexer());
}
示例2: setUp
protected function setUp()
{
$this->parser = new \PhpParser\Parser(new \PhpParser\Lexer());
$this->traverser = new \PhpParser\NodeTraverser();
$this->sut = $this->createVisitor();
$this->traverser->addVisitor($this->sut);
}
示例3: __construct
/**
* @param \PhpParser\Parser $parser
* @param Compiler $compiler
*/
public function __construct(\PhpParser\Parser $parser, Compiler $compiler)
{
$this->nodeTraverser = new \PhpParser\NodeTraverser();
$this->nodeTraverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver());
$this->parser = $parser;
$this->compiler = $compiler;
}
示例4: getTags
/**
* Parse a file and return found tags.
*
* @param string $filePath Full path to filename.
* @return array
*/
public function getTags($filePath)
{
// Create a PHP-Parser instance.
$parser = new PhpParser(new Lexer(array('usedAttributes' => array('startLine', 'endLine', 'startFilePos', 'endFilePos'))));
// Parse the source code into a list of statements.
$source = $this->getContents($filePath);
$statements = $parser->parse($source);
$traverser = new NodeTraverser();
// Make sure all names are resolved as fully qualified.
$traverser->addVisitor(new NameResolver());
// Create visitors that turn statements into tags.
$visitors = array(new Visitor\ClassDefinition(), new Visitor\ClassReference(), new Visitor\ConstantDefinition(), new Visitor\FunctionDefinition(), new Visitor\GlobalVariableDefinition(), new Visitor\InterfaceDefinition(), new Visitor\TraitDefinition());
foreach ($visitors as $visitor) {
$traverser->addVisitor($visitor);
}
$traverser->traverse($statements);
// Extract tags from the visitors.
$tags = array();
foreach ($visitors as $visitor) {
$tags = array_merge($tags, array_map(function (Tag $tag) use($source) {
$comment = substr($source, $tag->getStartFilePos(), $tag->getEndFilePos() - $tag->getStartFilePos() + 1);
if (($bracePos = strpos($comment, '{')) !== false) {
$comment = substr($comment, 0, $bracePos) . ' {}';
}
return $tag->setComment(preg_replace('/\\s+/', ' ', $comment));
}, $visitor->getTags()));
}
return $tags;
}
示例5: __construct
public function __construct(OutputInterface $output)
{
$this->parser = new Parser(new Lexer());
$this->traverser = new NodeTraverser();
$this->nodeVisitor = new PhpStringFinder($output);
$this->traverser->addVisitor($this->nodeVisitor);
}
示例6: __construct
/**
* DefaultPhpFileExtractor constructor.
* @param DocParser $docParser
* @param FileSourceFactory $fileSourceFactory
*/
public function __construct(DocParser $docParser, FileSourceFactory $fileSourceFactory)
{
$this->docParser = $docParser;
$this->fileSourceFactory = $fileSourceFactory;
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this);
}
示例7: lint
/**
* @param $code
* @param bool $autoFix - In case of true - pretty code will be generated (avaiable by getPrettyCode method)
* @return Logger
*/
public function lint($code, $autoFix = false)
{
$this->autoFix = $autoFix;
$this->prettyCode = '';
$this->logger = new Logger();
try {
$stmts = $this->parser->parse($code);
} catch (Error $e) {
$this->logger->addRecord(new LogRecord('', '', Logger::LOGLEVEL_ERROR, $e->getMessage(), ''));
return $this->logger;
}
$traverser = new NodeTraverser();
$rulesVisitor = new RulesVisitor($this->rules, $this->autoFix);
$traverser->addVisitor($rulesVisitor);
$traverser->traverse($stmts);
$messages = $rulesVisitor->getLog();
foreach ($messages as $message) {
$this->logger->addRecord(new LogRecord($message['line'], $message['column'], $message['level'], $message['message'], $message['name']));
}
if ($autoFix) {
$prettyPrinter = new PrettyPrinter\Standard();
$this->prettyCode = $prettyPrinter->prettyPrint($stmts);
}
$sideEffectVisitor = new SideEffectsVisitor();
$traverser->addVisitor($sideEffectVisitor);
$traverser->traverse($stmts);
if ($sideEffectVisitor->isMixed()) {
$this->logger->addRecord(new LogRecord('', '', Logger::LOGLEVEL_ERROR, 'A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side' . 'effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both.', ''));
}
return $this->logger;
}
示例8: __construct
/**
* @param Parser $parser
* @param NodeTraverser $traverser
* @param TombstoneVisitor $visitor
*/
public function __construct(Parser $parser, NodeTraverser $traverser, TombstoneVisitor $visitor)
{
$this->parser = $parser;
$this->visitor = $visitor;
$this->traverser = $traverser;
$this->traverser->addVisitor($visitor);
}
示例9: __construct
/**
* @param RuleCollection $rules
* @param bool $fix
* @param book $debug
*/
public function __construct($rules, $fix = false, $debug = false)
{
$parserFactory = new ParserFactory();
$this->parser = $parserFactory->create(ParserFactory::PREFER_PHP7);
$this->traverser = new NodeTraverser();
$this->linterVisitor = new LinterVisitor($rules, $fix, $debug);
$this->traverser->addVisitor($this->linterVisitor);
}
示例10: parsePhpFileFromStringAndTraverseWithVisitor
protected function parsePhpFileFromStringAndTraverseWithVisitor(PhpFileInfo $phpFileInfo, $source, VisitorInterface $visitor)
{
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor($visitor->setPhpFileInfo($phpFileInfo));
$parser = new Parser(new Emulative());
$traverser->traverse($parser->parse($source));
return $phpFileInfo;
}
示例11: analyze
/**
* Get the fullyqualified imports and typehints.
*
* @param string $path
*
* @return string[]
*/
public function analyze($path)
{
$traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
$traverser->addVisitor($imports = new ImportVisitor());
$traverser->addVisitor($names = new NameVisitor());
$traverser->traverse($this->parser->parse(file_get_contents($path)));
return array_unique(array_merge($imports->getImports(), $names->getNames()));
}
示例12: parse
/**
* @param string $code
* @param int $lineNumber
* @return Node
*/
public function parse($code, $lineNumber)
{
$stmts = $this->parser->parse($code);
$this->nodeVisitor->setLine($lineNumber);
$this->nodeTraverser->addVisitor($this->nodeVisitor);
$this->nodeTraverser->traverse($stmts);
$node = $this->nodeVisitor->getNode();
return $node;
}
示例13: __construct
/**
* ValidationExtractor constructor.
* @param $metadataFactory
*/
public function __construct($metadataFactory)
{
if (!($metadataFactory instanceof MetadataFactoryInterface || $metadataFactory instanceof LegacyMetadataFactoryInterface || $metadataFactory instanceof ClassMetadataFactoryInterface)) {
throw new \InvalidArgumentException(sprintf('%s expects an instance of MetadataFactoryInterface or ClassMetadataFactoryInterface', get_class($this)));
}
$this->metadataFactory = $metadataFactory;
$this->traverser = new NodeTraverser();
$this->traverser->addVisitor($this);
}
示例14: modified
/**
* Get the modified AST
*
* @param string $view
* @return array
*/
protected function modified($view)
{
$ast = $this->parser->parse($view);
$traverser = new NodeTraverser();
$traverser->addVisitor(new Modifiers\RegisterDeviseTags($this->parser));
$traverser->addVisitor(new Modifiers\AddPlaceHolderTags($this->parser));
$traverser->addVisitor(new Modifiers\EchoDeviseMagic($this->parser));
return $traverser->traverse($ast);
}
示例15: __construct
public function __construct()
{
$this->registry = new Registry();
$this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$this->traverser = new NodeTraverser();
$visitors = [new NameResolver(), new ClassVisitor($this->registry), new InterfaceVisitor($this->registry), new FunctionVisitor($this->registry), new TraitVisitor($this->registry)];
foreach ($visitors as $visitor) {
$this->traverser->addVisitor($visitor);
}
}