Exemplo n.º 1
0
 /**
  * @param PathCheckSettings $checkSettings
  */
 public function check(PathCheckSettings $checkSettings)
 {
     $this->visitorResolver->setLevel($checkSettings->getMessageLevel());
     foreach ($this->visitorResolver->resolve() as $visitor) {
         $this->traverser->addVisitor($visitor);
     }
     $this->pathChecker->check($this->pathTraversableFactory->createTraversable($checkSettings->getCheckedPaths(), $checkSettings->getCheckedFileExtensions(), $checkSettings->getExcludedPaths()), $checkSettings->getUseRelativePaths());
 }
 /**
  * {@inheritDoc}
  */
 protected function setUp()
 {
     $this->collector = new UsedSymbolCollector();
     $this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
     $this->traverser = new NodeTraverser();
     $this->traverser->addVisitor(new NameResolver());
     $this->traverser->addVisitor($this->collector);
 }
Exemplo n.º 3
0
 /**
  * @param string $file
  *
  * @return RefClass[]
  * @throws RuntimeException
  */
 protected function parseFile($file)
 {
     $stmts = $this->phpParser->parse(file_get_contents($file));
     $visitor = new RefVisitor($file);
     $this->traverser->addVisitor($visitor);
     $this->traverser->traverse($stmts);
     $this->traverser->removeVisitor($visitor);
     return $visitor->getClasses();
 }
Exemplo n.º 4
0
 /**
  * @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);
     }
 }
Exemplo n.º 5
0
 /**
  * @param CheckerInterface[] $checkers
  */
 public function __construct(array $checkers)
 {
     $this->parser = new PhpParser(new KeepOriginalValueLexer());
     $this->traverser = new NodeTraverser();
     $this->parserContext = new ParserContext();
     $this->errorCollection = new ErrorCollection();
     // Resolve fully qualified name (class, interface, function, etc) to ease some process
     $this->traverser->addVisitor(new NameResolver());
     // Register all the checker that should visit the parsed files
     foreach ($checkers as $checker) {
         $checker->setParserContext($this->parserContext);
         $checker->setErrorCollection($this->errorCollection);
         $this->traverser->addVisitor($checker);
     }
 }
Exemplo n.º 6
0
 /**
  * Extracts user defined PHP Type's from a source php file.
  *
  * @param  string $package
  * @param  string $version
  * @param  string $file
  * @return array
  */
 private function getTypes($package, $version, $file)
 {
     $fullPath = $this->vendorDir . '/' . $package . '/' . $version . '/' . $file;
     $src = $this->filesystem->read($fullPath);
     $ast = $this->parser->parse($src);
     $this->traverser->addVisitor($this->typeExtracator);
     $this->traverser->traverse($ast);
     $this->traverser->removeVisitor($this->typeExtracator);
     return $this->typeExtracator->getTypes();
 }
Exemplo n.º 7
0
 /** @inheritdoc */
 public function renameConflicts(array $conflicts)
 {
     $replacements = [];
     $this->traverser->addVisitor($this->reNamer);
     foreach ($conflicts as $package => $types) {
         foreach ($types as $type => $versions) {
             foreach ($versions as $version => $files) {
                 $composer = $this->reader->setPackage($package)->setVersion($version)->getComposerObject();
                 if ($this->hasNs($type)) {
                     $split = $this->splitNsandClass($type);
                     $fromNs = $split['ns'];
                     $psrNs = $this->getPsrNs($composer, $fromNs);
                     $toNs = $psrNs . $this->sanitizeVersionNo($version);
                     $diff = str_replace($psrNs, '', $fromNs);
                     if ($psrNs != $diff . '\\') {
                         $toNs = $toNs . '\\' . $diff;
                     }
                     $newFullyQualifiedType = $toNs . '\\' . $split['class'];
                 } else {
                     $fromNs = $type;
                     $toNs = $type . '_' . $this->sanitizeVersionNo($version);
                     $newFullyQualifiedType = $toNs;
                 }
                 $this->reNamer->rename($fromNs, $toNs);
                 $replacements[] = ['package' => $package, 'version' => $version, 'originalFullyQualifiedType' => $type, 'originalNamespace' => $fromNs, 'newFullyQualifiedType' => $newFullyQualifiedType, 'newNamespace' => $toNs, 'replacedIn' => $files];
                 foreach ($files as $file) {
                     $fullPath = $this->vendorDir . '/' . $package . '/' . $version . '/' . $file;
                     $src = $this->filesystem->read($fullPath);
                     $ast = $this->parser->parse($src);
                     $newAst = $this->traverser->traverse($ast);
                     $code = $this->prettyPrinter->prettyPrintFile($newAst);
                     $this->filesystem->update($fullPath, $code);
                 }
             }
         }
     }
     $this->traverser->removeVisitor($this->reNamer);
     return $replacements;
 }
