Skip to content
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
60 changes: 30 additions & 30 deletions src/Internal/Checker/ParamChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use TypePHP\Internal\Validator\TypeValidatorRegistry;

/**
* @phpstan-import-type FunctionContract from DocblockParser
*
* @internal Evaluates function and method parameter contract validations (including dynamic @method calls via __call / __callStatic).
*/
final class ParamChecker
Expand Down Expand Up @@ -102,23 +104,7 @@ private static function isUnconstrained(TypeNode $typeNode): bool

/**
* @param array<string, mixed> $vars
* @param array{
* types: array<string, TypeNode>,
* templates: array<string, TemplateTagValueNode>,
* classTemplates: array<string, TemplateTagValueNode>,
* return: ?TypeNode,
* aliases: array<string, TypeNode>,
* hasParamContract: bool,
* hasReturnContract: bool,
* paramsUseGenerics: bool,
* returnUsesGenerics: bool,
* returnUsesMethodTemplates: bool,
* returnIsThis: bool,
* returnIsDynamic: bool,
* allParamsUnconstrained: bool,
* returnUnconstrained: bool,
* isSimple: bool
* }|null $contract Pre-resolved contract to avoid re-parsing
* @param FunctionContract|null $contract Pre-resolved contract to avoid re-parsing
*/
public static function checkParams(
string $function,
Expand Down Expand Up @@ -170,9 +156,10 @@ public static function checkParams(
$classTemplates = $contract['classTemplates'] ?? [];
$aliases = $contract['aliases'];
$hasMethodTemplates = (\count($methodTemplates) > 0);
$sensitiveParams = $contract['sensitiveParams'] ?? [];

if (! $paramsUseGenerics && ! $hasMethodTemplates && \count($aliases) === 0) {
return self::validateSimpleParams($contract['types'], $vars, $effectiveFunction, $registry);
return self::validateSimpleParams($contract['types'], $vars, $effectiveFunction, $registry, $sensitiveParams);
}

self::prepareGenericBindings($effectiveFunction, $methodTemplates, $thisObj, $classTemplates, $thisOrClass);
Expand Down Expand Up @@ -203,7 +190,8 @@ public static function checkParams(
$declaredTemplates,
$registry,
$classTemplates,
$thisOrClass
$thisOrClass,
$sensitiveParams
);
}

Expand All @@ -212,12 +200,14 @@ public static function checkParams(
*
* @param array<string, TypeNode> $types
* @param array<string, mixed> $vars
* @param array<string, bool> $sensitiveParams
*/
private static function validateSimpleParams(
array $types,
array $vars,
string $effectiveFunction,
TypeValidatorRegistry $registry
TypeValidatorRegistry $registry,
array $sensitiveParams = []
): ?ErrorMessage {
foreach ($types as $paramName => $typeNode) {
if (isset($vars[$paramName]) || \array_key_exists($paramName, $vars)) {
Expand All @@ -231,7 +221,9 @@ private static function validateSimpleParams(
if (self::isUnconstrained($typeNode)) {
continue;
}
$err = $registry->validate($vars[$paramName], $typeNode, '');

$isSensitive = $sensitiveParams[$paramName] ?? false;
$err = $registry->validate($vars[$paramName], $typeNode, '', $isSensitive);
if ($err !== null) {
return ErrorFactory::createError($effectiveFunction . '(): Argument $' . $paramName . $err->getMessage());
}
Expand Down Expand Up @@ -310,6 +302,7 @@ private static function resolveBaseTypes(
* @param array<string, TypeNode> $boundTemplates
* @param array<string, TemplateTagValueNode> $declaredTemplates
* @param array<string, TemplateTagValueNode> $classTemplates
* @param array<string, bool> $sensitiveParams
*/
private static function validateAllParameters(
array $contractTypes,
Expand All @@ -323,7 +316,8 @@ private static function validateAllParameters(
array $declaredTemplates,
TypeValidatorRegistry $registry,
array $classTemplates,
object|string|null $thisOrClass = null
object|string|null $thisOrClass = null,
array $sensitiveParams = []
): ?ErrorMessage {
foreach ($contractTypes as $paramName => $_) {
if (! isset($vars[$paramName]) && ! \array_key_exists($paramName, $vars)) {
Expand All @@ -334,6 +328,8 @@ private static function validateAllParameters(
? TemplateManager::getBoundTemplates($effectiveFunction, $thisOrClass, $allTemplates)
: $boundTemplates;

$isSensitive = $sensitiveParams[$paramName] ?? false;

$err = self::validateSingleParam(
$paramName,
$baseTypes[$paramName],
Expand All @@ -347,7 +343,8 @@ private static function validateAllParameters(
$registry,
$classTemplates,
$vars,
$thisOrClass
$thisOrClass,
$isSensitive
);

if ($err !== null) {
Expand Down Expand Up @@ -911,7 +908,8 @@ private static function validateSingleParam(
TypeValidatorRegistry $registry,
array $classTemplates = [],
array $vars = [],
object|string|null $thisOrClass = null
object|string|null $thisOrClass = null,
bool $isSensitive = false
): ?ErrorMessage {
if (
$typeNode instanceof ConditionalTypeForParameterNode ||
Expand Down Expand Up @@ -941,14 +939,14 @@ private static function validateSingleParam(
}

if ($typeNode instanceof GenericTypeNode && self::isClassStringTemplate($typeNode, $templates)) {
return self::resolveClassStringTemplate($typeNode, $val, $paramName, $effectiveFunction, $thisObj, $templates, $classTemplates);
return self::resolveClassStringTemplate($typeNode, $val, $paramName, $effectiveFunction, $thisObj, $templates, $classTemplates, $isSensitive);
}

if (self::getTemplateName($typeNode, $templates) !== null) {
return self::resolveTemplateParam($typeNode, $val, $paramName, $effectiveFunction, $thisObj, $templates, $registry, $classTemplates, $thisOrClass);
}

return $registry->validate($val, $typeNode, $effectiveFunction . '(): Argument $' . $paramName);
return $registry->validate($val, $typeNode, $effectiveFunction . '(): Argument $' . $paramName, $isSensitive);
}

/**
Expand Down Expand Up @@ -1078,7 +1076,8 @@ private static function resolveClassStringTemplate(
string $function,
?object $thisObj,
array $templates,
array $classTemplates = []
array $classTemplates = [],
bool $isSensitive = false
): ?ErrorMessage {
/** @var IdentifierTypeNode $innerType */
$innerType = $typeNode->genericTypes[0];
Expand All @@ -1089,7 +1088,7 @@ private static function resolveClassStringTemplate(

if (! TemplateManager::isBound($function, $targetObj, $templateName)) {
if (! \is_string($val) || ! ClassNameValidator::isValidClassString($val)) {
return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' must be a valid class-string, ' . TypeFormatter::formatGivenValue($val) . ' given');
return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' must be a valid class-string, ' . TypeFormatter::formatGivenValue($val, $isSensitive) . ' given');
}

if ($templateNode->bound !== null) {
Expand All @@ -1098,8 +1097,9 @@ private static function resolveClassStringTemplate(
$resolvedBound = SpecialTypeResolver::resolve($templateNode->bound, $function, $thisObj);
if (! self::checkClassStringSatisfiesBound($val, $resolvedBound)) {
$boundDisplay = (string) $resolvedBound;
$displayVal = $isSensitive ? 'string given' : "'" . $val . "' given";

return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' (class-string<' . $templateName . '>) must be a class-string of ' . $boundDisplay . ", '" . $val . "' given");
return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' (class-string<' . $templateName . '>) must be a class-string of ' . $boundDisplay . ', ' . $displayVal);
}
}
}
Expand All @@ -1110,7 +1110,7 @@ private static function resolveClassStringTemplate(
$expectedTypeNode = TemplateManager::getBoundType($function, $targetObj, $templateName);
if ($expectedTypeNode !== null) {
if (! \is_string($val) || ! self::checkClassStringSatisfiesBound($val, $expectedTypeNode)) {
$valStr = TypeFormatter::formatGivenValue($val);
$valStr = TypeFormatter::formatGivenValue($val, $isSensitive);
$targetDisplay = (string) $expectedTypeNode;

return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' must be a class-string of ' . $targetDisplay . ', ' . $valStr . ' given');
Expand Down
35 changes: 4 additions & 31 deletions src/Internal/Checker/ReturnChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use TypePHP\Internal\Wrapper\CallableWrapper;

/**
* @phpstan-import-type FunctionContract from DocblockParser
*
* @internal Evaluates function and method return contract validations (including dynamic @method calls via __call / __callStatic).
*/
final class ReturnChecker
Expand Down Expand Up @@ -81,23 +83,7 @@ public static function isReturnUnconstrained(string $effectiveFunction): bool

/**
* @param array<string, mixed> $vars
* @param array{
* types: array<string, TypeNode>,
* templates: array<string, TemplateTagValueNode>,
* classTemplates: array<string, TemplateTagValueNode>,
* return: ?TypeNode,
* aliases: array<string, TypeNode>,
* hasParamContract: bool,
* hasReturnContract: bool,
* paramsUseGenerics: bool,
* returnUsesGenerics: bool,
* returnUsesMethodTemplates: bool,
* returnIsThis: bool,
* returnIsDynamic: bool,
* allParamsUnconstrained: bool,
* returnUnconstrained: bool,
* isSimple: bool
* }|null $contract Pre-resolved contract to avoid re-parsing
* @param FunctionContract|null $contract Pre-resolved contract to avoid re-parsing
*/
public static function checkReturn(
string $function,
Expand Down Expand Up @@ -145,7 +131,6 @@ public static function checkReturn(
return $value;
}

// Use pre-resolved contract or parse
$contract ??= DocblockParser::parse($effectiveFunction);

if (! ($contract['hasReturnContract'] ?? ($contract['return'] !== null))) {
Expand Down Expand Up @@ -236,18 +221,7 @@ private static function handleMagicReturn(
* @param array<int|string, mixed> $vars
* @param array<string, TypeNode> $aliases
* @param array<string, TemplateTagValueNode> $templates
* @param array{
* return?: TypeNode|null,
* hasReturnContract?: bool,
* returnIsThis?: bool,
* returnIsDynamic?: bool,
* aliases?: array<string, TypeNode>,
* templates?: array<string, TemplateTagValueNode>,
* classTemplates?: array<string, TemplateTagValueNode>,
* allParamsUnconstrained?: bool,
* returnUnconstrained?: bool,
* isSimple?: bool
* } $contract
* @param FunctionContract|array{} $contract
*/
private static function evaluateReturn(
TypeNode $returnTypeNode,
Expand Down Expand Up @@ -348,7 +322,6 @@ private static function evaluateReturn(
}
}

// Fast-path: if return type is mixed or array, skip validation.
if ($resolvedType instanceof IdentifierTypeNode) {
$lower = strtolower($resolvedType->name);
if ($lower === 'mixed' || $lower === 'array') {
Expand Down
6 changes: 5 additions & 1 deletion src/Internal/Diagnostic/TypeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
*/
final class TypeFormatter
{
public static function formatGivenValue(mixed $value): string
public static function formatGivenValue(mixed $value, bool $isSensitive = false): string
{
if ($isSensitive) {
return get_debug_type($value);
}

if (\is_int($value)) {
if ($value < 0) {
return "negative int ($value)";
Expand Down
Loading
Loading