-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Decouple AssignVariableNameResolver
- Loading branch information
1 parent
ee3ca5a
commit a3bebbd
Showing
6 changed files
with
120 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
rules/Naming/AssignVariableNameResolver/NewAssignVariableNameResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Naming\AssignVariableNameResolver; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\New_; | ||
use Rector\Core\Exception\NotImplementedYetException; | ||
use Rector\Naming\Contract\AssignVariableNameResolverInterface; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
/** | ||
* @implements AssignVariableNameResolverInterface<New_> | ||
*/ | ||
final class NewAssignVariableNameResolver implements AssignVariableNameResolverInterface | ||
{ | ||
public function __construct( | ||
private readonly NodeNameResolver $nodeNameResolver | ||
) { | ||
} | ||
|
||
public function match(Node $node): bool | ||
{ | ||
return $node instanceof New_; | ||
} | ||
|
||
/** | ||
* @param New_ $node | ||
*/ | ||
public function resolve(Node $node): string | ||
{ | ||
$className = $this->nodeNameResolver->getName($node->class); | ||
if ($className === null) { | ||
throw new NotImplementedYetException(); | ||
} | ||
|
||
return $this->nodeNameResolver->getShortName($className); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
rules/Naming/AssignVariableNameResolver/PropertyFetchAssignVariableNameResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Naming\AssignVariableNameResolver; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use Rector\Core\Exception\NotImplementedYetException; | ||
use Rector\Naming\Contract\AssignVariableNameResolverInterface; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
/** | ||
* @implements AssignVariableNameResolverInterface<PropertyFetch> | ||
*/ | ||
final class PropertyFetchAssignVariableNameResolver implements AssignVariableNameResolverInterface | ||
{ | ||
public function __construct( | ||
private readonly NodeNameResolver $nodeNameResolver | ||
) { | ||
} | ||
|
||
public function match(Node $node): bool | ||
{ | ||
return $node instanceof PropertyFetch; | ||
} | ||
|
||
/** | ||
* @param PropertyFetch $node | ||
*/ | ||
public function resolve(Node $node): string | ||
{ | ||
$varName = $this->nodeNameResolver->getName($node->var); | ||
if (! is_string($varName)) { | ||
throw new NotImplementedYetException(); | ||
} | ||
|
||
$propertyName = $this->nodeNameResolver->getName($node->name); | ||
if (! is_string($propertyName)) { | ||
throw new NotImplementedYetException(); | ||
} | ||
|
||
if ($varName === 'this') { | ||
return $propertyName; | ||
} | ||
|
||
return $varName . ucfirst($propertyName); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
rules/Naming/Contract/AssignVariableNameResolverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Naming\Contract; | ||
|
||
use PhpParser\Node; | ||
|
||
/** | ||
* @template TNode as Node | ||
*/ | ||
interface AssignVariableNameResolverInterface | ||
{ | ||
public function match(Node $node): bool; | ||
|
||
/** | ||
* @param TNode $node | ||
*/ | ||
public function resolve(Node $node): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters