Skip to content

Commit bde2df3

Browse files
committed
Improve Test Coverage for added new features
1 parent 65bb64f commit bde2df3

6 files changed

Lines changed: 193 additions & 9 deletions

File tree

‎src/Internal/Resolver/SpecialTypeResolver.php‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public static function resolve(TypeNode $node, \ReflectionClass|\ReflectionFunct
207207

208208
if ($node instanceof GenericTypeNode) {
209209
$genericType = self::resolve($node->type, $context, $thisObj);
210-
$innerTypes = array_map(fn ($t) => self::resolve($t, $context, $thisObj), $node->genericTypes);
210+
$innerTypes = array_map(fn($t) => self::resolve($t, $context, $thisObj), $node->genericTypes);
211211

212212
return new GenericTypeNode(
213213
$genericType instanceof IdentifierTypeNode ? $genericType : $node->type,
@@ -261,11 +261,11 @@ public static function resolve(TypeNode $node, \ReflectionClass|\ReflectionFunct
261261
}
262262

263263
if ($node instanceof UnionTypeNode) {
264-
return new UnionTypeNode(array_map(fn ($t) => self::resolve($t, $context, $thisObj), $node->types));
264+
return new UnionTypeNode(array_map(fn($t) => self::resolve($t, $context, $thisObj), $node->types));
265265
}
266266

267267
if ($node instanceof IntersectionTypeNode) {
268-
return new IntersectionTypeNode(array_map(fn ($t) => self::resolve($t, $context, $thisObj), $node->types));
268+
return new IntersectionTypeNode(array_map(fn($t) => self::resolve($t, $context, $thisObj), $node->types));
269269
}
270270

271271
return $node;
@@ -299,7 +299,7 @@ public static function resolveForFile(TypeNode $node, string $file): TypeNode
299299

300300
if ($node instanceof GenericTypeNode) {
301301
$genericType = self::resolveForFile($node->type, $file);
302-
$innerTypes = array_map(fn ($t) => self::resolveForFile($t, $file), $node->genericTypes);
302+
$innerTypes = array_map(fn($t) => self::resolveForFile($t, $file), $node->genericTypes);
303303

304304
return new GenericTypeNode(
305305
$genericType instanceof IdentifierTypeNode ? $genericType : $node->type,
@@ -353,11 +353,11 @@ public static function resolveForFile(TypeNode $node, string $file): TypeNode
353353
}
354354

355355
if ($node instanceof UnionTypeNode) {
356-
return new UnionTypeNode(array_map(fn ($t) => self::resolveForFile($t, $file), $node->types));
356+
return new UnionTypeNode(array_map(fn($t) => self::resolveForFile($t, $file), $node->types));
357357
}
358358

359359
if ($node instanceof IntersectionTypeNode) {
360-
return new IntersectionTypeNode(array_map(fn ($t) => self::resolveForFile($t, $file), $node->types));
360+
return new IntersectionTypeNode(array_map(fn($t) => self::resolveForFile($t, $file), $node->types));
361361
}
362362

363363
return clone $node;
@@ -873,6 +873,10 @@ public static function getClassTraitUseDocs(string $className): array
873873
$startLine = $ref->getStartLine();
874874
$endLine = $ref->getEndLine();
875875

876+
if ($startLine === false || $endLine === false) {
877+
return self::$classTraitUseDocs[$className] = [];
878+
}
879+
876880
if (! isset(self::$anonymousTraitUseDocs[$normalizedFile]) && file_exists($fileName)) {
877881
$source = file_get_contents($fileName);
878882
if ($source !== false) {

‎src/Internal/Util/PathMatcher.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public static function mayPathBeIncluded(string $normalizedPath): bool
291291
}
292292

293293
if (str_starts_with($canon, 'var/')
294-
|| (!str_starts_with($canon, '/var/') && str_contains($canon, '/var/'))
294+
|| (! str_starts_with($canon, '/var/') && str_contains($canon, '/var/'))
295295
|| (str_starts_with($canon, '/var/') && str_contains(substr($canon, 4), '/var/'))
296296
) {
297297
if (! self::hasIncludeMatchingPrefix('var/', $includes)) {

‎tests/Internal/Io/StreamWrapperTest.php‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use TypePHP\Internal\Io\StreamWrapper;
6+
use TypePHP\Internal\Resolver\SpecialTypeResolver;
67
use TypePHP\Internal\Util\Config;
78
use TypePHP\Internal\Util\FileFilter;
89

@@ -577,4 +578,77 @@ function dedicatedStreamAction(int $id): int
577578
}
578579
});
579580
});
581+
582+
describe('Anonymous Class Trait Metadata AST Extraction', function () {
583+
test('extracts and seeds trait docblocks for anonymous classes located inside methods during transformSource', function () {
584+
$source = <<<'PHP'
585+
<?php
586+
587+
namespace App\Test;
588+
589+
trait LoggerTraitFixture {}
590+
591+
class ServiceFactory
592+
{
593+
public function create()
594+
{
595+
return new class () {
596+
/**
597+
* @use LoggerTraitFixture<string>
598+
*/
599+
use LoggerTraitFixture;
600+
};
601+
}
602+
}
603+
PHP;
604+
StreamWrapper::transformSource($source, 'VirtualServiceFactory.php');
605+
606+
$obj = (new class () {
607+
public function create(): object
608+
{
609+
return new class () {
610+
/** @use \TypePHP\Tests\Fixtures\Generics\GenericItemLoggerTrait<int> */
611+
use TypePHP\Tests\Fixtures\Generics\GenericItemLoggerTrait;
612+
};
613+
}
614+
})->create();
615+
616+
$docs = SpecialTypeResolver::getClassTraitUseDocs($obj::class);
617+
expect($docs)->not()->toBeEmpty()
618+
->and($docs[0])->toContain('GenericItemLoggerTrait<int>')
619+
;
620+
});
621+
622+
test('handles multiple anonymous classes in the same file with different trait uses without collision', function () {
623+
$source = <<<'PHP'
624+
<?php
625+
626+
namespace App\Test;
627+
628+
trait TraitA {}
629+
trait TraitB {}
630+
631+
class MultiAnonFactory
632+
{
633+
public function first()
634+
{
635+
return new class () {
636+
/** @use TraitA<int> */
637+
use TraitA;
638+
};
639+
}
640+
641+
public function second()
642+
{
643+
return new class () {
644+
/** @use TraitB<string> */
645+
use TraitB;
646+
};
647+
}
648+
}
649+
PHP;
650+
$transformed = StreamWrapper::transformSource($source, 'VirtualMultiAnon.php');
651+
expect($transformed)->toContain('class MultiAnonFactory');
652+
});
653+
});
580654
});