Exemplo n.º 8
0
 function it_should_pass_visitor_configuration_to_a_configuration_aware_visitor(ContainerInterface $container, ParserContext $context, NodeTraverserInterface $traverser, ConfigurableVisitorInterface $visitor)
 {
     $visitorAlias = 'configurable_visitor';
     $configuration = ['key' => 'value'];
     $this->registerVisitorId($visitorAlias, $visitorAlias);
     $this->registerContext($context);
     $this->registerOptions(['visitors' => [$visitorAlias => $configuration]]);
     $container->get($visitorAlias)->willReturn($visitor);
     $visitor->configure($configuration)->shouldBeCalled();
     $traverser->addVisitor($visitor)->shouldBeCalled();
     $this->configure($traverser);
 }
Exemplo n.º 9
0
 /**
  * Create a generator using sensible defaults when possible
  *
  * @return Generator
  */
 public function build()
 {
     if (null === $this->namespacePrefix) {
         $this->namespacePrefix = 'Dynamo';
     }
     if (null === $this->parameterDataProviderFactory) {
         $this->parameterDataProviderFactory = new ParameterDataProviderFactory();
     }
     if (null === $this->reader) {
         $this->reader = new AnnotationReader();
     }
     if (null === $this->annotationDataProviderFactory) {
         $this->annotationDataProviderFactory = new AnnotationDataProviderFactory($this->reader);
     }
     if (null === $this->methodDataProviderFactory) {
         $this->methodDataProviderFactory = new MethodDataProviderFactory($this->parameterDataProviderFactory, $this->annotationDataProviderFactory);
     }
     if (null === $this->interfaceDataProviderFactory) {
         $this->interfaceDataProviderFactory = new InterfaceDataProviderFactory($this->methodDataProviderFactory, $this->reader);
     }
     if (null === $this->parameterModelFactory) {
         $this->parameterModelFactory = new ParameterModelFactory();
     }
     if (null === $this->methodModelFactory) {
         $this->methodModelFactory = new MethodModelFactory($this->parameterModelFactory);
     }
     if (null === $this->eventDispatcher) {
         $this->eventDispatcher = new EventDispatcher();
     }
     if (null === $this->classModelFactory) {
         $this->classModelFactory = new ClassModelFactory($this->namespacePrefix, $this->interfaceDataProviderFactory, $this->methodModelFactory, $this->eventDispatcher);
     }
     if (null === $this->cacheDir) {
         $this->cacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'dynamo';
     }
     if (null === $this->filesystem) {
         $this->filesystem = new Filesystem();
     }
     if (null === $this->builderFactory) {
         $this->builderFactory = new BuilderFactory();
     }
     if (null === $this->lexer) {
         $this->lexer = new Lexer();
     }
     if (null === $this->parser) {
         $this->parser = new Parser($this->lexer);
     }
     if (null === $this->traverser) {
         $this->traverser = new NodeTraverser();
     }
     $this->traverser->addVisitor(new NameResolver());
     if (null === $this->classModelTransformer) {
         $this->classModelTransformer = new ClassModelTransformer($this->builderFactory, $this->parser, $this->traverser);
     }
     if (null === $this->printer) {
         $this->printer = new Standard();
     }
     if (null === $this->classModelCacher) {
         $this->classModelCacher = new ClassModelCacher($this->cacheDir, $this->filesystem, $this->classModelTransformer, $this->printer);
     }
     $generator = new Generator($this->classModelFactory, $this->classModelCacher);
     return $generator;
 }
Exemplo n.º 10
0
 /**
  * @param NodeTraverserInterface $traverser
  *
  * @throws \GrumPHP\Exception\RuntimeException
  */
 public function configure(NodeTraverserInterface $traverser)
 {
     $this->guardTaskHasVisitors();
     $this->guardContextIsRegistered();
     $configuredVisitors = $this->loadEnabledVisitorsForCurrentOptions();
     $configuredVisitorIds = array_keys($configuredVisitors);
     $registeredVisitors = $this->registeredVisitorIds;
     $registeredVisitorsIds = array_keys($registeredVisitors);
     $visitorIds = array_values(array_intersect($registeredVisitorsIds, $configuredVisitorIds));
     $unknownConfiguredVisitorIds = array_diff($configuredVisitorIds, $registeredVisitorsIds);
     if (count($unknownConfiguredVisitorIds)) {
         throw new RuntimeException(sprintf('Found unknown php_parser visitors: %s', implode(',', $unknownConfiguredVisitorIds)));
     }
     foreach ($visitorIds as $visitorAlias) {
         $visitorId = $registeredVisitors[$visitorAlias];
         $visitor = $this->container->get($visitorId);
         if ($visitor instanceof ContextAwareVisitorInterface) {
             $visitor->setContext($this->context);
         }
         $options = $configuredVisitors[$visitorAlias];
         if ($visitor instanceof ConfigurableVisitorInterface && is_array($options)) {
             $visitor->configure($options);
         }
         $traverser->addVisitor($visitor);
     }
     // Reset context to make sure the next configure call will actually run in the correct context:
     $this->context = null;
 }