Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0cb4f74

Browse files
committedFeb 25, 2025
refactor and enhancement of importers.
1 parent 75d03fd commit 0cb4f74

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed
 

‎Src/Helper/ImportBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public function import(): bool {
4242

4343
public function getAlias(): ?string {
4444
return match ( true ) {
45-
$this->globalContains() => $this->getGlobalAlias(),
46-
$this->namespaceContains() => $this->getNamespaceAlias(),
45+
$this->globalContains() => $this->findAliasAsIndexIn( $this->globalImports() ),
46+
$this->namespaceContains() => $this->findAliasAsIndexIn( $this->namespaceImports() ),
4747
default => null,
4848
};
4949
}

‎Src/Traits/GlobalImporter.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
trait GlobalImporter {
1010
use AliasResolver;
1111

12+
/** @var array<string,string> */
13+
private array $globalImports;
14+
1215
/** @return PhpGlobal<TType,array<string,string>> */
1316
abstract protected function inGlobal(): PhpGlobal;
1417

@@ -20,17 +23,9 @@ final protected function importGlobal(): bool {
2023
return $isImportable;
2124
}
2225

23-
protected function getGlobalAlias(): ?string {
24-
return $this->findAliasAsIndexIn( $this->globalTypeImports() );
25-
}
26-
27-
/** @return array<string,string> */
28-
private function globalTypeImports(): array {
29-
return $this->inGlobal()[ $this->forType() ] ?? array();
30-
}
31-
3226
private function globalContains(): bool {
33-
return ( $imports = $this->globalTypeImports() ) && in_array( $this->forImport(), $imports, strict: true );
27+
return ( $this->globalImports = $this->inGlobal()[ $this->forType() ] ?? array() )
28+
&& in_array( $this->forImport(), $this->globalImports, strict: true );
3429
}
3530

3631
private function globalImportable(): bool {
@@ -40,6 +35,11 @@ private function globalImportable(): bool {
4035
/** @return array<string,string> */
4136
private function withGlobalImport(): array {
4237
// phpcs:ignore WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
43-
return array( ...$this->globalTypeImports(), $this->forImport() => $this->forImport() );
38+
return array( ...$this->globalImports(), $this->forImport() => $this->forImport() );
39+
}
40+
41+
/** @return array<string,string> */
42+
private function globalImports(): array {
43+
return $this->globalImports ?? array();
4444
}
4545
}

‎Src/Traits/NamespaceImporter.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
trait NamespaceImporter {
1010
use AliasResolver;
1111

12-
private string $aliasOfImport;
1312
/** @var array<string,string> */
14-
private array $currentTypeImports = array();
13+
private array $namespaceImports;
1514

1615
abstract protected function inNamespace(): PhpNamespace;
1716

@@ -22,30 +21,21 @@ protected function importNamespace(): bool {
2221
return $isImportable;
2322
}
2423

25-
/**
26-
* Gets the alias of a namespace item, if it has been imported to the current namespace.
27-
* Always check with `NamespaceImporter::namespaceContains()` before using this method.
28-
*/
29-
protected function getNamespaceAlias(): ?string {
30-
return $this->findAliasAsIndexIn( imports: $this->namespaceTypeImports() );
31-
}
32-
3324
private function namespaceContains(): bool {
34-
return ( $this->currentTypeImports = $this->inNamespace()->getUses( $this->forType() ) )
35-
&& in_array( $this->forImport(), $this->currentTypeImports, strict: true );
25+
return ( $this->namespaceImports = $this->inNamespace()->getUses( $this->forType() ) )
26+
&& in_array( $this->forImport(), $this->namespaceImports, strict: true );
3627
}
3728

3829
private function withNamespaceAlias(): string {
39-
$alias = Helpers::extractShortName( $this->forImport() );
40-
$namespaceLastPart = isset( $this->namespaceTypeImports()[ $alias ] )
41-
? Helpers::extractShortName( Helpers::extractNamespace( $this->forImport() ) )
42-
: '';
30+
$alias = Helpers::extractShortName( $this->forImport() );
4331

44-
return $namespaceLastPart . $alias;
32+
return ( isset( $this->namespaceImports()[ $alias ] )
33+
? Helpers::extractShortName( Helpers::extractNamespace( $this->forImport() ) )
34+
: '' ) . $alias;
4535
}
4636

4737
/** @return array<string,string> */
48-
private function namespaceTypeImports(): array {
49-
return $this->currentTypeImports ?? array();
38+
private function namespaceImports(): array {
39+
return $this->namespaceImports ?? array();
5040
}
5141
}

‎phpstan.dist.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ parameters:
2525
count: 1
2626
- # PhpNamespace::getUses() method's return value is legit.
2727
path: Src/Traits/NamespaceImporter.php
28-
message: '|\$currentTypeImports \(array\<string, string\>\) does not accept array\<string\>|'
28+
message: '|\$namespaceImports \(array\<string, string\>\) does not accept array\<string\>|'
2929
identifier: assign.propertyType
3030
count: 1

0 commit comments

Comments
 (0)
Please sign in to comment.