From 9c9eabae700ac6b7cac20c11c25c3e546b33f527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20M=C3=AD=C5=A1ek?= Date: Thu, 11 Apr 2019 15:50:47 +0200 Subject: [PATCH] IsNullable() helper - reflection to determine properly whether parameter value can be NULL --- .../Reflection/ReflectionUtils.cs | 2 +- .../Reflection/ReflectionUtils.cs | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Peachpie.Library/Reflection/ReflectionUtils.cs b/src/Peachpie.Library/Reflection/ReflectionUtils.cs index 11df12801b..83908131ed 100644 --- a/src/Peachpie.Library/Reflection/ReflectionUtils.cs +++ b/src/Peachpie.Library/Reflection/ReflectionUtils.cs @@ -57,7 +57,7 @@ public static List ResolveReflectionParameters(ReflectionFu { var p = ps[pi]; - var allowsNull = p.GetCustomAttribute() == null; + var allowsNull = p.IsNullable(); var defaultValue = p.HasDefaultValue ? PhpValue.FromClr(p.RawDefaultValue) : default(PhpValue); var isVariadic = p.GetCustomAttribute() != null; diff --git a/src/Peachpie.Runtime/Reflection/ReflectionUtils.cs b/src/Peachpie.Runtime/Reflection/ReflectionUtils.cs index c4aaa58cf7..d16c260d90 100644 --- a/src/Peachpie.Runtime/Reflection/ReflectionUtils.cs +++ b/src/Peachpie.Runtime/Reflection/ReflectionUtils.cs @@ -88,6 +88,31 @@ public static bool IsPhpClassType(TypeInfo tinfo) return !tinfo.IsValueType && t != typeof(PhpArray) && t != typeof(string) && t != typeof(IPhpCallable); } + /// + /// Determines whether given parametr allows NULL as the argument value. + /// + public static bool IsNullable(this ParameterInfo p) + { + if (p.ParameterType.IsValueType && + p.ParameterType != typeof(PhpValue) && + //p.ParameterType != typeof(PhpArray) // TODO: uncomment when PhpArray will be struct + p.ParameterType != typeof(PhpString)) + { + if (p.ParameterType.IsNullable_T(out var _)) + { + return true; + } + + // NULL is not possible on value types + return false; + } + else + { + // NULL is explicitly disallowed? + return p.GetCustomAttribute() == null; + } + } + /// /// Types that we do not expose in reflection. ///