mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 03:40:50 +00:00
Do not prepend a \ to the type, if it is already Fully Qualified (#1863)
* Do not prepend a \ to the type, if it is already Fully Qualified * add test for issue #7023 * fix issue #7023 for php versions >= 7.4 * add test for issue #7023 with php version <= 7.3 * removed unused use
This commit is contained in:
parent
3486e78f7e
commit
bb4a9068c2
|
@ -62,6 +62,10 @@ final class ObjectTypeMapper implements TypeMapperInterface
|
|||
return new IdentifierTypeNode($type->getClassName());
|
||||
}
|
||||
|
||||
if ($type instanceof FullyQualifiedObjectType && str_starts_with($type->getClassName(), '\\')) {
|
||||
return new IdentifierTypeNode($type->getClassName());
|
||||
}
|
||||
|
||||
return new IdentifierTypeNode('\\' . $type->getClassName());
|
||||
}
|
||||
|
||||
|
@ -83,7 +87,13 @@ final class ObjectTypeMapper implements TypeMapperInterface
|
|||
}
|
||||
|
||||
if ($type instanceof FullyQualifiedObjectType) {
|
||||
return new FullyQualified($type->getClassName());
|
||||
$className = $type->getClassName();
|
||||
|
||||
if(str_starts_with($className, '\\')) {
|
||||
// skip leading \
|
||||
return new FullyQualified(substr($className, 1));
|
||||
}
|
||||
return new FullyQualified($className);
|
||||
}
|
||||
|
||||
if (! $type instanceof GenericObjectType) {
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\Issue7023;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
* @see https://github.com/rectorphp/rector/issues/7023
|
||||
*/
|
||||
final class DoNotPrependAnotherBackslashIfAlreadyPresent73Test extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture/7.3');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule_73.php';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Tests\Issues\Issue7023;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
/**
|
||||
* @see https://github.com/rectorphp/rector/issues/7023
|
||||
*/
|
||||
final class DoNotPrependAnotherBackslashIfAlreadyPresentTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture/7.4');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
43
tests/Issues/Issue7023/Fixture/7.3/fixture.php.inc
Normal file
43
tests/Issues/Issue7023/Fixture/7.3/fixture.php.inc
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\Issue7023\Fixture;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Acme\Entity\User;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
*/
|
||||
class Fixture
|
||||
{
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="\Acme\Entity\User", inversedBy="images")
|
||||
* @ORM\JoinColumn(name="data_id", nullable=true)
|
||||
* @var User
|
||||
*/
|
||||
private $user;
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\Issue7023\Fixture;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Acme\Entity\User;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
*/
|
||||
class Fixture
|
||||
{
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="\Acme\Entity\User", inversedBy="images")
|
||||
* @ORM\JoinColumn(name="data_id", nullable=true)
|
||||
* @var \Acme\Entity\User|null
|
||||
*/
|
||||
private $user;
|
||||
}
|
||||
?>
|
43
tests/Issues/Issue7023/Fixture/7.4/fixture.php.inc
Normal file
43
tests/Issues/Issue7023/Fixture/7.4/fixture.php.inc
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\Issue7023\Fixture;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Acme\Entity\User;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
*/
|
||||
class Fixture
|
||||
{
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="\Acme\Entity\User", inversedBy="images")
|
||||
* @ORM\JoinColumn(name="data_id", nullable=true)
|
||||
* @var User
|
||||
*/
|
||||
private $user;
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Core\Tests\Issues\Issue7023\Fixture;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Acme\Entity\User;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
*/
|
||||
class Fixture
|
||||
{
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="\Acme\Entity\User", inversedBy="images")
|
||||
* @ORM\JoinColumn(name="data_id", nullable=true)
|
||||
* @var User
|
||||
*/
|
||||
private ?\Acme\Entity\User $user = null;
|
||||
}
|
||||
?>
|
11
tests/Issues/Issue7023/config/configured_rule.php
Normal file
11
tests/Issues/Issue7023/config/configured_rule.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Doctrine\Rector\Property\TypedPropertyFromToOneRelationTypeRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(TypedPropertyFromToOneRelationTypeRector::class);
|
||||
};
|
16
tests/Issues/Issue7023/config/configured_rule_73.php
Normal file
16
tests/Issues/Issue7023/config/configured_rule_73.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\Doctrine\Rector\Property\TypedPropertyFromToOneRelationTypeRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::TYPED_PROPERTIES - 1);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(TypedPropertyFromToOneRelationTypeRector::class);
|
||||
};
|
Loading…
Reference in New Issue
Block a user