Skip to content

Commit

Permalink
[TypeDeclaration] Improve FlipTypeControlToUseExclusiveTypeRector : a…
Browse files Browse the repository at this point in the history
…dd Nullable support for Assign expr (#5254)

Co-authored-by: rector-bot <tomas@getrector.org>
  • Loading branch information
samsonasik and rector-bot authored Jan 20, 2021
1 parent d5fc2cb commit c2c5228
Show file tree
Hide file tree
Showing 274 changed files with 597 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function readClassAnnotation(Class_ $class, string $annotationClassName):
public function readPropertyAnnotation(Property $property, string $annotationClassName): ?object
{
$propertyReflection = $this->createPropertyReflectionFromPropertyNode($property);
if ($propertyReflection === null) {
if (! $propertyReflection instanceof ReflectionProperty) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDocInfo;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
Expand Down Expand Up @@ -98,7 +99,7 @@ public function createFromNode(Node $node): ?PhpDocInfo
$this->currentNodeProvider->setNode($node);

$docComment = $node->getDocComment();
if ($docComment === null) {
if (! $docComment instanceof Doc) {
if ($node->getComments() !== []) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDocManipulator;

use PhpParser\Node;
use PhpParser\Node\Param;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -143,7 +144,7 @@ public function changeParamType(PhpDocInfo $phpDocInfo, Type $newType, Param $pa
private function notifyChange(): void
{
$node = $this->currentNodeProvider->getNode();
if ($node === null) {
if (! $node instanceof Node) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function parseTagValue(TokenIterator $tokenIterator, string $tag): PhpDoc
$tagValueNode = null;

$currentPhpNode = $this->currentNodeProvider->getNode();
if ($currentPhpNode === null) {
if (! $currentPhpNode instanceof \PhpParser\Node) {
throw new ShouldNotHappenException();
}

Expand Down Expand Up @@ -297,7 +297,7 @@ private function resolveTag(TokenIterator $tokenIterator): string
private function matchTagToPhpDocNodeFactory(string $tag): ?PhpDocNodeFactoryInterface
{
$currentPhpNode = $this->currentNodeProvider->getNode();
if ($currentPhpNode === null) {
if (! $currentPhpNode instanceof \PhpParser\Node) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getSilentKey(): string
private function addCustomItems(array $items): array
{
if ($this->indexes !== []) {
if ($this->indexesAroundSpaces === null) {
if (! $this->indexesAroundSpaces instanceof AroundSpaces) {
throw new ShouldNotHappenException();
}

Expand All @@ -115,7 +115,7 @@ private function addCustomItems(array $items): array
}

if ($this->uniqueConstraints !== []) {
if ($this->uniqueConstraintsAroundSpaces === null) {
if (! $this->uniqueConstraintsAroundSpaces instanceof AroundSpaces) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private function createJoinColumnItems(string $joinColumnsKey, string $inverseJo
$items = [];

if ($this->joinColumns !== []) {
if ($this->joinColumnsAroundSpaces === null) {
if (! $this->joinColumnsAroundSpaces instanceof AroundSpaces) {
throw new ShouldNotHappenException();
}

Expand All @@ -168,7 +168,7 @@ private function createJoinColumnItems(string $joinColumnsKey, string $inverseJo
}

if ($this->inverseJoinColumns !== []) {
if ($this->inverseJoinColumnsAroundSpaces === null) {
if (! $this->inverseJoinColumnsAroundSpaces instanceof AroundSpaces) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser;

use Iterator;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
Expand Down Expand Up @@ -69,7 +70,7 @@ protected function doTestPrintedPhpDocInfo(SmartFileInfo $fileInfo, string $tagV
$nodeWithPhpDocInfo = $this->parseFileAndGetFirstNodeOfType($fileInfo, $nodeType);

$docComment = $nodeWithPhpDocInfo->getDocComment();
if ($docComment === null) {
if (! $docComment instanceof Doc) {
throw new ShouldNotHappenException(sprintf('Doc comments for "%s" file cannot not be empty', $fileInfo));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ function (RectorWithFileAndLineChange $rectorWithFileAndLineChange) use ($smartF
public function notifyNodeFileInfo(Node $node): void
{
$fileInfo = $node->getAttribute(AttributeKey::FILE_INFO);
if ($fileInfo === null) {
if (! $fileInfo instanceof SmartFileInfo) {
// this file was changed before and this is a sub-new node
// array Traverse to all new nodes would have to be used, but it's not worth the performance
return;
}

$currentRector = $this->currentRectorProvider->getCurrentRector();
if ($currentRector === null) {
if (! $currentRector instanceof RectorInterface) {
throw new ShouldNotHappenException();
}

Expand Down
5 changes: 3 additions & 2 deletions packages/family-tree/src/NodeAnalyzer/ClassChildAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\Class_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use ReflectionClass;
use ReflectionMethod;

final class ClassChildAnalyzer
{
Expand All @@ -21,7 +22,7 @@ public function hasChildClassConstructor(Class_ $class): bool

$reflectionClass = new ReflectionClass($childClass);
$constructorReflectionMethod = $reflectionClass->getConstructor();
if ($constructorReflectionMethod === null) {
if (! $constructorReflectionMethod instanceof ReflectionMethod) {
continue;
}

Expand All @@ -48,7 +49,7 @@ public function hasParentClassConstructor(Class_ $class): bool
foreach ($classParents as $classParent) {
$parentReflectionClass = new ReflectionClass($classParent);
$constructMethodReflection = $parentReflectionClass->getConstructor();
if ($constructMethodReflection === null) {
if (! $constructMethodReflection instanceof ReflectionMethod) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function isPropertyFetchedInChildClass(Property $property): bool
$childrenClassNames = $this->familyRelationsAnalyzer->getChildrenOfClass($className);
foreach ($childrenClassNames as $childClassName) {
$childClass = $this->nodeRepository->findClass($childClassName);
if ($childClass === null) {
if (! $childClass instanceof Class_) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function collect(Node $node): void
// array callable - [$this, 'someCall']
if ($node instanceof Array_) {
$arrayCallable = $this->arrayCallableMethodReferenceAnalyzer->match($node);
if ($arrayCallable === null) {
if (! $arrayCallable instanceof ArrayCallable) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function provideByClassAndMethodName(string $class, string $method, Scope
public function provideParameterTypesByStaticCall(StaticCall $staticCall): array
{
$methodReflection = $this->provideByStaticCall($staticCall);
if ($methodReflection === null) {
if (! $methodReflection instanceof MethodReflection) {
return [];
}

Expand Down Expand Up @@ -120,7 +120,7 @@ public function provideByStaticCall(StaticCall $staticCall): ?MethodReflection
public function provideParameterTypesByClassMethod(ClassMethod $classMethod): array
{
$methodReflection = $this->provideByClassMethod($classMethod);
if ($methodReflection === null) {
if (! $methodReflection instanceof MethodReflection) {
return [];
}

Expand Down
2 changes: 1 addition & 1 deletion packages/node-nesting-scope/src/ScopeNestingComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function isNodeConditionallyScoped(Node $node): bool
ControlStructure::CONDITIONAL_NODE_SCOPE_TYPES + [FunctionLike::class]
);

if ($foundParentType === null) {
if (! $foundParentType instanceof Node) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/node-type-resolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public function isPropertyBoolean(Property $property): bool
}

$defaultNodeValue = $property->props[0]->default;
if ($defaultNodeValue === null) {
if (! $defaultNodeValue instanceof \PhpParser\Node\Expr) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function getNodeClasses(): array
public function resolve(Node $node): Type
{
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
if ($classLike === null) {
if (! $classLike instanceof ClassLike) {
// anonymous class
return new ObjectWithoutClassType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -115,7 +116,7 @@ private function resolveFromParamType(Param $param): Type
private function resolveFromFirstVariableUse(Param $param): Type
{
$classMethod = $param->getAttribute(AttributeKey::METHOD_NODE);
if ($classMethod === null) {
if (! $classMethod instanceof ClassMethod) {
return new MixedType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ public function resolve(Node $node): Type
}
}

if ($scope === null) {
if (! $scope instanceof Scope) {
$classNode = $node->getAttribute(AttributeKey::CLASS_NODE);
// fallback to class, since property fetches are not scoped by PHPStan
if ($classNode instanceof ClassLike) {
$scope = $classNode->getAttribute(AttributeKey::SCOPE);
}

if ($scope === null) {
if (! $scope instanceof Scope) {
return new MixedType();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private function resolveTypesFromScope(Variable $variable, string $variableName)
{
$nodeScope = $this->resolveNodeScope($variable);

if ($nodeScope === null) {
if (! $nodeScope instanceof Scope) {
return new MixedType();
}

Expand Down Expand Up @@ -147,7 +147,7 @@ private function resolveFromParentNodes(Variable $variable): ?Scope
}

$parentNodeScope = $parentNode->getAttribute(AttributeKey::SCOPE);
if ($parentNodeScope === null) {
if (! $parentNodeScope instanceof Scope) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private function processClosure(Node $node): void
private function setClassNodeAndName(?ClassLike $classLike): void
{
$this->classLike = $classLike;
if ($classLike === null || $classLike->name === null) {
if (! $classLike instanceof ClassLike || $classLike->name === null) {
$this->className = null;
} elseif (property_exists($classLike, 'namespacedName')) {
$this->className = $classLike->namespacedName->toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function beforeTraverse(array $nodes): ?array
public function enterNode(Node $node): ?Node
{
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parent === null) {
if (! $parent instanceof Node) {
if (! $node instanceof Stmt) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\BoolUnionTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis;

final class UnionTypeMapper implements TypeMapperInterface
{
Expand Down Expand Up @@ -119,7 +120,7 @@ public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node

// special case for nullable
$nullabledType = $this->matchTypeForNullableUnionType($type);
if ($nullabledType === null) {
if (! $nullabledType instanceof Type) {
// use first unioned type in case of unioned object types
return $this->matchTypeForUnionedObjectTypes($type);
}
Expand Down Expand Up @@ -166,7 +167,7 @@ public function mapToDocString(Type $type, ?Type $parentType = null): string
private function shouldSkipIterable(UnionType $unionType): bool
{
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
if ($unionTypeAnalysis === null) {
if (! $unionTypeAnalysis instanceof UnionTypeAnalysis) {
return false;
}
if (! $unionTypeAnalysis->hasIterable()) {
Expand All @@ -181,7 +182,7 @@ private function shouldSkipIterable(UnionType $unionType): bool
private function matchArrayTypes(UnionType $unionType): ?Node
{
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
if ($unionTypeAnalysis === null) {
if (! $unionTypeAnalysis instanceof UnionTypeAnalysis) {
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/post-rector/src/Collector/UseNodesToAddCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function addUseImport(Node $positionNode, ObjectType $objectType): void
{
/** @var SmartFileInfo|null $fileInfo */
$fileInfo = $positionNode->getAttribute(AttributeKey::FILE_INFO);
if ($fileInfo === null) {
if (! $fileInfo instanceof \Symplify\SmartFileSystem\SmartFileInfo) {
// fallback for freshly created Name nodes
$fileInfo = $this->currentFileInfoProvider->getSmartFileInfo();
if ($fileInfo === null) {
if (! $fileInfo instanceof \Symplify\SmartFileSystem\SmartFileInfo) {
return;
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ private function getRealPathFromNode(Node $node): ?string
}

$smartFileInfo = $this->currentFileInfoProvider->getSmartFileInfo();
if ($smartFileInfo === null) {
if (! $smartFileInfo instanceof \Symplify\SmartFileSystem\SmartFileInfo) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/post-rector/src/Rector/UseAddingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function beforeTraverse(array $nodes): array
}

$smartFileInfo = $this->getSmartFileInfo($nodes);
if ($smartFileInfo === null) {
if (! $smartFileInfo instanceof SmartFileInfo) {
return $nodes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function isRead(Node $node): bool
Assert::isAnyOf($node, [PropertyFetch::class, StaticPropertyFetch::class]);

$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parent === null) {
if (! $parent instanceof Node) {
throw new MissingParentNodeException();
}

Expand Down Expand Up @@ -93,7 +93,7 @@ private function unwrapPostPreIncDec(Node $node): Node
{
if ($node instanceof PreInc || $node instanceof PreDec || $node instanceof PostInc || $node instanceof PostDec) {
$node = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($node === null) {
if (! $node instanceof Node) {
throw new MissingParentNodeException();
}
}
Expand All @@ -104,7 +104,7 @@ private function unwrapPostPreIncDec(Node $node): Node
private function isArrayDimFetchRead(ArrayDimFetch $arrayDimFetch): bool
{
$parentParent = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
if ($parentParent === null) {
if (! $parentParent instanceof Node) {
throw new MissingParentNodeException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(?RectorRecipe $rectorRecipe = null)

public function provide(): RectorRecipe
{
if ($this->rectorRecipe === null) {
if (! $this->rectorRecipe instanceof RectorRecipe) {
throw new ConfigurationException(self::MESSAGE);
}

Expand Down
Loading

0 comments on commit c2c5228

Please sign in to comment.