2021-04-06 17:33:09 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-04-06 17:33:09 +00:00
|
|
|
namespace Rector\NodeTypeResolver\PhpDocNodeVisitor;
|
|
|
|
|
2021-12-04 07:57:42 +00:00
|
|
|
use RectorPrefix20211204\Nette\Utils\Strings;
|
2021-04-06 17:33:09 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Node;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\Renaming\ValueObject\PseudoNamespaceToNamespace;
|
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2021-12-04 07:57:42 +00:00
|
|
|
use RectorPrefix20211204\Symplify\SimplePhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
|
|
|
|
final class UnderscoreRenamePhpDocNodeVisitor extends \RectorPrefix20211204\Symplify\SimplePhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor
|
2021-04-06 17:33:09 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \Rector\Renaming\ValueObject\PseudoNamespaceToNamespace|null
|
2021-04-06 17:33:09 +00:00
|
|
|
*/
|
|
|
|
private $pseudoNamespaceToNamespace;
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \PhpParser\Node|null
|
2021-04-06 17:33:09 +00:00
|
|
|
*/
|
|
|
|
private $currentPhpParserNode;
|
2021-05-10 23:39:21 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper)
|
2021-04-06 17:33:09 +00:00
|
|
|
{
|
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
|
|
|
}
|
2021-07-05 23:06:30 +00:00
|
|
|
public function beforeTraverse(\PHPStan\PhpDocParser\Ast\Node $node) : void
|
2021-04-06 17:33:09 +00:00
|
|
|
{
|
|
|
|
if ($this->pseudoNamespaceToNamespace === null) {
|
2021-05-10 22:23:08 +00:00
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException('Set PseudoNamespaceToNamespace first');
|
2021-04-06 17:33:09 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->currentPhpParserNode instanceof \PhpParser\Node) {
|
2021-05-10 22:23:08 +00:00
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException('Set "$currentPhpParserNode" first');
|
2021-04-06 17:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-05 23:06:30 +00:00
|
|
|
public function enterNode(\PHPStan\PhpDocParser\Ast\Node $node) : ?\PHPStan\PhpDocParser\Ast\Node
|
2021-04-06 17:33:09 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$node instanceof \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode) {
|
2021-04-06 17:33:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($this->shouldSkip($node, $this->currentPhpParserNode, $this->pseudoNamespaceToNamespace)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/** @var IdentifierTypeNode $node */
|
2021-05-09 20:15:43 +00:00
|
|
|
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($node, $this->currentPhpParserNode);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$staticType instanceof \PHPStan\Type\ObjectType) {
|
2021-04-06 17:33:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// change underscore to \\
|
2021-12-04 07:57:42 +00:00
|
|
|
$slashedName = '\\' . \RectorPrefix20211204\Nette\Utils\Strings::replace($staticType->getClassName(), '#_#', '\\');
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode($slashedName);
|
2021-04-06 17:33:09 +00:00
|
|
|
}
|
2021-07-05 23:06:30 +00:00
|
|
|
public function setPseudoNamespaceToNamespace(\Rector\Renaming\ValueObject\PseudoNamespaceToNamespace $pseudoNamespaceToNamespace) : void
|
2021-04-06 17:33:09 +00:00
|
|
|
{
|
|
|
|
$this->pseudoNamespaceToNamespace = $pseudoNamespaceToNamespace;
|
|
|
|
}
|
2021-07-05 23:06:30 +00:00
|
|
|
public function setCurrentPhpParserNode(\PhpParser\Node $node) : void
|
2021-04-06 17:33:09 +00:00
|
|
|
{
|
|
|
|
$this->currentPhpParserNode = $node;
|
|
|
|
}
|
2021-07-05 23:06:30 +00:00
|
|
|
private function shouldSkip(\PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode $identifierTypeNode, \PhpParser\Node $phpParserNode, \Rector\Renaming\ValueObject\PseudoNamespaceToNamespace $pseudoNamespaceToNamespace) : bool
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
|
|
|
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($identifierTypeNode, $phpParserNode);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$staticType instanceof \PHPStan\Type\ObjectType) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-04-06 17:33:09 +00:00
|
|
|
}
|
2021-05-30 10:12:56 +00:00
|
|
|
if (\strncmp($staticType->getClassName(), $pseudoNamespaceToNamespace->getNamespacePrefix(), \strlen($pseudoNamespaceToNamespace->getNamespacePrefix())) !== 0) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-04-06 17:33:09 +00:00
|
|
|
}
|
|
|
|
// excluded?
|
2021-05-09 20:15:43 +00:00
|
|
|
return \in_array($staticType->getClassName(), $pseudoNamespaceToNamespace->getExcludedClasses(), \true);
|
2021-04-06 17:33:09 +00:00
|
|
|
}
|
|
|
|
}
|