-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Decouple PropertyTypeDecorator (#1055)
- Loading branch information
1 parent
8e2908c
commit b305c4e
Showing
5 changed files
with
80 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
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
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\NodeDecorator; | ||
|
||
use PhpParser\Node\Stmt\Property; | ||
use PHPStan\Type\Generic\GenericObjectType; | ||
use PHPStan\Type\Type; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; | ||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; | ||
use Rector\Core\Php\PhpVersionProvider; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind; | ||
use Rector\StaticTypeMapper\StaticTypeMapper; | ||
|
||
final class PropertyTypeDecorator | ||
{ | ||
public function __construct( | ||
private PhpDocInfoFactory $phpDocInfoFactory, | ||
private PhpVersionProvider $phpVersionProvider, | ||
private StaticTypeMapper $staticTypeMapper, | ||
private PhpDocTypeChanger $phpDocTypeChanger, | ||
) { | ||
} | ||
|
||
public function decorate(Property $property, ?Type $type): void | ||
{ | ||
if ($type === null) { | ||
return; | ||
} | ||
|
||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); | ||
|
||
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) { | ||
$phpParserType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PROPERTY()); | ||
|
||
if ($phpParserType !== null) { | ||
$property->type = $phpParserType; | ||
|
||
if ($type instanceof GenericObjectType) { | ||
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $type); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $type); | ||
} | ||
} |
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