3030use TypePHP \Internal \Validator \TypeValidatorRegistry ;
3131
3232/**
33+ * @phpstan-import-type FunctionContract from DocblockParser
34+ *
3335 * @internal Evaluates function and method parameter contract validations (including dynamic @method calls via __call / __callStatic).
3436 */
3537final class ParamChecker
@@ -102,23 +104,7 @@ private static function isUnconstrained(TypeNode $typeNode): bool
102104
103105 /**
104106 * @param array<string, mixed> $vars
105- * @param array{
106- * types: array<string, TypeNode>,
107- * templates: array<string, TemplateTagValueNode>,
108- * classTemplates: array<string, TemplateTagValueNode>,
109- * return: ?TypeNode,
110- * aliases: array<string, TypeNode>,
111- * hasParamContract: bool,
112- * hasReturnContract: bool,
113- * paramsUseGenerics: bool,
114- * returnUsesGenerics: bool,
115- * returnUsesMethodTemplates: bool,
116- * returnIsThis: bool,
117- * returnIsDynamic: bool,
118- * allParamsUnconstrained: bool,
119- * returnUnconstrained: bool,
120- * isSimple: bool
121- * }|null $contract Pre-resolved contract to avoid re-parsing
107+ * @param FunctionContract|null $contract Pre-resolved contract to avoid re-parsing
122108 */
123109 public static function checkParams (
124110 string $ function ,
@@ -170,9 +156,10 @@ public static function checkParams(
170156 $ classTemplates = $ contract ['classTemplates ' ] ?? [];
171157 $ aliases = $ contract ['aliases ' ];
172158 $ hasMethodTemplates = (\count ($ methodTemplates ) > 0 );
159+ $ sensitiveParams = $ contract ['sensitiveParams ' ] ?? [];
173160
174161 if (! $ paramsUseGenerics && ! $ hasMethodTemplates && \count ($ aliases ) === 0 ) {
175- return self ::validateSimpleParams ($ contract ['types ' ], $ vars , $ effectiveFunction , $ registry );
162+ return self ::validateSimpleParams ($ contract ['types ' ], $ vars , $ effectiveFunction , $ registry, $ sensitiveParams );
176163 }
177164
178165 self ::prepareGenericBindings ($ effectiveFunction , $ methodTemplates , $ thisObj , $ classTemplates , $ thisOrClass );
@@ -203,7 +190,8 @@ public static function checkParams(
203190 $ declaredTemplates ,
204191 $ registry ,
205192 $ classTemplates ,
206- $ thisOrClass
193+ $ thisOrClass ,
194+ $ sensitiveParams
207195 );
208196 }
209197
@@ -212,12 +200,14 @@ public static function checkParams(
212200 *
213201 * @param array<string, TypeNode> $types
214202 * @param array<string, mixed> $vars
203+ * @param array<string, bool> $sensitiveParams
215204 */
216205 private static function validateSimpleParams (
217206 array $ types ,
218207 array $ vars ,
219208 string $ effectiveFunction ,
220- TypeValidatorRegistry $ registry
209+ TypeValidatorRegistry $ registry ,
210+ array $ sensitiveParams = []
221211 ): ?ErrorMessage {
222212 foreach ($ types as $ paramName => $ typeNode ) {
223213 if (isset ($ vars [$ paramName ]) || \array_key_exists ($ paramName , $ vars )) {
@@ -231,7 +221,9 @@ private static function validateSimpleParams(
231221 if (self ::isUnconstrained ($ typeNode )) {
232222 continue ;
233223 }
234- $ err = $ registry ->validate ($ vars [$ paramName ], $ typeNode , '' );
224+
225+ $ isSensitive = $ sensitiveParams [$ paramName ] ?? false ;
226+ $ err = $ registry ->validate ($ vars [$ paramName ], $ typeNode , '' , $ isSensitive );
235227 if ($ err !== null ) {
236228 return ErrorFactory ::createError ($ effectiveFunction . '(): Argument $ ' . $ paramName . $ err ->getMessage ());
237229 }
@@ -310,6 +302,7 @@ private static function resolveBaseTypes(
310302 * @param array<string, TypeNode> $boundTemplates
311303 * @param array<string, TemplateTagValueNode> $declaredTemplates
312304 * @param array<string, TemplateTagValueNode> $classTemplates
305+ * @param array<string, bool> $sensitiveParams
313306 */
314307 private static function validateAllParameters (
315308 array $ contractTypes ,
@@ -323,7 +316,8 @@ private static function validateAllParameters(
323316 array $ declaredTemplates ,
324317 TypeValidatorRegistry $ registry ,
325318 array $ classTemplates ,
326- object |string |null $ thisOrClass = null
319+ object |string |null $ thisOrClass = null ,
320+ array $ sensitiveParams = []
327321 ): ?ErrorMessage {
328322 foreach ($ contractTypes as $ paramName => $ _ ) {
329323 if (! isset ($ vars [$ paramName ]) && ! \array_key_exists ($ paramName , $ vars )) {
@@ -334,6 +328,8 @@ private static function validateAllParameters(
334328 ? TemplateManager ::getBoundTemplates ($ effectiveFunction , $ thisOrClass , $ allTemplates )
335329 : $ boundTemplates ;
336330
331+ $ isSensitive = $ sensitiveParams [$ paramName ] ?? false ;
332+
337333 $ err = self ::validateSingleParam (
338334 $ paramName ,
339335 $ baseTypes [$ paramName ],
@@ -347,7 +343,8 @@ private static function validateAllParameters(
347343 $ registry ,
348344 $ classTemplates ,
349345 $ vars ,
350- $ thisOrClass
346+ $ thisOrClass ,
347+ $ isSensitive
351348 );
352349
353350 if ($ err !== null ) {
@@ -911,7 +908,8 @@ private static function validateSingleParam(
911908 TypeValidatorRegistry $ registry ,
912909 array $ classTemplates = [],
913910 array $ vars = [],
914- object |string |null $ thisOrClass = null
911+ object |string |null $ thisOrClass = null ,
912+ bool $ isSensitive = false
915913 ): ?ErrorMessage {
916914 if (
917915 $ typeNode instanceof ConditionalTypeForParameterNode ||
@@ -941,14 +939,14 @@ private static function validateSingleParam(
941939 }
942940
943941 if ($ typeNode instanceof GenericTypeNode && self ::isClassStringTemplate ($ typeNode , $ templates )) {
944- return self ::resolveClassStringTemplate ($ typeNode , $ val , $ paramName , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates );
942+ return self ::resolveClassStringTemplate ($ typeNode , $ val , $ paramName , $ effectiveFunction , $ thisObj , $ templates , $ classTemplates, $ isSensitive );
945943 }
946944
947945 if (self ::getTemplateName ($ typeNode , $ templates ) !== null ) {
948946 return self ::resolveTemplateParam ($ typeNode , $ val , $ paramName , $ effectiveFunction , $ thisObj , $ templates , $ registry , $ classTemplates , $ thisOrClass );
949947 }
950948
951- return $ registry ->validate ($ val , $ typeNode , $ effectiveFunction . '(): Argument $ ' . $ paramName );
949+ return $ registry ->validate ($ val , $ typeNode , $ effectiveFunction . '(): Argument $ ' . $ paramName, $ isSensitive );
952950 }
953951
954952 /**
@@ -1078,7 +1076,8 @@ private static function resolveClassStringTemplate(
10781076 string $ function ,
10791077 ?object $ thisObj ,
10801078 array $ templates ,
1081- array $ classTemplates = []
1079+ array $ classTemplates = [],
1080+ bool $ isSensitive = false
10821081 ): ?ErrorMessage {
10831082 /** @var IdentifierTypeNode $innerType */
10841083 $ innerType = $ typeNode ->genericTypes [0 ];
@@ -1089,7 +1088,7 @@ private static function resolveClassStringTemplate(
10891088
10901089 if (! TemplateManager ::isBound ($ function , $ targetObj , $ templateName )) {
10911090 if (! \is_string ($ val ) || ! ClassNameValidator ::isValidClassString ($ val )) {
1092- return ErrorFactory ::createError ($ function . '(): Argument $ ' . $ paramName . ' must be a valid class-string, ' . TypeFormatter ::formatGivenValue ($ val ) . ' given ' );
1091+ return ErrorFactory ::createError ($ function . '(): Argument $ ' . $ paramName . ' must be a valid class-string, ' . TypeFormatter ::formatGivenValue ($ val, $ isSensitive ) . ' given ' );
10931092 }
10941093
10951094 if ($ templateNode ->bound !== null ) {
@@ -1098,8 +1097,9 @@ private static function resolveClassStringTemplate(
10981097 $ resolvedBound = SpecialTypeResolver ::resolve ($ templateNode ->bound , $ function , $ thisObj );
10991098 if (! self ::checkClassStringSatisfiesBound ($ val , $ resolvedBound )) {
11001099 $ boundDisplay = (string ) $ resolvedBound ;
1100+ $ displayVal = $ isSensitive ? 'string given ' : "' " . $ val . "' given " ;
11011101
1102- return ErrorFactory ::createError ($ function . '(): Argument $ ' . $ paramName . ' (class-string< ' . $ templateName . '>) must be a class-string of ' . $ boundDisplay . " , ' " . $ val . " ' given " );
1102+ return ErrorFactory ::createError ($ function . '(): Argument $ ' . $ paramName . ' (class-string< ' . $ templateName . '>) must be a class-string of ' . $ boundDisplay . ' , ' . $ displayVal );
11031103 }
11041104 }
11051105 }
@@ -1110,7 +1110,7 @@ private static function resolveClassStringTemplate(
11101110 $ expectedTypeNode = TemplateManager ::getBoundType ($ function , $ targetObj , $ templateName );
11111111 if ($ expectedTypeNode !== null ) {
11121112 if (! \is_string ($ val ) || ! self ::checkClassStringSatisfiesBound ($ val , $ expectedTypeNode )) {
1113- $ valStr = TypeFormatter ::formatGivenValue ($ val );
1113+ $ valStr = TypeFormatter ::formatGivenValue ($ val, $ isSensitive );
11141114 $ targetDisplay = (string ) $ expectedTypeNode ;
11151115
11161116 return ErrorFactory ::createError ($ function . '(): Argument $ ' . $ paramName . ' must be a class-string of ' . $ targetDisplay . ', ' . $ valStr . ' given ' );
0 commit comments