mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-22 02:36:52 +01:00
Merge pull request #3400 from rectorphp/context-interface
remove interface suffix/prexit for PropertyNaming
This commit is contained in:
commit
80f429b5b7
@ -12,6 +12,7 @@ use PhpParser\Node\Stmt\ClassLike;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\ChangesReporting\Collector\RectorChangeCollector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\PHPStan\Type\AliasedObjectType;
|
||||
use Rector\PHPStan\Type\FullyQualifiedObjectType;
|
||||
use Rector\PostRector\Collector\NodesToAddCollector;
|
||||
@ -120,6 +121,13 @@ trait NodeCommandersTrait
|
||||
|
||||
protected function removeNode(Node $node): void
|
||||
{
|
||||
// this make sure to keep just added nodes, e.g. added class constant, that doesn't have analysis of full code in this run
|
||||
// if this is missing, there are false positive e.g. for unused private constant
|
||||
$isJustAddedNode = ! (bool) $node->getAttribute(AttributeKey::ORIGINAL_NODE);
|
||||
if ($isJustAddedNode) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->nodesToRemoveCollector->addNodeToRemove($node);
|
||||
$this->rectorChangeCollector->notifyNodeFileInfo($node);
|
||||
}
|
||||
|
@ -7,6 +7,10 @@ parameters:
|
||||
|
||||
auto_import_names: true
|
||||
|
||||
# sets:
|
||||
# - solid
|
||||
# - 'dead-code'
|
||||
|
||||
paths:
|
||||
- src
|
||||
- tests
|
||||
@ -25,3 +29,7 @@ parameters:
|
||||
|
||||
# so Rector code is still PHP 7.2 compatible
|
||||
php_version_features: '7.2'
|
||||
|
||||
services:
|
||||
Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateConstantRector: null
|
||||
Rector\SOLID\Rector\Class_\RepeatedLiteralToClassConstantRector: null
|
||||
|
@ -134,6 +134,10 @@ PHP
|
||||
/** @var string $methodCallMethodName */
|
||||
$methodCallMethodName = $methodCall->getAttribute(AttributeKey::METHOD_NAME);
|
||||
|
||||
if ($methodCall->name instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// is method called not in itself
|
||||
if (! $this->isName($methodCall->name, $methodCallMethodName)) {
|
||||
return true;
|
||||
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source;
|
||||
|
||||
interface ISomeInterfaceToInject
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture;
|
||||
|
||||
use Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source\ISomeInterfaceToInject;
|
||||
|
||||
class InterfaceName
|
||||
{
|
||||
/**
|
||||
* @var \Nette\DI\Container
|
||||
*/
|
||||
private $context;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$someTypeToInject = $this->context->getByType(ISomeInterfaceToInject::class);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture;
|
||||
|
||||
use Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source\ISomeInterfaceToInject;
|
||||
|
||||
class InterfaceName
|
||||
{
|
||||
/**
|
||||
* @var \Nette\DI\Container
|
||||
*/
|
||||
private $context;
|
||||
/**
|
||||
* @var \Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source\ISomeInterfaceToInject
|
||||
*/
|
||||
private $someInterfaceToInject;
|
||||
public function __construct(\Rector\Nette\Tests\Rector\MethodCall\ContextGetByTypeToConstructorInjectionRector\Fixture\Source\ISomeInterfaceToInject $someInterfaceToInject)
|
||||
{
|
||||
$this->someInterfaceToInject = $someInterfaceToInject;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$someTypeToInject = $this->someInterfaceToInject;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -9,16 +9,22 @@ use PHPStan\Type\ObjectType;
|
||||
|
||||
final class PropertyNaming
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const INTERFACE = 'Interface';
|
||||
|
||||
/**
|
||||
* @param ObjectType|string $objectType
|
||||
*/
|
||||
public function fqnToVariableName($objectType): string
|
||||
{
|
||||
if ($objectType instanceof ObjectType) {
|
||||
$objectType = $objectType->getClassName();
|
||||
}
|
||||
$className = $this->resolveClassName($objectType);
|
||||
|
||||
return lcfirst($this->fqnToShortName($objectType));
|
||||
$shortName = $this->fqnToShortName($className);
|
||||
$shortName = $this->removeInterfaceSuffixPrefix($className, $shortName);
|
||||
|
||||
return lcfirst($shortName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,10 +45,41 @@ final class PropertyNaming
|
||||
|
||||
/** @var string $lastNamePart */
|
||||
$lastNamePart = Strings::after($fqn, '\\', - 1);
|
||||
if (Strings::endsWith($lastNamePart, 'Interface')) {
|
||||
return Strings::substring($lastNamePart, 0, - strlen('Interface'));
|
||||
if (Strings::endsWith($lastNamePart, self::INTERFACE)) {
|
||||
return Strings::substring($lastNamePart, 0, - strlen(self::INTERFACE));
|
||||
}
|
||||
|
||||
return $lastNamePart;
|
||||
}
|
||||
|
||||
private function removeInterfaceSuffixPrefix(string $className, string $shortName): string
|
||||
{
|
||||
// remove interface prefix/suffix
|
||||
if (! interface_exists($className)) {
|
||||
return $shortName;
|
||||
}
|
||||
|
||||
// starts with "I\W+"?
|
||||
if (Strings::match($shortName, '#^I[A-Z]#')) {
|
||||
return Strings::substring($shortName, 1);
|
||||
}
|
||||
|
||||
if (Strings::match($shortName, '#Interface$#')) {
|
||||
return Strings::substring($shortName, -strlen(self::INTERFACE));
|
||||
}
|
||||
|
||||
return $shortName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectType|string $objectType
|
||||
*/
|
||||
private function resolveClassName($objectType): string
|
||||
{
|
||||
if ($objectType instanceof ObjectType) {
|
||||
return $objectType->getClassName();
|
||||
}
|
||||
|
||||
return $objectType;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user