Skip to content

Commit

Permalink
Remove deprecated instanceof (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar authored Apr 21, 2023
1 parent 77ccee0 commit 01f4747
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
33 changes: 14 additions & 19 deletions src/WpParseUrlFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
Expand Down Expand Up @@ -63,32 +62,28 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

$this->cacheReturnTypes();

$urlType = $scope->getType($functionCall->getArgs()[0]->value);
$componentType = new ConstantIntegerType(-1);
if (count($functionCall->getArgs()) > 1) {
$componentType = $scope->getType($functionCall->getArgs()[1]->value);

if (!$componentType instanceof ConstantType) {
return $this->createAllComponentsReturnType();
}

$componentType = $componentType->toInteger();

if (!$componentType instanceof ConstantIntegerType) {
throw new \PHPStan\ShouldNotHappenException();
return $this->createAllComponentsReturnType();
}
} else {
$componentType = new ConstantIntegerType(-1);
}

if ($urlType instanceof ConstantStringType) {
try {
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
$result = @parse_url($urlType->getValue(), $componentType->getValue());
} catch (\ValueError $e) {
return new ConstantBooleanType(false);
$urlType = $scope->getType($functionCall->getArgs()[0]->value);
if (count($urlType->getConstantStrings()) !== 0) {
$returnTypes = [];
foreach ($urlType->getConstantStrings() as $constantString) {
try {
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
$result = @parse_url($constantString->getValue(), $componentType->getValue());
$returnTypes[] = $scope->getTypeFromValue($result);
} catch (\ValueError $e) {
$returnTypes[] = new ConstantBooleanType(false);
}
}

return $scope->getTypeFromValue($result);
return TypeCombinator::union(...$returnTypes);
}

if ($componentType->getValue() === -1) {
Expand Down
22 changes: 9 additions & 13 deletions tests/data/wp_parse_url.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,8 @@

namespace SzepeViktor\PHPStan\WordPress\Tests;

use function wp_parse_url;
use function PHPStan\Testing\assertType;
use function wp_clear_scheduled_hook;
use function wp_insert_attachment;
use function wp_insert_category;
use function wp_insert_link;
use function wp_insert_post;
use function wp_reschedule_event;
use function wp_schedule_event;
use function wp_schedule_single_event;
use function wp_set_comment_status;
use function wp_unschedule_event;
use function wp_unschedule_hook;
use function wp_update_comment;
use function wp_update_post;

/** @var int $integer */
$integer = doFoo();
Expand Down Expand Up @@ -61,3 +49,11 @@

$value = wp_parse_url($string);
assertType('array{scheme?: string, host?: string, port?: int, user?: string, pass?: string, path?: string, query?: string, fragment?: string}|false', $value);

/** @var 'http://def.abc'|'https://example.com' $union */
$union = $union;
assertType("array{scheme: 'http', host: 'def.abc'}|array{scheme: 'https', host: 'example.com'}", wp_parse_url($union));

/** @var 'http://def.abc#fragment1'|'https://example.com#fragment2' $union */
$union = $union;
assertType("'fragment1'|'fragment2'", wp_parse_url($union, PHP_URL_FRAGMENT));

0 comments on commit 01f4747

Please sign in to comment.