‎tests/Internal/Resolver/SpecialTypeResolverTest.php‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,109 @@ class SampleDemo
397397
expect($imports)->toBeEmpty();
398398
});
399399
});
400+
401+
describe('Anonymous Class Trait Use DocBlock Extraction & Scope Tracking', function () {
402+
test('parses and extracts trait use docblocks for anonymous classes without polluting enclosing class', function () {
403+
$source = <<<'PHP'
404+
<?php
405+
406+
namespace App\Test;
407+
408+
trait SomeTraitFixture {}
409+
410+
class OuterNamedClass
411+
{
412+
public function make()
413+
{
414+
return new class () {
415+
/**
416+
* @use SomeTraitFixture<int, string>
417+
*/
418+
use SomeTraitFixture;
419+
};
420+
}
421+
}
422+
PHP;
423+
$virtualFile = 'VirtualAnonTraitTest.php';
424+
SpecialTypeResolver::parseFileMetadata($virtualFile, $source);
425+
426+
expect(SpecialTypeResolver::getClassTraitUseDocs('App\Test\OuterNamedClass'))->toBeEmpty();
427+
428+
$anon = (new class () {
429+
public function make(): object
430+
{
431+
return new class () {
432+
/** @use \TypePHP\Tests\Fixtures\Generics\GenericItemLoggerTrait<int> */
433+
use TypePHP\Tests\Fixtures\Generics\GenericItemLoggerTrait;
434+
};
435+
}
436+
})->make();
437+
438+
$docs = SpecialTypeResolver::getClassTraitUseDocs($anon::class);
439+
expect($docs)->toHaveCount(1)
440+
->and($docs[0])->toContain('GenericItemLoggerTrait<int>')
441+
;
442+
});
443+
444+
test('ignores ::class constant fetches and does not push ghost anonymous classes to classStack', function () {
445+
$source = <<<'PHP'
446+
<?php
447+
448+
namespace App\Test;
449+
450+
trait MySampleTrait {}
451+
452+
class SampleClassWithConstFetches
453+
{
454+
public function check(): string
455+
{
456+
$x = \DateTimeInterface::class;
457+
$y = \stdClass::class;
458+
459+
if (true) {
460+
return $x;
461+
}
462+
463+
return $y;
464+
}
465+
466+
/**
467+
* @use MySampleTrait<string>
468+
*/
469+
use MySampleTrait;
470+
}
471+
PHP;
472+
$virtualFile = 'VirtualConstFetchTest.php';
473+
SpecialTypeResolver::parseFileMetadata($virtualFile, $source);
474+
475+
$docs = SpecialTypeResolver::getClassTraitUseDocs('App\Test\SampleClassWithConstFetches');
476+
expect($docs)->toHaveCount(1)
477+
->and($docs[0])->toContain('@use MySampleTrait<string>')
478+
;
479+
});
480+
481+
test('resolves anonymous class trait use docs with proximity matching when line numbers shift', function () {
482+
$anon = new class () {
483+
/**
484+
* @use \TypePHP\Tests\Fixtures\Generics\GenericItemLoggerTrait<\TypePHP\Tests\Fixtures\Domain\Dog>
485+
*/
486+
use TypePHP\Tests\Fixtures\Generics\GenericItemLoggerTrait;
487+
};
488+
489+
$docs = SpecialTypeResolver::getClassTraitUseDocs($anon::class);
490+
expect($docs)->toHaveCount(1)
491+
->and($docs[0])->toContain('GenericItemLoggerTrait<')
492+
->and($docs[0])->toContain('Dog>')
493+
;
494+
});
495+
496+
test('returns empty array cleanly for anonymous class without any trait docblocks', function () {
497+
$anonWithoutDoc = new class () {
498+
use TypePHP\Tests\Fixtures\Generics\GenericItemLoggerTrait;
499+
};
500+
501+
$docs = SpecialTypeResolver::getClassTraitUseDocs($anonWithoutDoc::class);
502+
expect($docs)->toBeEmpty();
503+
});
504+
});
400505
});

‎tests/Internal/Util/PathMatcherTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
test('permits var when var is root directory using default config', function () {
254254
try {
255255
Config::set([
256-
'include' => ['src/**', 'app/**', 'tests/**',],
256+
'include' => ['src/**', 'app/**', 'tests/**'],
257257
]);
258258

259259
expect(PathMatcher::mayPathBeIncluded('/var/www/app/src/index.php'))->toBeTrue();

‎tests/Pest.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
->afterEach(function () {
1212
Config::reset();
1313
})
14-
->in('Contract', 'Internal', 'Feature');
14+
->in('Contract', 'Internal', 'Feature')
15+
;

0 commit comments

Comments
 (0)