本文整理汇总了PHP中PhpParser\NodeTraverser::traverse方法的典型用法代码示例。如果您正苦于以下问题:PHP NodeTraverser::traverse方法的具体用法?PHP NodeTraverser::traverse怎么用?PHP NodeTraverser::traverse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhpParser\NodeTraverser
的用法示例。
在下文中一共展示了NodeTraverser::traverse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: resolveConstructor
/**
* @param Node\Stmt\Class_ $node
*
* @return Node\Stmt\Class_
*/
public function resolveConstructor(Node\Stmt\Class_ $node)
{
foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof Node\Stmt\ClassMethod && $stmt->name === '__construct') {
// this will make problems we need an layer above which chains the variable resolvers
// because we need may need more than this resolver
// skip constructor if is abstract
if ($stmt->isAbstract()) {
return $node;
}
// change recursivly the nodes
$subTraverser = new NodeTraverser();
foreach ($this->visitors as $visitor) {
$subTraverser->addVisitor($visitor);
}
// the table switches to a method scope
// $x = ... will be treated normal
// $this->x = ... will be stored in the above class scope and is available afterwards
$this->table->enterScope(new TableScope(TableScope::CLASS_METHOD_SCOPE));
$subTraverser->traverse($stmt->params);
$nodes = $subTraverser->traverse($stmt->stmts);
$this->table->leaveScope();
//override the old statement
$stmt->stmts = $nodes;
// override the classmethod statement in class
$node->stmts[$key] = $stmt;
// return the changed node to override it
return $node;
}
}
// no constructor defined
return $node;
}
示例2: 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;
}
示例3: walk
public function walk($fileNodes)
{
array_walk($fileNodes, function (&$nodes) {
$nodes = $this->traverser->traverse($nodes);
});
return $fileNodes;
}
示例4: parseFile
/**
* @param PhpFileInfo $phpFileInfo
*
* @return PhpFileInfo
*/
public function parseFile(PhpFileInfo $phpFileInfo)
{
foreach ($this->deprecationVisitors as $visitor) {
$visitor->setPhpFileInfo($phpFileInfo);
}
$this->traverser->traverse($this->parse($phpFileInfo->getContents()));
return $phpFileInfo;
}
示例5: itShouldAnalyseAllFiles
/**
* @test
*/
public function itShouldAnalyseAllFiles()
{
$files = [fixture('MyClass.php')];
$nodes = [$this->node];
$parseRes = $this->parser->parse(Argument::type('string'));
$this->parser->parse(Argument::type('string'))->shouldBeCalled()->willReturn($nodes);
$this->nodeTraverser->traverse($nodes)->shouldBeCalled();
$this->analyser->analyse($files);
}
示例6: 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;
}
示例7: parseFile
/**
* @param PhpFileInfo $phpFileInfo
*
* @return PhpFileInfo
*/
public function parseFile(PhpFileInfo $phpFileInfo)
{
$nodes = $this->parse($phpFileInfo->getContents());
$nodes = $this->nameResolver->traverse($nodes);
$nodes = $this->staticTraverser->traverse($nodes);
foreach ($this->violationVisitors as $visitor) {
$visitor->setPhpFileInfo($phpFileInfo);
}
$this->violationTraverser->traverse($nodes);
return $phpFileInfo;
}
示例8: scan
/**
* @param string $file
*/
public function scan($file)
{
// Set the current file used by the registry so that we can tell where the change was scanned.
$this->registry->setCurrentFile($file);
$code = file_get_contents($file);
try {
$statements = $this->parser->parse($code);
$this->traverser->traverse($statements);
} catch (Error $e) {
throw new RuntimeException('Parse Error: ' . $e->getMessage() . ' in ' . $file);
}
}
示例9: parse
/**
* @param $code
* @param $sourcePath
* @return Model
*/
public function parse($code, $sourcePath)
{
try {
$stmts = $this->parser->parse($code);
$stmts = $this->traverser->traverse($stmts);
if ($this->namespaceParser->match($stmts)) {
$this->namespaceParser->parse($stmts, $sourcePath);
}
} catch (PhpParserError $e) {
throw new ParseException('Parse Error: ' . $e->getMessage());
}
return $this->model;
}
示例10: extractTombstones
/**
* @param string $filePath
*
* @throws TombstoneExtractionException
*/
public function extractTombstones($filePath)
{
$this->visitor->setCurrentFile($filePath);
if (!is_readable($filePath)) {
throw new TombstoneExtractionException('File "' . $filePath . '" is not readable.');
}
try {
$code = file_get_contents($filePath);
$stmts = $this->parser->parse($code);
$this->traverser->traverse($stmts);
} catch (Error $e) {
throw new TombstoneExtractionException('PHP code in "' . $filePath . '" could not be parsed.', null, $e);
}
}
示例11: minify
/**
* @param array $classMap
* @param bool $noComments
*/
public function minify(array $classMap, $noComments)
{
if ($noComments) {
$this->printer->disableComments();
}
file_put_contents($this->target, "<?php ");
foreach ($classMap as $file) {
if ($stmts = $this->parser->parse(file_get_contents($file))) {
$stmts = $this->traverser->traverse($stmts);
$code = $this->printer->prettyPrintFile($stmts);
file_put_contents($this->target, $code, FILE_APPEND);
}
}
}
示例12: parse
/**
* 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;
}
示例13: implement
public function implement($class)
{
$parts = $orig_parts = explode("\\", ltrim($class, "\\"));
$real_class_parts = [];
while ($part = array_shift($parts)) {
if (strpos($part, self::CLASS_TOKEN) !== false) {
break;
}
$real_class_parts[] = $part;
}
array_unshift($parts, $part);
$types = [];
foreach ($parts as $part) {
$types[] = str_replace([self::CLASS_TOKEN, self::NS_TOKEN], ["", "\\"], $part);
}
$real_class = implode("\\", $real_class_parts);
if (!class_exists($real_class)) {
throw new \RuntimeException("Attempting to use generics on unknown class {$real_class}");
}
if (!($ast = $this->compiler->getClass($real_class))) {
throw new \RuntimeException("Attempting to use generics with non-generic class");
}
$generator = new Generator($ast->getAttribute("generics"), $types);
$traverser = new NodeTraverser();
$traverser->addVisitor($generator);
$ast = $traverser->traverse([$ast]);
$ast[0]->name = array_pop($orig_parts);
$ast[0]->extends = new Node\Name\FullyQualified($real_class_parts);
array_unshift($ast, new Node\Stmt\Namespace_(new Node\Name($orig_parts)));
return $this->prettyPrinter->prettyPrint($ast);
}
示例14: check
/**
* @param ExerciseInterface $exercise
* @param string $fileName
* @return ResultInterface
*/
public function check(ExerciseInterface $exercise, $fileName)
{
if (!$exercise instanceof FunctionRequirementsExerciseCheck) {
throw new \InvalidArgumentException();
}
$requiredFunctions = $exercise->getRequiredFunctions();
$bannedFunctions = $exercise->getBannedFunctions();
$code = file_get_contents($fileName);
try {
$ast = $this->parser->parse($code);
} catch (Error $e) {
return Failure::fromCheckAndCodeParseFailure($this, $e, $fileName);
}
$visitor = new FunctionVisitor($requiredFunctions, $bannedFunctions);
$traverser = new NodeTraverser();
$traverser->addVisitor($visitor);
$traverser->traverse($ast);
$bannedFunctions = [];
if ($visitor->hasUsedBannedFunctions()) {
$bannedFunctions = array_map(function (FuncCall $node) {
return ['function' => $node->name->__toString(), 'line' => $node->getLine()];
}, $visitor->getBannedUsages());
}
$missingFunctions = [];
if (!$visitor->hasMetFunctionRequirements()) {
$missingFunctions = $visitor->getMissingRequirements();
}
if (!empty($bannedFunctions) || !empty($missingFunctions)) {
return new FunctionRequirementsFailure($this, $bannedFunctions, $missingFunctions);
}
return Success::fromCheck($this);
}
示例15: visitPhpFile
/**
* @param \SplFileInfo $file
* @param MessageCatalogue $catalogue
* @param array $ast
*/
public function visitPhpFile(\SplFileInfo $file, MessageCatalogue $catalogue, array $ast)
{
$this->file = $file;
$this->namespace = '';
$this->catalogue = $catalogue;
$this->traverser->traverse($ast);
}