Skip to content

Commit

Permalink
Fix some tests and PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypkhantc committed Aug 27, 2024
1 parent 0e2218c commit 28775bd
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private function entries(
$changed = false;

$classFinder = $classFinder->withPathFilter(static function (string $filename) use (&$entries, &$result, &$changed, $previousEntries) {
/** @var array{ data: TEntry, dependencies: ClassSnapshot, matching: bool } $entry */
$entry = $previousEntries[$filename] ?? null;

// If there's no entry in cache for this filename (new file or previously uncached),
Expand Down Expand Up @@ -124,6 +125,7 @@ private function entries(
$this->cache->set($key, $entries);
}

/** @phpstan-ignore return.type */
return $result;
}
}
1 change: 1 addition & 0 deletions src/Discovery/Cache/HardClassFinderComputedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private function entries(
$entries[$classReflection->getFileName()] = $map($classReflection);
}

/** @phpstan-ignore return.type */
return $entries;
}
}
2 changes: 2 additions & 0 deletions src/GlobControllerQueryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private function getAggregateControllerQueryProvider(): AggregateControllerQuery
*/
private function getClassList(): array
{
/** @phpstan-ignore assign.propertyType */
$this->classList ??= $this->classFinderComputedCache->compute(
$this->classFinder,
'globQueryProvider',
Expand All @@ -75,6 +76,7 @@ function (ReflectionClass $classReflection): string|null {
static fn (array $entries) => array_values(array_filter($entries)),
);

/** @phpstan-ignore return.type */
return $this->classList;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/GlobTypeMapperCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function registerAnnotations(ReflectionClass|string $sourceClass, GlobAnn
foreach ($globAnnotationsCache->getFactories() as $methodName => [$inputName, $inputClassName, $isDefault, $declaringClass]) {
if ($isDefault) {
if ($inputClassName !== null && isset($this->mapClassToFactory[$inputClassName])) {
throw DuplicateMappingException::createForFactory($inputClassName, $this->mapClassToFactory[$inputClassName][0], $this->mapClassToFactory[$inputClassName][1], $sourceClass, $methodName);
throw DuplicateMappingException::createForFactory($inputClassName, $this->mapClassToFactory[$inputClassName][0], $this->mapClassToFactory[$inputClassName][1], $className, $methodName);
}
} else {
// If this is not the default factory, let's not map the class name to the factory.
Expand Down
7 changes: 7 additions & 0 deletions src/Mappers/Root/RootTypeMapperFactoryContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Container\ContainerInterface;
use Psr\SimpleCache\CacheInterface;
use TheCodingMachine\GraphQLite\AnnotationReader;
use TheCodingMachine\GraphQLite\Cache\ClassBoundCache;
use TheCodingMachine\GraphQLite\Discovery\Cache\ClassFinderComputedCache;
use TheCodingMachine\GraphQLite\Discovery\ClassFinder;
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
Expand All @@ -30,6 +31,7 @@ public function __construct(
private readonly CacheInterface $cache,
private readonly ClassFinder $classFinder,
private readonly ClassFinderComputedCache $classFinderComputedCache,
private readonly ClassBoundCache $classBoundCache,
) {
}

Expand Down Expand Up @@ -77,4 +79,9 @@ public function getClassFinderComputedCache(): ClassFinderComputedCache
{
return $this->classFinderComputedCache;
}

public function getClassBoundCache(): ClassBoundCache
{
return $this->classBoundCache;
}
}
6 changes: 4 additions & 2 deletions src/Reflection/DocBlock/CachedDocBlockContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use phpDocumentor\Reflection\Types\Context;
use ReflectionClass;
use Reflector;
use ReflectionClassConstant;
use ReflectionMethod;
use ReflectionProperty;
use TheCodingMachine\GraphQLite\Cache\ClassBoundCache;

class CachedDocBlockContextFactory implements DocBlockContextFactory
Expand All @@ -18,7 +20,7 @@ public function __construct(
{
}

public function createFromReflector(Reflector $reflector): Context
public function createFromReflector(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionClassConstant $reflector): Context
{
$reflector = $reflector instanceof ReflectionClass ? $reflector : $reflector->getDeclaringClass();

Expand Down
6 changes: 4 additions & 2 deletions src/Reflection/DocBlock/CachedDocBlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use phpDocumentor\Reflection\DocBlock;
use ReflectionClass;
use Reflector;
use ReflectionClassConstant;
use ReflectionMethod;
use ReflectionProperty;
use TheCodingMachine\GraphQLite\Cache\ClassBoundCache;

use function md5;
Expand All @@ -23,7 +25,7 @@ public function __construct(
{
}

public function createFromReflector(Reflector $reflector): DocBlock
public function createFromReflector(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionClassConstant $reflector): DocBlock
{
$class = $reflector instanceof ReflectionClass ? $reflector : $reflector->getDeclaringClass();

Expand Down
7 changes: 5 additions & 2 deletions src/Reflection/DocBlock/DocBlockContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace TheCodingMachine\GraphQLite\Reflection\DocBlock;

use phpDocumentor\Reflection\Types\Context;
use Reflector;
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionMethod;
use ReflectionProperty;

interface DocBlockContextFactory
{
public function createFromReflector(Reflector $reflector): Context;
public function createFromReflector(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionClassConstant $reflector): Context;
}
7 changes: 5 additions & 2 deletions src/Reflection/DocBlock/DocBlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace TheCodingMachine\GraphQLite\Reflection\DocBlock;

use phpDocumentor\Reflection\DocBlock;
use Reflector;
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionMethod;
use ReflectionProperty;

interface DocBlockFactory
{
public function createFromReflector(Reflector $reflector): DocBlock;
public function createFromReflector(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionClassConstant $reflector): DocBlock;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\ContextFactory as ContextFactoryConcrete;
use Reflector;
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionMethod;
use ReflectionProperty;

class PhpDocumentorDocBlockContextFactory implements DocBlockContextFactory
{
Expand All @@ -16,7 +19,7 @@ public function __construct(
{
}

public function createFromReflector(Reflector $reflector): Context
public function createFromReflector(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionClassConstant $reflector): Context
{
return $this->contextFactory->createFromReflector($reflector);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Reflection/DocBlock/PhpDocumentorDocBlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
namespace TheCodingMachine\GraphQLite\Reflection\DocBlock;

use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlockFactory as DocBlockFactoryConcrete;
use Reflector;
use phpDocumentor\Reflection\DocBlockFactoryInterface;
use ReflectionClass;
use ReflectionClassConstant;
use ReflectionMethod;
use ReflectionProperty;

class PhpDocumentorDocBlockFactory implements DocBlockFactory
{
public function __construct(
private readonly DocBlockFactoryConcrete $docBlockFactory,
private readonly DocBlockFactoryInterface $docBlockFactory,
private readonly DocBlockContextFactory $docBlockContextFactory,
)
{
}

public function createFromReflector(Reflector $reflector): DocBlock
public function createFromReflector(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionClassConstant $reflector): DocBlock
{
$docblock = $reflector->getDocComment() ?: '/** */';
$context = $this->docBlockContextFactory->createFromReflector($reflector);
Expand Down
17 changes: 12 additions & 5 deletions src/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class SchemaFactory

private NamingStrategyInterface|null $namingStrategy = null;

private FinderInterface|null $finder = null;
private ClassFinder|FinderInterface|null $finder = null;

private SchemaConfig|null $schemaConfig = null;

Expand Down Expand Up @@ -275,7 +275,7 @@ public function setSchemaConfig(SchemaConfig $schemaConfig): self
return $this;
}

public function setFinder(FinderInterface $finder): self
public function setFinder(ClassFinder|FinderInterface $finder): self
{
$this->finder = $finder;

Expand Down Expand Up @@ -405,8 +405,9 @@ public function createSchema(): Schema
$recursiveTypeMapper,
$this->container,
$namespacedCache,
$classFinder,
$classFinderComputedCache,
classFinder: $classFinder,
classFinderComputedCache: $classFinderComputedCache,
classBoundCache: $classBoundCache,
);

$reversedRootTypeMapperFactories = array_reverse($this->rootTypeMapperFactories);
Expand Down Expand Up @@ -528,6 +529,10 @@ classBoundCache: $classBoundCache,

private function createClassFinder(): ClassFinder
{
if ($this->finder instanceof ClassFinder) {
return $this->finder;
}

// When no namespaces are specified, class finder uses all available namespaces to discover classes.
// While this is technically okay, it doesn't follow SchemaFactory's semantics that allow it's
// users to manually specify classes (see SchemaFactory::testCreateSchemaOnlyWithFactories()),
Expand All @@ -541,7 +546,9 @@ private function createClassFinder(): ClassFinder
// Because this finder may be iterated more than once, we need to make
// sure that the filesystem is only hit once in the lifetime of the application,
// as that may be expensive for larger projects or non-native filesystems.
$finder = $finder->withFileFinder(new CachedFileFinder(new DefaultFileFinder(), new ArrayAdapter()));
if ($finder instanceof ComposerFinder) {
$finder = $finder->withFileFinder(new CachedFileFinder(new DefaultFileFinder(), new ArrayAdapter()));
}

foreach ($this->namespaces as $namespace) {
$finder = $finder->inNamespace($namespace);
Expand Down
10 changes: 7 additions & 3 deletions tests/RootTypeMapperFactoryContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;
use Symfony\Component\Cache\Simple\ArrayCache;
use TheCodingMachine\GraphQLite\Cache\HardClassBoundCache;
use TheCodingMachine\GraphQLite\Containers\EmptyContainer;
use TheCodingMachine\GraphQLite\Mappers\Root\RootTypeMapperFactoryContext;
use TheCodingMachine\GraphQLite\Utils\Namespaces\NS;
Expand All @@ -20,6 +21,8 @@ public function testContext(): void
$container = new EmptyContainer();
$arrayCache = new Psr16Cache(new ArrayAdapter());
$classFinder = $this->getClassFinder('namespace');
$classFinderComputedCache = $this->getClassFinderComputedCache();
$classBoundCache = new HardClassBoundCache($arrayCache);

$context = new RootTypeMapperFactoryContext(
$this->getAnnotationReader(),
Expand All @@ -30,7 +33,8 @@ public function testContext(): void
$container,
$arrayCache,
$classFinder,
self::GLOB_TTL_SECONDS
$classFinderComputedCache,
$classBoundCache,
);

$this->assertSame($this->getAnnotationReader(), $context->getAnnotationReader());
Expand All @@ -41,7 +45,7 @@ public function testContext(): void
$this->assertSame($container, $context->getContainer());
$this->assertSame($arrayCache, $context->getCache());
$this->assertSame($classFinder, $context->getClassFinder());
$this->assertSame(self::GLOB_TTL_SECONDS, $context->getGlobTTL());
$this->assertNull($context->getMapTTL());
$this->assertSame($classFinderComputedCache, $context->getClassFinderComputedCache());
$this->assertSame($classBoundCache, $context->getClassBoundCache());
}
}

0 comments on commit 28775bd

Please sign in to comment.