Skip to content

Commit

Permalink
[TypedPropertyFromAssignsRector] Handle parse_url() native function w…
Browse files Browse the repository at this point in the history
…ith second arg on TypedPropertyFromAssignsRector (#6562)

* [TypedPropertyFromAssignsRector] Handle parse_url() native function with second arg on TypedPropertyFromAssignsRector

* update fixture

* Fix type constant

* Fix type constant

* fix

* fix

* typo fix

* rollback tweak to find another solution

* fix

* fix
  • Loading branch information
samsonasik authored Dec 12, 2024
1 parent 1c9f232 commit 4579273
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\FixtureComplexTypes;

final class ParseUrlWithSecondArg
{
private $host = null;

public function __construct(string $url)
{
$this->host = parse_url($url, \PHP_URL_HOST);
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\FixtureComplexTypes;

final class ParseUrlWithSecondArg
{
private string|false|null $host = null;

public function __construct(string $url)
{
$this->host = parse_url($url, \PHP_URL_HOST);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ private function resolveTypeWithVerifyDefaultValue(Property $property, array $as
}

$inferredType = $this->typeFactory->createMixedPassedOrUnionType($assignedExprTypes);
// to compare with default value, constant type must not be kept
// eg, use more general bool over false or true
if ($this->shouldSkipWithDifferentDefaultValueType($defaultPropertyValue, $inferredType)) {
return null;
}

return $inferredType;
// returns with constant as final result
return $this->typeFactory->createMixedPassedOrUnionType($assignedExprTypes, true);
}

private function shouldSkipWithDifferentDefaultValueType(?Expr $expr, Type $inferredType): bool
Expand Down
9 changes: 6 additions & 3 deletions src/NodeAnalyzer/ExprAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ public function isNonTypedFromParam(Expr $expr): bool
}

$nativeType = $scope->getNativeType($expr);
if ($nativeType instanceof MixedType && ! $nativeType->isExplicitMixed()) {
$type = $scope->getType($expr);
if (
($nativeType instanceof MixedType && ! $nativeType->isExplicitMixed())
||
($nativeType instanceof MixedType && ! $type instanceof MixedType)
) {
return true;
}

$type = $scope->getType($expr);

if ($nativeType instanceof ObjectWithoutClassType && ! $type instanceof ObjectWithoutClassType) {
return true;
}
Expand Down
20 changes: 20 additions & 0 deletions src/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassConst;
Expand All @@ -23,6 +25,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Broker\ClassAutoloadingException;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Native\NativeFunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
Expand Down Expand Up @@ -559,6 +562,23 @@ private function resolveNativeTypeWithBuiltinMethodCallFallback(Expr $expr, Scop
}
}

if ($expr instanceof FuncCall) {
if (! $expr->name instanceof Name) {
return $scope->getNativeType($expr);
}

if (! $this->reflectionProvider->hasFunction($expr->name, $scope)) {
return $scope->getNativeType($expr);
}

$functionReflection = $this->reflectionProvider->getFunction($expr->name, $scope);
if (! $functionReflection instanceof NativeFunctionReflection) {
return $scope->getNativeType($expr);
}

return $scope->getType($expr);
}

return $scope->getNativeType($expr);
}
}

0 comments on commit 4579273

Please sign in to comment.