Exemple #1
0
 /**
  * @covers \PHPSA\AliasManager::isClassImported
  * @depends testAddNamespace
  */
 public function testIsClassImported()
 {
     $manager = new AliasManager();
     $this->assertNull($manager->add('WebThings'));
     $this->assertTrue($manager->isClassImported('WebThings'));
     $this->assertFalse($manager->isClassImported('AnalogStuff'));
 }
Exemple #2
0
 /**
  * @param Node\Stmt|Node\Stmt[] $topStatement
  * @param AliasManager $aliasManager
  * @param string $filepath
  */
 protected function parseTopDefinitions($topStatement, AliasManager $aliasManager, $filepath)
 {
     foreach ($topStatement as $statement) {
         if ($statement instanceof Node\Stmt\Use_) {
             if (count($statement->uses) > 0) {
                 foreach ($statement->uses as $use) {
                     $aliasManager->add($use->name->parts);
                 }
             }
         } elseif ($statement instanceof Node\Stmt\GroupUse) {
             if (count($statement->uses) > 0) {
                 foreach ($statement->uses as $use) {
                     $aliasManager->add($statement->prefix . $use->name->parts);
                 }
             }
         } elseif ($statement instanceof Node\Stmt\Trait_) {
             $definition = new TraitDefinition($statement->name, $statement);
             $definition->setFilepath($filepath);
             $definition->setNamespace($aliasManager->getNamespace());
             $definition->precompile();
             $this->compiler->addTrait($definition);
         } elseif ($statement instanceof Node\Stmt\Class_) {
             $definition = new ClassDefinition($statement->name, $statement, $statement->type);
             $definition->setFilepath($filepath);
             $definition->setNamespace($aliasManager->getNamespace());
             if ($statement->extends) {
                 $definition->setExtendsClass($statement->extends->toString());
             }
             if ($statement->implements) {
                 foreach ($statement->implements as $interface) {
                     $definition->addInterface($interface->toString());
                 }
             }
             foreach ($statement->stmts as $stmt) {
                 if ($stmt instanceof Node\Stmt\ClassMethod) {
                     $definition->addMethod(new ClassMethod($stmt->name, $stmt, $stmt->type));
                 } elseif ($stmt instanceof Node\Stmt\Property) {
                     $definition->addProperty($stmt);
                 } elseif ($stmt instanceof Node\Stmt\TraitUse) {
                     foreach ($stmt->traits as $traitPart) {
                         $traitDefinition = $this->compiler->getTrait($traitPart->toString());
                         if ($traitDefinition) {
                             $definition->mergeTrait($traitDefinition, $stmt->adaptations);
                         }
                     }
                 } elseif ($stmt instanceof Node\Stmt\ClassConst) {
                     $definition->addConst($stmt);
                 }
             }
             $this->compiler->addClass($definition);
         } elseif ($statement instanceof Node\Stmt\Function_) {
             $definition = new FunctionDefinition($statement->name, $statement);
             $definition->setFilepath($filepath);
             $definition->setNamespace($aliasManager->getNamespace());
             $this->compiler->addFunction($definition);
         }
     }
 }
Exemple #3
0
 /**
  * @param string $filepath
  * @param Parser $parser
  * @param Context $context
  */
 protected function parserFile($filepath, Parser $parser, Context $context)
 {
     $astTraverser = new \PhpParser\NodeTraverser();
     $astTraverser->addVisitor(new \PHPSA\Visitor\FunctionCall());
     $astTraverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver());
     try {
         if (!is_readable($filepath)) {
             throw new RuntimeException('File ' . $filepath . ' is not readable');
         }
         $context->debug('<comment>Precompile: ' . $filepath . '.</comment>');
         $code = file_get_contents($filepath);
         $astTree = $parser->parse($code);
         $astTraverser->traverse($astTree);
         $aliasManager = new AliasManager();
         $namespace = null;
         /**
          * Step 1 Precompile
          */
         foreach ($astTree as $topStatement) {
             if ($topStatement instanceof Node\Stmt\Namespace_) {
                 /**
                  * Namespace block can be created without NS name
                  */
                 if ($topStatement->name) {
                     $namespace = $topStatement->name->toString();
                     $aliasManager->setNamespace($namespace);
                 }
                 if ($topStatement->stmts) {
                     $this->parseTopDefinitions($topStatement->stmts, $aliasManager, $filepath, $namespace);
                 }
             } else {
                 $this->parseTopDefinitions($topStatement, $aliasManager, $filepath, $namespace);
             }
         }
         $context->clear();
     } catch (\PhpParser\Error $e) {
         $context->sytaxError($e, $filepath);
     } catch (Exception $e) {
         $context->output->writeln("<error>{$e->getMessage()}</error>");
     }
 }