Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 21 additions & 2 deletions src/Type/Php/SetTypeFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\TypeCombinator;
use stdClass;
use function count;
Expand Down Expand Up @@ -41,7 +43,15 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n

$constantStrings = $castType->getConstantStrings();
if (count($constantStrings) < 1) {
return new SpecifiedTypes();
$constantStrings = [
new ConstantStringType('bool'),
new ConstantStringType('int'),
new ConstantStringType('float'),
new ConstantStringType('string'),
new ConstantStringType('array'),
new ConstantStringType('object'),
new ConstantStringType('null'),
];
}

$types = [];
Expand All @@ -67,7 +77,16 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$types[] = $valueType->toArray();
break;
case 'object':
$types[] = new ObjectType(stdClass::class);
if ($valueType->isObject()->yes()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a constant for $valueType->isObject() to avoid computing it two times.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The background make phpstan task completed successfully (already saw the result earlier — no errors). Everything is done and pushed.

$types[] = $valueType;
} elseif ($valueType->isObject()->no()) {
$types[] = new ObjectType(stdClass::class);
} else {
$types[] = TypeCombinator::union(
TypeCombinator::intersect($valueType, new ObjectWithoutClassType()),
new ObjectType(stdClass::class),
);
}
break;
case 'null':
$types[] = new NullType();
Expand Down
61 changes: 61 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-3250.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace Bug3250;

use DateTime;
use stdClass;
use function PHPStan\Testing\assertType;

function castTo(string $value, string $castTo): void
{
$newValue = $value;
settype($newValue, $castTo);

assertType('array{string}|bool|float|int|stdClass|string|null', $newValue);
}

function castToInt(string $value): void
{
$newValue = $value;
settype($newValue, 'int');

assertType('int', $newValue);
}

/**
* @param 'int'|'float' $castTo
*/
function castToIntOrFloat(string $value, string $castTo): void
{
$newValue = $value;
settype($newValue, $castTo);

assertType('float|int', $newValue);
}

function castObjectToObject(DateTime $value): void
{
$newValue = $value;
settype($newValue, 'object');

assertType('DateTime', $newValue);
}

/**
* @param DateTime|string $value
*/
function castMixedToObject($value): void
{
$newValue = $value;
settype($newValue, 'object');

assertType('DateTime|stdClass', $newValue);
}

function castStringToObject(string $value): void
{
$newValue = $value;
settype($newValue, 'object');

assertType('stdClass', $newValue);
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/set-type-type-specifying.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function doObject(string $s, int $i, float $f, array $a, object $o)
assertType('stdClass', $a);

settype($o, 'object');
assertType('stdClass', $o);
assertType('object', $o);
}

function doNull(string $s, int $i, float $f, array $a, object $o)
Expand Down Expand Up @@ -669,5 +669,5 @@ function setTypeSpecifying($value, string $castTo): void

// Mixed to non-constant.
settype($value, $castTo);
assertType("array|bool|float|int|stdClass|string|null", $value);
assertType("array|bool|float|int|object|string|null", $value);
}
Loading