/** * @dataProvider provideTestParseAndDump * * @param $file * @param string $analyzer * @param string $expectedDump * @throws \PHPSA\Exception\RuntimeException * @throws \Webiny\Component\EventManager\EventManagerException */ public function testParseAndDump($file, $analyzer, $expectedDump) { $compiler = new Compiler(); $fileParser = new FileParser((new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative(array('usedAttributes' => array('comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos')))), $compiler); $context = new Context(new \Symfony\Component\Console\Output\NullOutput(), $application = new Application(), $this->getEventManager($analyzer)); $application->compiler = $compiler; $fileParser->parserFile($file, $context); $compiler->compile($context); $expectedArray = json_decode($expectedDump, true); $expectedType = $expectedArray[0]["type"]; $issues = array_map(function (Issue $issue) { $location = $issue->getLocation(); return ['type' => $issue->getCheckName(), 'message' => $issue->getDescription(), 'file' => $location->getFileName(), 'line' => $location->getLineStart()]; }, $application->getIssuesCollector()->getIssues()); foreach ($expectedArray as $check) { self::assertContains($check, $issues, $file); // every expected Issue is in the collector } foreach ($issues as $check) { if ($check["type"] == $expectedType) { self::assertContains($check, $expectedArray, $file); // there is no other issue in the collector with the same type } } }
/** * @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); } } }