function enterNode(Node $node)
 {
     switch (get_class($node)) {
         case Class_::class:
             $name = $node->namespacedName ? $node->namespacedName->toString() : "anonymous class";
             if ($name) {
                 $file = $this->index->getClassFile($name);
                 if ($file) {
                     $this->output->emitError(__CLASS__, $this->filename, $node->getLine(), BaseCheck::TYPE_PARSE_ERROR, "Class {$name} already exists in {$file}.");
                 } else {
                     $this->index->addClass($name, $node, $this->filename);
                 }
                 array_push($this->classStack, $node);
             }
             break;
         case Interface_::class:
             $name = $node->namespacedName->toString();
             $this->index->addInterface($name, $node, $this->filename);
             array_push($this->classStack, $node);
             break;
         case Function_::class:
             $name = $node->namespacedName->toString();
             $this->index->addFunction($name, $node, $this->filename);
             break;
         case \PhpParser\Node\Const_::class:
             if (count($this->classStack) == 0) {
                 $defineName = strval($node->name);
                 $this->index->addDefine($defineName, $node, $this->filename);
             }
             break;
         case FuncCall::class:
             if ($node->name instanceof Node\Name) {
                 $name = strval($node->name);
                 if (strcasecmp($name, 'define') == 0 && count($node->args) >= 1 && $node->args[0]->value instanceof Node\Scalar\String_) {
                     $defineName = $node->args[0]->value->value;
                     $this->index->addDefine($defineName, $node, $this->filename);
                 }
             }
             break;
         case Trait_::class:
             $name = $node->namespacedName->toString();
             $this->index->addTrait($name, $node, $this->filename);
             array_push($this->classStack, $node);
             break;
     }
     if ($node instanceof ClassMethod && property_exists($node, 'namespacedName') && count($this->classStack) > 0) {
         $classNode = $this->classStack[count($this->classStack) - 1];
         $className = $classNode->namespacedName->toString();
         $this->index->addMethod($className, $node->name, $node);
     }
     return null;
 }
 function phase2(Config $config, OutputInterface $output, $toProcess)
 {
     $traverser1 = new NodeTraverser();
     $traverser1->addVisitor(new DocBlockNameResolver());
     $analyzer = new StaticAnalyzer($config->getBasePath(), $config->getSymbolTable(), $output, $config);
     $traverser2 = new NodeTraverser();
     $traverser2->addVisitor(new TraitImportingVisitor($config->getSymbolTable()));
     $traverser3 = new NodeTraverser();
     $traverser3->addVisitor($analyzer);
     $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
     $processingCount = 0;
     foreach ($toProcess as $file) {
         try {
             $name = Util::removeInitialPath($config->getBasePath(), $file);
             $output->output(".", $name);
             $processingCount++;
             //echo " - $processingCount:" . $file . "\n";
             $fileData = file_get_contents($file);
             $stmts = $parser->parse($fileData);
             if ($stmts) {
                 $analyzer->setFile($name);
                 $stmts = $traverser1->traverse($stmts);
                 $stmts = $traverser2->traverse($stmts);
                 $traverser3->traverse($stmts);
             }
         } catch (Error $e) {
             $output->emitError(__CLASS__, $file, 0, "Parse error", $e->getMessage());
         } catch (\Guardrail\Exceptions\UnknownTraitException $e) {
             $output->emitError(__CLASS__, $file, 0, "Unknown trait error", $e->getMessage());
         }
     }
     if ($output instanceof XUnitOutput) {
         //	print_r($output->getCounts());
     }
     return $output->getErrorCount() > 0 ? 1 : 0;
 }
 function index(Config $config, OutputInterface $output, \RecursiveIteratorIterator $it2, $stubs = false)
 {
     $baseDir = $config->getBasePath();
     $symbolTable = $config->getSymbolTable();
     $indexer = new SymbolTableIndexer($symbolTable, $output);
     $traverser1 = new NodeTraverser();
     $traverser1->addVisitor(new NameResolver());
     $traverser2 = new NodeTraverser();
     $traverser2->addVisitor($indexer);
     $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
     $configArr = $config->getConfigArray();
     $count = 0;
     foreach ($it2 as $file) {
         if (($file->getExtension() == "php" || $file->getExtension() == "inc") && $file->isFile()) {
             $name = Util::removeInitialPath($baseDir, $file->getPathname());
             if (strpos($name, "phar://") === 0) {
                 $name = str_replace(\Phar::running(), "", $name);
                 while ($name[0] == '/') {
                     $name = substr($name, 1);
                 }
                 $name = "phar://" . $name;
             }
             try {
                 if (!$stubs && isset($configArr['ignore']) && is_array($configArr['ignore']) && Util::matchesGlobs($baseDir, $file->getPathname(), $configArr['ignore'])) {
                     continue;
                 }
                 ++$count;
                 $output->output(".", " - {$count}:" . $name);
                 // If the $fileName is in our phar then make it a relative path so that files that we index don't
                 // depend on the phar file existing in a particular directory.
                 $fileData = file_get_contents($file->getPathname());
                 if ($config->shouldReindex()) {
                     $symbolTable->removeFileFromIndex($file->getPathname());
                 }
                 $indexer->setFilename($name);
                 $stmts = $parser->parse($fileData);
                 if ($stmts) {
                     $traverser1->traverse($stmts);
                     $traverser2->traverse($stmts);
                 }
             } catch (Error $e) {
                 $output->emitError(__CLASS__, $file, 0, ' Parse Error: ' . $e->getMessage() . "\n");
             }
         }
     }
     return $count;
 }
Esempio n. 4
0
 function emitErrorOnLine($file, $lineNumber, $class, $message = "")
 {
     return $this->doc->emitError(get_class($this), $file, $lineNumber, $class, $message);
 }