Skip to content

Fix Call to method Assert\AssertionChain::isJsonString() will always evaluate to true. #68

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

Open
wants to merge 1 commit into
base: 2.0.x
Choose a base branch
from
Open
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
78 changes: 67 additions & 11 deletions src/Type/BeberleiAssert/AssertHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use ReflectionObject;
use function array_key_exists;
use function count;
use function is_array;
use function key;
use function reset;

Expand All @@ -62,7 +63,7 @@ public static function isSupported(
}

$resolver = $resolvers[$assertName];
$resolverReflection = new ReflectionObject($resolver);
$resolverReflection = new ReflectionObject(Closure::fromCallable($resolver));

return count($args) >= count($resolverReflection->getMethod('__invoke')->getParameters()) - 1;
}
Expand All @@ -78,7 +79,7 @@ public static function specifyTypes(
bool $nullOr
): SpecifiedTypes
{
$expression = self::createExpression($scope, $assertName, $args);
[$expression, $rootExpr] = self::createExpression($scope, $assertName, $args);
if ($expression === null) {
return new SpecifiedTypes([], []);
}
Expand All @@ -93,11 +94,13 @@ public static function specifyTypes(
);
}

return $typeSpecifier->specifyTypesInCondition(
$specifiedTypes = $typeSpecifier->specifyTypesInCondition(
$scope,
$expression,
TypeSpecifierContext::createTruthy(),
);
)->setRootExpr($rootExpr ?? $expression);

return self::specifyRootExprIfSet($rootExpr, $scope, $specifiedTypes, $typeSpecifier);
}

public static function handleAll(
Expand Down Expand Up @@ -239,21 +242,34 @@ private static function allArrayOrIterable(

/**
* @param Arg[] $args
* @return array{?Expr, ?Expr}
*/
private static function createExpression(
Scope $scope,
string $assertName,
array $args
): ?Expr
): array
{
$resolvers = self::getExpressionResolvers();
$resolver = $resolvers[$assertName];

return $resolver($scope, ...$args);
$resolverResult = $resolver($scope, ...$args);
if (is_array($resolverResult)) {
[$expr, $rootExpr] = $resolverResult;
} else {
$expr = $resolverResult;
$rootExpr = null;
}

if ($expr === null) {
return [null, null];
}

return [$expr, $rootExpr];
}

/**
* @return Closure[]
* @return array<string, callable(Scope, Arg...): (Expr|array{?Expr, ?Expr}|null)>
*/
private static function getExpressionResolvers(): array
{
Expand Down Expand Up @@ -363,10 +379,6 @@ private static function getExpressionResolvers(): array
$class,
],
),
'isJsonString' => static fn (Scope $scope, Arg $value): Expr => new FuncCall(
new Name('is_string'),
[$value],
),
'integerish' => static fn (Scope $scope, Arg $value): Expr => new FuncCall(
new Name('is_numeric'),
[$value],
Expand Down Expand Up @@ -406,7 +418,51 @@ private static function getExpressionResolvers(): array
];
}

$assertionsResultingAtLeastInNonEmptyString = [
'isJsonString',
];
foreach ($assertionsResultingAtLeastInNonEmptyString as $name) {
self::$resolvers[$name] = static fn (Scope $scope, Arg $value): array => self::createIsNonEmptyStringAndSomethingExprPair($name, [$value]);
}

return self::$resolvers;
}

/**
* @param Arg[] $args
* @return array{Expr, Expr}
*/
private static function createIsNonEmptyStringAndSomethingExprPair(string $name, array $args): array
{
$expr = new BooleanAnd(
new FuncCall(
new Name('is_string'),
[$args[0]],
),
new NotIdentical(
$args[0]->value,
new String_(''),
),
);

$rootExpr = new BooleanAnd(
$expr,
new FuncCall(new Name('FAUX_FUNCTION_ ' . $name), $args),
);

return [$expr, $rootExpr];
}

private static function specifyRootExprIfSet(?Expr $rootExpr, Scope $scope, SpecifiedTypes $specifiedTypes, TypeSpecifier $typeSpecifier): SpecifiedTypes
{
if ($rootExpr === null) {
return $specifiedTypes;
}

// Makes consecutive calls with a rootExpr adding unknown info via FAUX_FUNCTION evaluate to true
return $specifiedTypes->unionWith(
$typeSpecifier->create($rootExpr, new ConstantBooleanType(true), TypeSpecifierContext::createTruthy(), $scope),
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public function testExtension(): void
16,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Call to method Assert\AssertionChain::isJsonString() will always evaluate to true.',
22,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Call to method Assert\AssertionChain::isJsonString() will always evaluate to true.',
25,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testExtension(): void
[
'Call to static method Assert\Assertion::string() with string will always evaluate to true.',
12,
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
],
[
'Call to static method Assert\Assertion::allString() with array<string> will always evaluate to true.',
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/BeberleiAssert/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
\PHPStan\Testing\assertType('array<class-string<PHPStan\Type\BeberleiAssert\Foo>|PHPStan\Type\BeberleiAssert\Foo>', $ab);

Assertion::isJsonString($ac);
\PHPStan\Testing\assertType('string', $ac);
\PHPStan\Testing\assertType('non-empty-string', $ac);

/** @var array{a?: string, b?: int} $ad */
$ad = doFoo();
Expand Down
8 changes: 8 additions & 0 deletions tests/Type/BeberleiAssert/data/impossible-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ public function doBar(string $s)
Assert::that($strings)->all()->string();
}

public function nonEmptyStringAndSomethingUnknownNarrow(string $a, array $b): void
{
Assert::that($a)->isJsonString();
Assert::that($a)->isJsonString(); // only this should report

Assert::thatAll($b)->isJsonString();
Assert::thatAll($b)->isJsonString(); // only this should report
}
}