Skip to content

Commit 54f4fe1

Browse files
committed
[AutoImport] Handle FQCN in/not in use on auto import + remove unused import
1 parent 2475409 commit 54f4fe1

5 files changed

Lines changed: 74 additions & 12 deletions

File tree

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+
?>

tests/Issues/NamespacedUseAutoImport/Fixture/merged_import_property_annotation_2.php.inc renamed to tests/Issues/NamespacedUseAutoImport/Fixture/not_in_use_fqcn_docblock.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use Carbon\CarbonInterface;
1010
/**
1111
* @property \Carbon\CarbonInterface|null $deleted_at
1212
*/
13-
class MergedImportErrorOtherVariety
13+
class NotInUseFqcnDocblock
1414
{
1515
public function demoFunction(): array
1616
{
@@ -32,7 +32,7 @@ use Carbon\CarbonInterface;
3232
/**
3333
* @property CarbonInterface|null $deleted_at
3434
*/
35-
class MergedImportErrorOtherVariety
35+
class NotInUseFqcnDocblock
3636
{
3737
public function demoFunction(): array
3838
{

tests/Issues/NamespacedUseAutoImport/Fixture/merged_import_property_annotation.php.inc renamed to tests/Issues/NamespacedUseAutoImport/Fixture/not_in_use_fqcn_docblock_2.inc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use Carbon\CarbonInterface;
1010
/**
1111
* @property \Carbon\CarbonInterface|null $deleted_at
1212
*/
13-
class MergedImportError
13+
class NotInUseFqcnDocblock2
1414
{
1515
public function demoFunction(): array
1616
{
@@ -27,12 +27,11 @@ declare(strict_types=1);
2727
namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;
2828

2929
use Carbon\Carbon;
30-
use Carbon\CarbonInterface;
3130

3231
/**
3332
* @property CarbonInterface|null $deleted_at
3433
*/
35-
class MergedImportError
34+
class NotInUseFqcnDocblock2
3635
{
3736
public function demoFunction(): array
3837
{

0 commit comments

Comments
 (0)