/** * @param \Iterator $files * * @return Node[] */ private function parseFiles(\Iterator $files) { $nodes = []; foreach ($files as $file) { /* @var SplFileInfo $file */ $nodes[$file->getRealPath()] = $this->parser->parse($file->getContents()); } return $nodes; }
/** * @param string $code * * @return ParserResult * * @throws ParserException */ public function parse($code) { try { $stmts = $this->parser->parse($code); $visitor = $this->createVisitor(); $this->nodeTraverser->addVisitor($visitor); $this->nodeTraverser->traverse($stmts); return $visitor->getResult(); } catch (Error $e) { throw new ParserException("Failed to parse PHP file: " . $e->getMessage(), $e->getCode(), $e); } }
/** * @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); } } }
/** * @param \Iterator|SplFileInfo[] $files * @return array */ public function extractClassMapFrom(\Iterator $files) { foreach ($files as $file) { if ($stmts = $this->parser->parse($file->getContents())) { $this->traverser->traverse($stmts); $this->collector->reset(); } } $classMap = $this->collector->getClassMap(); $classMap = $this->removeUnloadableClassesFrom($classMap); $classMap = $this->sort($classMap); $classFileMap = array(); foreach ($classMap as $class) { $classFileMap[$class] = $this->classloader->findFile($class); } return $classFileMap; }
/** * Convert a ClassModel into an array of statements for the php parser * * @param ClassModel $classModel * @return array */ public function transform(ClassModel $classModel) { // create namespace $namespace = $this->factory->namespace($classModel->getNamespace()); // create class $class = $this->factory->class($classModel->getName()); $class->implement($classModel->getInterface()); // add class properties foreach ($classModel->getProperties() as $propertyModel) { $property = $this->factory->property($propertyModel->getName()); $property->setDefault($propertyModel->getDefaultValue()); switch ($propertyModel->getVisibility()) { case PropertyModel::VISIBILITY_PUBLIC: $property->makePublic(); break; case PropertyModel::VISIBILITY_PROTECTED: $property->makeProtected(); break; case PropertyModel::VISIBILITY_PRIVATE: $property->makePrivate(); break; default: throw new UnexpectedValueException('Property visibility must be public, protected, private'); } // add property to class $class->addStmt($property); } // add class methods foreach ($classModel->getMethods() as $methodModel) { $method = $this->factory->method($methodModel->getName()); $method->makePublic(); // add method body $body = '<?php ' . $methodModel->getBody(); $methodStatements = $this->parser->parse($body); if (null !== $methodStatements) { $methodStatements = $this->nodeTraverser->traverse($methodStatements); $method->addStmts($methodStatements); } // add method parameters foreach ($methodModel->getParameters() as $parameterModel) { $parameter = $this->factory->param($parameterModel->getName()); if ($parameterModel->hasTypeHint()) { $parameter->setTypeHint($parameterModel->getTypeHint()); } if ($parameterModel->isOptional()) { $parameter->setDefault($parameterModel->getDefaultValue()); } $method->addParam($parameter); } // add method to class $class->addStmt($method); } // add class to namespace $namespace->addStmt($class); // return an array of statements return [$namespace->getNode()]; }
public function analyze(SplFileInfo $file) { $analysis = new Analysis($file); try { if ($stmts = $this->parser->parse($file->getContents())) { $this->adtTraverser->bindFile($file); $adtStmts = $this->adtTraverser->getAdtStmtsBy($stmts); foreach ($adtStmts as $node) { $this->nodeTraverser->setAdt($analysis->createAdt()); $this->nodeTraverser->traverse(array($node)); } } } catch (Error $error) { $this->logger->error($error->getMessage(), array($file)); } $this->getAnalysisCollection()->attach($analysis); return $analysis; }