Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from
Dec 12, 2024
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
@@ -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);
}
}
Loading