Skip to content

Commit

Permalink
[CodeQuality] Skip nullable mixed on ReturnTypeFromStrictScalarReturn…
Browse files Browse the repository at this point in the history
…ExprRector (#2947)

* Map ?mixed to mixed

* Do not match non-scalar return type of function

* Update packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>

* Rename and clean up fixture

* Move mapping for nullable type to private method.

* Fix PHPStan error and skip mixed

Co-authored-by: Abdul Malik Ikhsan <samsonasik@gmail.com>
  • Loading branch information
thomasschiet and samsonasik committed Sep 23, 2022
1 parent df36e38 commit 54cc8e7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return $this->matchTypeForUnionedObjectTypes($type, $typeKind);
}

return $this->mapNullabledType($nullabledType, $typeKind);
}

/**
* @param TypeKind::* $typeKind
*/
private function mapNullabledType(Type $nullabledType, string $typeKind): ?Node
{
// void cannot be nullable
if ($nullabledType instanceof VoidType) {
return null;
Expand All @@ -138,7 +146,7 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
}

/** @var Name $nullabledTypeNode */
if (! $this->nodeNameResolver->isName($nullabledTypeNode, 'false')) {
if (! $this->nodeNameResolver->isNames($nullabledTypeNode, ['false', 'mixed'])) {
return new NullableType($nullabledTypeNode);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

function y()
{
if (rand() < 0.5) {
return null;
}

return json_decode('something', null, 512, JSON_THROW_ON_ERROR);
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
Expand Down Expand Up @@ -53,12 +54,43 @@ public function matchStrictScalarExpr(Expr $expr): ?Type
}

if ($expr instanceof FuncCall) {
return $this->resolveFuncCallType($expr);
$returnType = $this->resolveFuncCallType($expr);

if ($returnType === null) {
return null;
}

if (! $this->isScalarType($returnType)) {
return null;
}

return $returnType;
}

return null;
}

private function isScalarType(Type $type): bool
{
if ($type instanceof StringType && ! $type instanceof ConstantStringType) {
return true;
}

if ($type instanceof FloatType) {
return true;
}

if ($type instanceof IntegerType) {
return true;
}

if ($type instanceof BooleanType) {
return true;
}

return false;
}

private function resolveTypeFromScalar(Scalar $scalar): Type|null
{
if ($scalar instanceof String_) {
Expand Down

0 comments on commit 54cc8e7

Please sign in to comment.