Various cleanup, stmts naming, split big test fixture (#1070)

This commit is contained in:
Tomas Votruba 2021-10-26 14:27:34 +02:00 committed by GitHub
parent fbeecb22d4
commit 0ff81a9d2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 111 additions and 74 deletions

View File

@ -92,16 +92,16 @@ final class NodeScopeAndMetadataDecorator
}
/**
* @param Stmt[] $nodes
* @param Stmt[] $stmts
* @return Stmt[]
*/
public function decorateNodesFromString(array $nodes): array
public function decorateStmtsFromString(array $stmts): array
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($this->nodeConnectingVisitor);
$nodeTraverser->addVisitor($this->functionMethodAndClassNodeVisitor);
$nodeTraverser->addVisitor($this->statementNodeVisitor);
return $nodeTraverser->traverse($nodes);
return $nodeTraverser->traverse($stmts);
}
}

View File

@ -40,7 +40,7 @@ parameters:
reportUnmatchedIgnoredErrors: false
checkGenericClassInNonGenericObjectType: false
excludes_analyse:
excludePaths:
# temporary stinrgable migration from template type provider
- src/Console/Command/InitCommand.php

View File

@ -0,0 +1,35 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;
final class ReturnStaticNullable
{
public function getNullableStatic()
{
if (rand(0,1)) {
return new static();
}
return null;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;
final class ReturnStaticNullable
{
public function getNullableStatic(): ?static
{
if (rand(0,1)) {
return new static();
}
return null;
}
}
?>

View File

@ -2,29 +2,12 @@
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;
class Static_
final class Static_
{
/**
* @return static
*/
public function getSelf()
{
return $this;
}
public function getStatic()
{
return new static();
}
public function getNullableStatic()
{
if (rand(0,1)) {
return new static();
}
return null;
}
}
?>
@ -33,29 +16,12 @@ class Static_
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;
class Static_
final class Static_
{
/**
* @return static
*/
public function getSelf(): static
{
return $this;
}
public function getStatic(): static
{
return new static();
}
public function getNullableStatic(): ?static
{
if (rand(0,1)) {
return new static();
}
return null;
}
}
?>

View File

@ -0,0 +1,33 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;
final class StaticDocBlock
{
/**
* @return static
*/
public function getSelf()
{
return $this;
}
}
?>
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\FixtureForPhp80;
final class StaticDocBlock
{
/**
* @return static
*/
public function getSelf(): static
{
return $this;
}
}
?>

View File

@ -30,7 +30,6 @@ use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\UnionType;
use PhpParser\Parser;
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
@ -41,6 +40,7 @@ use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\Core\PhpParser\Parser\SimplePhpParser;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
@ -61,7 +61,7 @@ final class AnonymousFunctionFactory
private NodeFactory $nodeFactory,
private StaticTypeMapper $staticTypeMapper,
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private Parser $parser,
private SimplePhpParser $simplePhpParser,
private NodeComparator $nodeComparator
) {
}
@ -143,11 +143,11 @@ final class AnonymousFunctionFactory
}
$phpCode = '<?php ' . $expr->value . ';';
$contentNodes = (array) $this->parser->parse($phpCode);
$contentStmts = $this->simplePhpParser->parseString($phpCode);
$anonymousFunction = new Closure();
$firstNode = $contentNodes[0] ?? null;
$firstNode = $contentStmts[0] ?? null;
if (! $firstNode instanceof Expression) {
return null;
}

View File

