Skip to content

Commit b49ed29

Browse files
[AutoImport] Handle FQCN in/not in use on auto import + remove unused import as prefix = suffix (#7957)
* Add test case for issue with Carbon import * fix fixture php line to enable clearer error showing * Second example with Illuminate\Support\Carbon * rename test files after discussion * move to tests * update fixture namespace * [AutoImport] Handle FQCN in/not in use on auto import + remove unused import --------- Co-authored-by: Kathryn Reeve <67553+BinaryKitten@users.noreply.github.com>
1 parent c2251bd commit b49ed29

File tree

5 files changed

+155
-7
lines changed

5 files changed

+155
-7
lines changed

rules/TypeDeclaration/PHPStan/ObjectTypeSpecifier.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ public function narrowToFullyQualifiedOrAliasedObjectType(
5151

5252
$uses = $this->useImportsResolver->resolve();
5353

54-
$aliasedObjectType = $this->matchAliasedObjectType($objectType, $uses);
55-
if ($aliasedObjectType instanceof AliasedObjectType) {
56-
return $aliasedObjectType;
57-
}
54+
if (! $withPreslash) {
55+
$aliasedObjectType = $this->matchAliasedObjectType($objectType, $uses);
56+
if ($aliasedObjectType instanceof AliasedObjectType) {
57+
return $aliasedObjectType;
58+
}
5859

59-
$shortenedObjectType = $this->matchShortenedObjectType($objectType, $uses);
60-
if ($shortenedObjectType !== null) {
61-
return $shortenedObjectType;
60+
$shortenedObjectType = $this->matchShortenedObjectType($objectType, $uses);
61+
if ($shortenedObjectType !== null) {
62+
return $shortenedObjectType;
63+
}
6264
}
6365

6466
if ($this->reflectionProvider->hasClass($className)) {

stubs/Carbon/CarbonInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Carbon;
4+
5+
if (interface_exists('Carbon\CarbonInterface')) {
6+
return;
7+
}
8+
9+
interface CarbonInterface
10+
{
11+
12+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;
6+
7+
use Carbon\Carbon;
8+
use Carbon\CarbonInterface;
9+
10+
/**
11+
* @property \Carbon\CarbonInterface|null $deleted_at
12+
*/
13+
class InUseFqcnDocblock
14+
{
15+
/**
16+
* @return CarbonInterface[]
17+
*/
18+
public function demoFunction(): array
19+
{
20+
return [Carbon::parse('2026-01-01 11:16:01')];
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
declare(strict_types=1);
29+
30+
namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;
31+
32+
use Carbon\Carbon;
33+
use Carbon\CarbonInterface;
34+
35+
/**
36+
* @property CarbonInterface|null $deleted_at
37+
*/
38+
class InUseFqcnDocblock
39+
{
40+
/**
41+
* @return CarbonInterface[]
42+
*/
43+
public function demoFunction(): array
44+
{
45+
return [Carbon::parse('2026-01-01 11:16:01')];
46+
}
47+
}
48+
49+
?>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;
6+
7+
use Illuminate\Support\Carbon;
8+
use Carbon\CarbonInterface;
9+
10+
/**
11+
* @property \Carbon\CarbonInterface|null $deleted_at
12+
*/
13+
class NotInUseFqcnDocblock
14+
{
15+
public function demoFunction(): array
16+
{
17+
return [Carbon::parse('2026-01-01 11:16:01')];
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
declare(strict_types=1);
26+
27+
namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;
28+
29+
use Illuminate\Support\Carbon;
30+
use Carbon\CarbonInterface;
31+
32+
/**
33+
* @property CarbonInterface|null $deleted_at
34+
*/
35+
class NotInUseFqcnDocblock
36+
{
37+
public function demoFunction(): array
38+
{
39+
return [Carbon::parse('2026-01-01 11:16:01')];
40+
}
41+
}
42+
43+
?>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;
6+
7+
use Carbon\Carbon;
8+
use Carbon\CarbonInterface;
9+
10+
/**
11+
* @property \Carbon\CarbonInterface|null $deleted_at
12+
*/
13+
class NotInUseFqcnDocblock2
14+
{
15+
public function demoFunction(): array
16+
{
17+
return [Carbon::parse('2026-01-01 11:16:01')];
18+
}
19+
}
20+
21+
?>
22+
-----
23+
<?php
24+
25+
declare(strict_types=1);
26+
27+
namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;
28+
29+
use Carbon\Carbon;
30+
31+
/**
32+
* @property CarbonInterface|null $deleted_at
33+
*/
34+
class NotInUseFqcnDocblock2
35+
{
36+
public function demoFunction(): array
37+
{
38+
return [Carbon::parse('2026-01-01 11:16:01')];
39+
}
40+
}
41+
42+
?>

0 commit comments

Comments
 (0)