Skip to content

Commit

Permalink
Do not prepend a \ to the type, if it is already Fully Qualified (#1863)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mkrauser committed Feb 24, 2022
1 parent 3486e78 commit bb4a906
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): Type
return new IdentifierTypeNode($type->getClassName());
}

if ($type instanceof FullyQualifiedObjectType && str_starts_with($type->getClassName(), '\\')) {
return new IdentifierTypeNode($type->getClassName());
}

return new IdentifierTypeNode('\\' . $type->getClassName());
}

Expand All @@ -83,7 +87,13 @@ public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
}

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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
}
}
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions tests/Issues/Issue7023/Fixture/7.3/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions tests/Issues/Issue7023/Fixture/7.4/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions tests/Issues/Issue7023/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions tests/Issues/Issue7023/config/configured_rule_73.php
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit bb4a906

Please sign in to comment.