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
  • Loading branch information
samsonasik committed Dec 11, 2024
1 parent 1c9f232 commit 95a5f20
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 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 _cosntructor(string $url)
{
$this->host = parse_url($url, \PHP_URL_HOST);
}
}

?>
-----
<?php

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

final class ParseUrlWithSecondArg
{
private $host = null;

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

?>
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 95a5f20

Please sign in to comment.