diff --git a/extension.neon b/extension.neon index b990a81..1abe9b7 100644 --- a/extension.neon +++ b/extension.neon @@ -1,6 +1,10 @@ services: - class: SzepeViktor\PHPStan\WordPress\HookDocBlock + - + class: SzepeViktor\PHPStan\WordPress\ApplyFiltersDynamicFunctionReturnTypeExtension + tags: + - phpstan.broker.dynamicFunctionReturnTypeExtension - class: SzepeViktor\PHPStan\WordPress\EscSqlDynamicFunctionReturnTypeExtension tags: @@ -10,7 +14,7 @@ services: tags: - phpstan.broker.dynamicFunctionReturnTypeExtension - - class: SzepeViktor\PHPStan\WordPress\ApplyFiltersDynamicFunctionReturnTypeExtension + class: SzepeViktor\PHPStan\WordPress\StripslashesFromStringsOnlyDynamicFunctionReturnTypeExtension tags: - phpstan.broker.dynamicFunctionReturnTypeExtension - diff --git a/src/StripslashesFromStringsOnlyDynamicFunctionReturnTypeExtension.php b/src/StripslashesFromStringsOnlyDynamicFunctionReturnTypeExtension.php new file mode 100644 index 0000000..56a8e43 --- /dev/null +++ b/src/StripslashesFromStringsOnlyDynamicFunctionReturnTypeExtension.php @@ -0,0 +1,42 @@ +getName() === 'stripslashes_from_strings_only'; + } + + /** + * @see https://developer.wordpress.org/reference/functions/stripslashes_from_strings_only/ + */ + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type + { + if (count($functionCall->getArgs()) === 0) { + return null; + } + + $argType = $scope->getType($functionCall->getArgs()[0]->value); + + return TypeTraverser::map( + $argType, + static function (Type $type): Type { + if ($type instanceof ConstantStringType) { + return new ConstantStringType(stripslashes($type->getValue())); + } + return $type; + } + ); + } +} diff --git a/tests/DynamicReturnTypeExtensionTest.php b/tests/DynamicReturnTypeExtensionTest.php index 98fbb6a..9f5a60e 100644 --- a/tests/DynamicReturnTypeExtensionTest.php +++ b/tests/DynamicReturnTypeExtensionTest.php @@ -16,6 +16,7 @@ public function dataFileAsserts(): iterable yield from self::gatherAssertTypes(__DIR__ . '/data/ApplyFiltersTestClass.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/esc_sql.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/shortcode_atts.php'); + yield from self::gatherAssertTypes(__DIR__ . '/data/stripslashes-from-strings-only.php'); yield from self::gatherAssertTypes(__DIR__ . '/data/wp_parse_url.php'); } diff --git a/tests/data/stripslashes-from-strings-only.php b/tests/data/stripslashes-from-strings-only.php new file mode 100644 index 0000000..9fb7c42 --- /dev/null +++ b/tests/data/stripslashes-from-strings-only.php @@ -0,0 +1,18 @@ +