Skip to content

Commit

Permalink
Simplified TyphoonReflector
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 2, 2024
1 parent 39654dd commit 862dcce
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions src/TyphoonReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@
use Typhoon\Reflection\Reflector\Cache\NullReflectionCache;
use Typhoon\Reflection\Reflector\Cache\SimpleReflectionCache;
use Typhoon\Reflection\Reflector\Context;
use Typhoon\Reflection\Reflector\ReflectionCache;

/**
* @api
*/
final class TyphoonReflector
{
private ?Context $context = null;

private function __construct(
private readonly ClassLoader $classLoader,
private readonly PhpParser $phpParser,
private readonly PhpDocParser $phpDocParser,
private readonly ReflectionCache $cache,
private readonly Context $context,
) {}

/**
Expand All @@ -42,6 +36,8 @@ public static function build(
?array $classLoaders = null,
TagPrioritizer $tagPrioritizer = new PHPStanOverPsalmOverOthersTagPrioritizer(),
): self {
$classLoaders ??= self::defaultClassLoaders();

if ($cache === null) {
$reflectionCache = new NullReflectionCache();
} else {
Expand All @@ -52,12 +48,12 @@ public static function build(
}
}

return new self(
classLoader: new ClassLoaderChain($classLoaders ?? self::defaultClassLoaders()),
return new self(new Context(
classLoader: new ClassLoaderChain($classLoaders),
phpParser: new PhpParser(),
phpDocParser: new PhpDocParser($tagPrioritizer),
cache: $reflectionCache,
);
));
}

/**
Expand Down Expand Up @@ -85,7 +81,7 @@ public static function defaultClassLoaders(): array
*/
public function classExists(string $name): bool
{
return $this->context()->classExists($name);
return $this->context->classExists($name);
}

/**
Expand All @@ -97,7 +93,7 @@ public function classExists(string $name): bool
public function reflectClass(string $name): ClassReflection
{
/** @var ClassReflection<T> */
return $this->context()->reflectClass($name);
return $this->context->reflectClass($name);
}

/**
Expand All @@ -107,16 +103,6 @@ public function reflectClass(string $name): ClassReflection
*/
public function reflectObject(object $object): ClassReflection
{
return $this->context()->reflectClass($object::class);
}

private function context(): Context
{
return $this->context ??= new Context(
classLoader: $this->classLoader,
phpParser: $this->phpParser,
phpDocParser: $this->phpDocParser,
cache: $this->cache,
);
return $this->context->reflectClass($object::class);
}
}

0 comments on commit 862dcce

Please sign in to comment.