vendor/symfony/property-info/PhpStan/NameScopeFactory.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\PropertyInfo\PhpStan;
  11. use phpDocumentor\Reflection\Types\ContextFactory;
  12. /**
  13.  * @author Baptiste Leduc <baptiste.leduc@gmail.com>
  14.  *
  15.  * @internal
  16.  */
  17. final class NameScopeFactory
  18. {
  19.     public function create(string $calledClassNamestring $declaringClassName null): NameScope
  20.     {
  21.         $declaringClassName $declaringClassName ?? $calledClassName;
  22.         $path explode('\\'$calledClassName);
  23.         $calledClassName array_pop($path);
  24.         $declaringReflection = new \ReflectionClass($declaringClassName);
  25.         [$declaringNamespace$declaringUses] = $this->extractFromFullClassName($declaringReflection);
  26.         $declaringUses array_merge($declaringUses$this->collectUses($declaringReflection));
  27.         return new NameScope($calledClassName$declaringNamespace$declaringUses);
  28.     }
  29.     private function collectUses(\ReflectionClass $reflection): array
  30.     {
  31.         $uses = [$this->extractFromFullClassName($reflection)[1]];
  32.         foreach ($reflection->getTraits() as $traitReflection) {
  33.             $uses[] = $this->extractFromFullClassName($traitReflection)[1];
  34.         }
  35.         if (false !== $parentClass $reflection->getParentClass()) {
  36.             $uses[] = $this->collectUses($parentClass);
  37.         }
  38.         return $uses array_merge(...$uses) : [];
  39.     }
  40.     private function extractFromFullClassName(\ReflectionClass $reflection): array
  41.     {
  42.         $namespace trim($reflection->getNamespaceName(), '\\');
  43.         $fileName $reflection->getFileName();
  44.         if (\is_string($fileName) && is_file($fileName)) {
  45.             if (false === $contents file_get_contents($fileName)) {
  46.                 throw new \RuntimeException(sprintf('Unable to read file "%s".'$fileName));
  47.             }
  48.             $factory = new ContextFactory();
  49.             $context $factory->createForNamespace($namespace$contents);
  50.             return [$namespace$context->getNamespaceAliases()];
  51.         }
  52.         return [$namespace, []];
  53.     }
  54. }