@ -29,9 +29,6 @@ final class ObjectTypeSpecifier
) {
}
/**
* @return AliasedObjectType|FullyQualifiedObjectType|ObjectType|MixedType
*/
public function narrowToFullyQualifiedOrAliasedObjectType(
Node $node,
ObjectType $objectType

View File

@ -132,13 +132,13 @@ CODE_SAMPLE
$this->phpDocTypeChanger->changeReturnType($phpDocInfo, $inferredReturnType);
if ($phpDocInfo->hasChanged()) {
$node->setAttribute(AttributeKey::HAS_PHP_DOC_INFO_JUST_CHANGED, true);
$this->returnTagRemover->removeReturnTagIfUseless($phpDocInfo, $node);
return $node;
if (! $phpDocInfo->hasChanged()) {
return null;
}
return null;
$node->setAttribute(AttributeKey::HAS_PHP_DOC_INFO_JUST_CHANGED, true);
$this->returnTagRemover->removeReturnTagIfUseless($phpDocInfo, $node);
return $node;
}
private function shouldSkip(ClassMethod $classMethod, PhpDocInfo $phpDocInfo): bool
@ -163,11 +163,6 @@ CODE_SAMPLE
return $currentPhpDocReturnType instanceof IterableType;
}
/**
* @deprecated
* @todo merge to
* @see \Rector\TypeDeclaration\TypeAlreadyAddedChecker\ReturnTypeAlreadyAddedChecker
*/
private function shouldSkipType(
Type $newType,
Type $currentType,

View File

@ -197,30 +197,30 @@ final class ReturnTypeInferer
private function resolveUnionStaticTypes(UnionType $unionType, bool $isSupportedStaticReturnType): UnionType|null
{
$returnTypes = $unionType->getTypes();
$types = [];
$resolvedTypes = [];
$hasStatic = false;
foreach ($returnTypes as $returnType) {
if ($this->isStaticType($returnType)) {
/** @var FullyQualifiedObjectType $returnType */
$types[] = new ThisType($returnType->getClassName());
foreach ($unionType->getTypes() as $unionedType) {
if ($this->isStaticType($unionedType)) {
/** @var FullyQualifiedObjectType $unionedType */
$resolvedTypes[] = new ThisType($unionedType->getClassName());
$hasStatic = true;
continue;
}
$types[] = $returnType;
$resolvedTypes[] = $unionedType;
}
if (! $hasStatic) {
return $unionType;
}
// has static, but it is not supported
if (! $isSupportedStaticReturnType) {
return null;
}
return new UnionType($types);
return new UnionType($resolvedTypes);
}
private function resolveStaticType(

View File

@ -48,7 +48,7 @@ final class InlineCodeParser
public function __construct(
private BetterStandardPrinter $betterStandardPrinter,
private NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
private Parser $parser,
private SimplePhpParser $simplePhpParser,
private SmartFileSystem $smartFileSystem
) {
}
@ -67,8 +67,8 @@ final class InlineCodeParser
$content = Strings::match($content, self::OPEN_PHP_TAG_REGEX) ? $content : '<?php ' . $content;
$content = Strings::match($content, self::ENDING_SEMI_COLON_REGEX) ? $content : $content . ';';
$nodes = (array) $this->parser->parse($content);
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromString($nodes);
$stmts = $this->simplePhpParser->parseString($content);
return $this->nodeScopeAndMetadataDecorator->decorateStmtsFromString($stmts);
}
public function stringify(Expr $expr): string

View File

@ -4,35 +4,46 @@ declare(strict_types=1);
namespace Rector\Core\PhpParser\Parser;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Symplify\SmartFileSystem\SmartFileSystem;
final class SimplePhpParser
{
private Parser $phpParser;
public function __construct(
private Parser $parser,
private SmartFileSystem $smartFileSystem
) {
$parserFactory = new ParserFactory();
$this->phpParser = $parserFactory->create(ParserFactory::PREFER_PHP7);
}
/**
* @return Node[]
* @return Stmt[]
*/
public function parseFile(string $filePath): array
{
$fileContent = $this->smartFileSystem->readFile($filePath);
$nodes = $this->parser->parse($fileContent);
return $this->parseString($fileContent);
}
if ($nodes === null) {
/**
* @return Stmt[]
*/
public function parseString(string $fileContent): array
{
$stmts = $this->phpParser->parse($fileContent);
if ($stmts === null) {
return [];
}
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NodeConnectingVisitor());
return $nodeTraverser->traverse($nodes);
return $nodeTraverser->traverse($stmts);
}
}