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

Remove deprecated instanceof from wp_parse_url extension #183

Merged
merged 1 commit into from
Apr 21, 2023
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
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));