Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions src/Type/Pest/PestTestCaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,16 @@ public function __construct(

public function resolve(string $filePath): Type
{
$bindings = $this->pestConfigReader->resolveFileBindings($filePath);

if ($bindings === []) {
$bindings = $this->pestConfigReader->resolveBindings($filePath);
}

$classNames = [];
$traitNames = [];

foreach ($bindings as $binding) {
if (! $this->reflectionProvider->hasClass($binding)) {
continue;
}

$reflection = $this->reflectionProvider->getClass($binding);

if ($reflection->isTrait()) {
$traitNames[] = $binding;
[$classNames, $traitNames] = $this->partition(
$this->pestConfigReader->resolveFileBindings($filePath),
);

continue;
}
if ($classNames === []) {
[$classNames, $directoryTraitNames] = $this->partition(
$this->pestConfigReader->resolveBindings($filePath),
);

$classNames[] = $binding;
$traitNames = array_values(array_unique([...$directoryTraitNames, ...$traitNames]));
}

if ($classNames === []) {
Expand All @@ -61,6 +48,32 @@ public function resolve(string $filePath): Type
);
}

/**
* @param list<string> $bindings
* @return array{list<class-string>, list<class-string>}
*/
private function partition(array $bindings): array
{
$classNames = [];
$traitNames = [];

foreach ($bindings as $binding) {
if (! $this->reflectionProvider->hasClass($binding)) {
continue;
}

if ($this->reflectionProvider->getClass($binding)->isTrait()) {
$traitNames[] = $binding;

continue;
}

$classNames[] = $binding;
}

return [$classNames, $traitNames];
}

/**
* @param list<string> $classNames
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace LocalUsesTraitOnlyKeepsDirectoryTestCase;

use Tests\Type\Fixtures\CustomTestCase;
use Tests\Type\Fixtures\HelperTrait;

use function PHPStan\Testing\assertType;

uses(HelperTrait::class);

function testTraitOnlyLocalUsesKeepsTheDirectoryTestCase(): void
{
it('keeps the directory-bound test case when the file binds only a trait', function (): void {
assertType(CustomTestCase::class, $this);
assertType('string', $this->publicHelper());
assertType('string', $this->helperMethod());
});
}
6 changes: 6 additions & 0 deletions tests/Type/CustomTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@
})->with(function (): Iterator {
yield from CustomTestCaseTestCase::gatherAssertTypes(__DIR__.'/../Fixtures/CustomTestCaseInference/Feature/local-uses-overrides-directory.php');
});

test('a trait-only file-level uses() keeps the directory binding', function (string $assertType, string $file, mixed ...$args): void {
$this->assertFileAsserts($assertType, $file, ...$args);
})->with(function (): Iterator {
yield from CustomTestCaseTestCase::gatherAssertTypes(__DIR__.'/../Fixtures/CustomTestCaseInference/Feature/local-uses-trait-only-keeps-directory-testcase.php');
});