Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PHPStan\Type\BooleanType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -74,11 +75,11 @@ public function refactor(Node $node): ?Node

if ($node->expr instanceof Identical) {
$identical = $node->expr;
if (! $this->isBoolType($identical->left)) {
if (! $this->isStaticType($identical->left, BooleanType::class)) {
return null;
}

if (! $this->isBoolType($identical->right)) {
if (! $this->isStaticType($identical->right, BooleanType::class)) {
return null;
}

Expand All @@ -90,11 +91,11 @@ public function refactor(Node $node): ?Node

private function processIdentical(Identical $identical): ?NotIdentical
{
if (! $this->isBoolType($identical->left)) {
if (! $this->isStaticType($identical->left, BooleanType::class)) {
return null;
}

if (! $this->isBoolType($identical->right)) {
if (! $this->isStaticType($identical->right, BooleanType::class)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PHPStan\Type\BooleanType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -55,11 +56,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($this->isBoolType($node->left) && ! $this->isBool($node->left)) {
if ($this->isStaticType($node->left, BooleanType::class) && ! $this->isBool($node->left)) {
return $this->processBoolTypeToNotBool($node, $node->left, $node->right);
}

if ($this->isBoolType($node->right) && ! $this->isBool($node->right)) {
if ($this->isStaticType($node->right, BooleanType::class) && ! $this->isBool($node->right)) {
return $this->processBoolTypeToNotBool($node, $node->right, $node->left);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\BooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -84,7 +87,7 @@ public function refactor(Node $node): ?Node
$isNegated = false;
}

if ($this->isBoolType($conditionNode)) {
if ($this->isStaticType($conditionNode, BooleanType::class)) {
return null;
}

Expand Down Expand Up @@ -113,11 +116,11 @@ private function resolveNewConditionNode(Expr $expr, bool $isNegated): ?BinaryOp
return $this->resolveString($isNegated, $expr);
}

if ($this->isIntegerType($expr)) {
if ($this->isStaticType($expr, IntegerType::class)) {
return $this->resolveInteger($isNegated, $expr);
}

if ($this->isFloatType($expr)) {
if ($this->isStaticType($expr, FloatType::class)) {
return $this->resolveFloat($isNegated, $expr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\BooleanType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -168,7 +169,7 @@ private function boolCastOrNullCompareIfNeeded(Expr $expr): Expr
return $expr;
}

if ($this->isBoolType($expr)) {
if ($this->isStaticType($expr, BooleanType::class)) {
return $expr;
}

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

use PhpParser\Node;
use PhpParser\Node\Expr\Ternary;
use PHPStan\Type\BooleanType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -52,7 +53,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isBoolType($node->cond)) {
if (! $this->isStaticType($node->cond, BooleanType::class)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\Ternary;
use PHPStan\Type\BooleanType;
use Rector\PhpParser\Node\AssignAndBinaryMap;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -89,15 +90,15 @@ public function refactor(Node $node): ?Node
private function processNonBinaryCondition(Expr $ifExpression, Expr $elseExpression, Expr $condition): ?Node
{
if ($this->isTrue($ifExpression) && $this->isFalse($elseExpression)) {
if ($this->isBoolType($condition)) {
if ($this->isStaticType($condition, BooleanType::class)) {
return $condition;
}

return new Bool_($condition);
}

if ($this->isFalse($ifExpression) && $this->isTrue($elseExpression)) {
if ($this->isBoolType($condition)) {
if ($this->isStaticType($condition, BooleanType::class)) {
return new BooleanNot($condition);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/NetteTesterToPHPUnit/src/AssertManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\Type\BooleanType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
Expand Down Expand Up @@ -218,7 +219,7 @@ private function processTruthyOrFalseyCall(StaticCall $staticCall): Expr
$call->name = new Identifier($method);
}

if (! $this->nodeTypeResolver->isBoolType($staticCall->args[0]->value)) {
if (! $this->nodeTypeResolver->isStaticType($staticCall->args[0]->value, BooleanType::class)) {
$call->args[0]->value = new Bool_($staticCall->args[0]->value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\Manipulator\ConstFetchManipulator;

Expand Down Expand Up @@ -41,11 +43,11 @@ public function resolver(Node $node): string
return 'float';
}

if ($this->nodeTypeResolver->isIntType($node)) {
if ($this->nodeTypeResolver->isStaticType($node, IntegerType::class)) {
return 'int';
}

if ($this->nodeTypeResolver->isStringType($node)) {
if ($this->nodeTypeResolver->isStaticType($node, StringType::class)) {
return 'string';
}

Expand Down
66 changes: 30 additions & 36 deletions packages/NodeTypeResolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
Expand All @@ -41,6 +40,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverAwareInterface;
use Rector\NodeTypeResolver\Contract\PerNodeTypeResolver\PerNodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -191,24 +191,9 @@ public function resolve(Node $node): array
return $types;
}

public function isStringType(Node $node): bool
{
return $this->getNodeStaticType($node) instanceof StringType;
}

public function isIntType(Node $node): bool
{
return $this->getNodeStaticType($node) instanceof IntegerType;
}

public function isFloatType(Node $node): bool
{
return $this->getNodeStaticType($node) instanceof FloatType;
}

public function isStringyType(Node $node): bool
{
$nodeType = $this->getNodeStaticType($node);
$nodeType = $this->getStaticType($node);
if ($nodeType instanceof StringType) {
return true;
}
Expand All @@ -226,32 +211,22 @@ public function isStringyType(Node $node): bool
return false;
}

public function isNullType(Node $node): bool
{
return $this->getNodeStaticType($node) instanceof NullType;
}

/**
* e.g. string|null, ObjectNull|null
*/
public function isNullableType(Node $node): bool
{
$nodeType = $this->getNodeStaticType($node);
$nodeType = $this->getStaticType($node);
if (! $nodeType instanceof UnionType) {
return false;
}

return $nodeType->isSuperTypeOf(new NullType())->yes();
}

public function isBoolType(Node $node): bool
{
return $this->getNodeStaticType($node) instanceof BooleanType;
}

public function isCountableType(Node $node): bool
{
$nodeType = $this->getNodeStaticType($node);
$nodeType = $this->getStaticType($node);
if ($nodeType === null) {
return false;
}
Expand All @@ -266,7 +241,7 @@ public function isCountableType(Node $node): bool

public function isArrayType(Node $node): bool
{
$nodeStaticType = $this->getNodeStaticType($node);
$nodeStaticType = $this->getStaticType($node);
if ($nodeStaticType === null) {
return false;
}
Expand Down Expand Up @@ -296,7 +271,7 @@ public function isArrayType(Node $node): bool
return $nodeStaticType instanceof ArrayType;
}

public function getNodeStaticType(Node $node): ?Type
public function getStaticType(Node $node): ?Type
{
if ($node instanceof String_) {
return new ConstantStringType($node->value);
Expand Down Expand Up @@ -330,7 +305,7 @@ public function getNodeStaticType(Node $node): ?Type
public function resolveSingleTypeToStrings(Node $node): array
{
if ($this->isArrayType($node)) {
$arrayType = $this->getNodeStaticType($node);
$arrayType = $this->getStaticType($node);
if ($arrayType instanceof ArrayType) {
$itemTypes = $this->staticTypeToStringResolver->resolveObjectType($arrayType->getItemType());

Expand All @@ -350,14 +325,14 @@ public function resolveSingleTypeToStrings(Node $node): array
return ['string'];
}

$nodeStaticType = $this->getNodeStaticType($node);
$nodeStaticType = $this->getStaticType($node);

return $this->staticTypeToStringResolver->resolveObjectType($nodeStaticType);
}

public function isNullableObjectType(Node $node): bool
{
$nodeType = $this->getNodeStaticType($node);
$nodeType = $this->getStaticType($node);
if (! $nodeType instanceof UnionType) {
return false;
}
Expand All @@ -381,7 +356,26 @@ public function isNullableObjectType(Node $node): bool

public function isNumberType(Node $node): bool
{
return $this->isIntType($node) || $this->isFloatType($node);
return $this->isStaticType($node, IntegerType::class) || $this->isStaticType($node, FloatType::class);
}

public function isStaticType(Node $node, string $staticTypeClass): bool
{
if (! is_a($staticTypeClass, Type::class, true)) {
throw new ShouldNotHappenException(sprintf(
'"%s" in "%s()" must be type of "%s"',
$staticTypeClass,
__METHOD__,
Type::class
));
}

$nodeStaticType = $this->getStaticType($node);
if ($nodeStaticType === null) {
return false;
}

return is_a($nodeStaticType, $staticTypeClass);
}

private function addPerNodeTypeResolver(PerNodeTypeResolverInterface $perNodeTypeResolver): void
Expand Down Expand Up @@ -611,7 +605,7 @@ function (Node $node) use ($paramName, &$paramStaticType): ?int {
return null;
}

$paramStaticType = $this->getNodeStaticType($node);
$paramStaticType = $this->getStaticType($node);

return NodeTraverser::STOP_TRAVERSAL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\StringType;
use Rector\Rector\AbstractPHPUnitRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -78,7 +79,7 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->isStringType($node->args[1]->value)) {
if (! $this->isStaticType($node->args[1]->value, StringType::class)) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/Php/src/Rector/FuncCall/CountOnNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\LNumber;
use PHPStan\Type\NullType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -71,7 +72,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->isNullableType($countedNode) || $this->isNullType($countedNode)) {
if ($this->isNullableType($countedNode) || $this->isStaticType($countedNode, NullType::class)) {
$identicalNode = new Identical($countedNode, $this->createNull());
$ternaryNode = new Ternary($identicalNode, new LNumber(0), $node);
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/Php/src/Rector/FuncCall/GetClassOnNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Type\NullType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -87,7 +88,7 @@ public function refactor(Node $node): ?Node
}

$valueNode = $node->args[0]->value;
if (! $this->isNullableType($valueNode) && ! $this->isNullType($valueNode)) {
if (! $this->isNullableType($valueNode) && ! $this->isStaticType($valueNode, NullType::class)) {
return null;
}

Expand Down
Loading