-
Notifications
You must be signed in to change notification settings - Fork 524
Implement JsonValidateTypeSpecifyingExtension #2491
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
Changes from all commits
98b4b0a
2f685b0
f00cb4a
957978a
14214b6
edd267b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php // phpcs:ignoreFile | ||
|
||
/** | ||
* Copied over from https://github.com/phan/phan/blob/8866d6b98be94b37996390da226e8c4befea29aa/src/Phan/Language/Internal/FunctionSignatureMap_php80_delta.php | ||
* Copyright (c) 2015 Rasmus Lerdorf | ||
* Copyright (c) 2015 Andrew Morrison | ||
*/ | ||
|
||
/** | ||
* This contains the information needed to convert the function signatures for php 8.0 to php 7.4 (and vice versa) | ||
* | ||
* This has two sections. | ||
* The 'new' section contains function/method names from FunctionSignatureMap (And alternates, if applicable) that do not exist in php7.4 or have different signatures in php 8.0. | ||
* If they were just updated, the function/method will be present in the 'added' signatures. | ||
* The 'old' signatures contains the signatures that are different in php 7.4. | ||
* Functions are expected to be removed only in major releases of php. | ||
* | ||
* @see FunctionSignatureMap.php | ||
* | ||
* @phan-file-suppress PhanPluginMixedKeyNoKey (read by Phan when analyzing this file) | ||
*/ | ||
return [ | ||
'new' => [ | ||
'json_validate' => ['bool', 'json'=>'string', 'depth='=>'positive-int', 'flags='=>'int-mask<JSON_INVALID_UTF8_IGNORE>'], | ||
], | ||
'old' => [ | ||
|
||
] | ||
]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\SpecifiedTypes; | ||
use PHPStan\Analyser\TypeSpecifier; | ||
use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
use PHPStan\Analyser\TypeSpecifierContext; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; | ||
use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
use function count; | ||
|
||
class JsonValidateTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried using a stub signature instead, but this did not work for me locally:
as I can see this extension also only work in CI PHP 8.3.. I will retry using a stub signature on a different composer which has PHP 8.3 installed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, did a few more tests. It does not work with pure stub signatures, because these would also affect the ELSE case in a wrong way:
|
||
{ | ||
|
||
private TypeSpecifier $typeSpecifier; | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool | ||
{ | ||
return $functionReflection->getName() === 'json_validate' && isset($node->getArgs()[0]) && $context->true(); | ||
} | ||
|
||
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes | ||
{ | ||
$args = $node->getArgs(); | ||
|
||
if (count($args) < 1) { | ||
return new SpecifiedTypes([], []); | ||
} | ||
|
||
return $this->typeSpecifier->create($node->getArgs()[0]->value, new AccessoryNonEmptyStringType(), $context, false, $scope); | ||
} | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace JsonValidate; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
function doFoo($m): void { | ||
assertType('bool', json_validate($m)); | ||
|
||
if (json_validate($m)) { | ||
assertType('non-empty-string', $m); | ||
} else { | ||
assertType('mixed', $m); | ||
} | ||
assertType('mixed', $m); | ||
} | ||
|
||
/** | ||
* @param non-empty-string $nonES | ||
*/ | ||
function doBar($nonES): void { | ||
if (json_validate($nonES)) { | ||
assertType('non-empty-string', $nonES); | ||
} else { | ||
assertType('non-empty-string', $nonES); | ||
} | ||
assertType('non-empty-string', $nonES); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace JsonValidateParams; | ||
|
||
function doFoo() { | ||
$x = json_validate('{ "test": { "foo": "bar" } }', 0, 0); // invalid depth | ||
$x = json_validate('{ "test": { "foo": "bar" } }', 100, JSON_BIGINT_AS_STRING); // invalid flags | ||
|
||
$x = json_validate('{ "test": { "foo": "bar" } }'); | ||
$x = json_validate('{ "test": { "foo": "bar" } }', 100, 0); | ||
$x = json_validate('{ "test": { "foo": "bar" } }', 100, JSON_INVALID_UTF8_IGNORE); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only a single flag is supported, see php/php-src#9399 (comment)