Skip to content

Commit

Permalink
IsNullable() helper
Browse files Browse the repository at this point in the history
- reflection to determine properly whether parameter value can be NULL
  • Loading branch information
jakubmisek committed Apr 11, 2019
1 parent b99bb23 commit 9c9eaba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Peachpie.Library/Reflection/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static List<ReflectionParameter> ResolveReflectionParameters(ReflectionFu
{
var p = ps[pi];

var allowsNull = p.GetCustomAttribute<NotNullAttribute>() == null;
var allowsNull = p.IsNullable();
var defaultValue = p.HasDefaultValue ? PhpValue.FromClr(p.RawDefaultValue) : default(PhpValue);
var isVariadic = p.GetCustomAttribute<ParamArrayAttribute>() != null;

Expand Down
25 changes: 25 additions & 0 deletions src/Peachpie.Runtime/Reflection/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ public static bool IsPhpClassType(TypeInfo tinfo)
return !tinfo.IsValueType && t != typeof(PhpArray) && t != typeof(string) && t != typeof(IPhpCallable);
}

/// <summary>
/// Determines whether given parametr allows <c>NULL</c> as the argument value.
/// </summary>
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<NotNullAttribute>() == null;
}
}

/// <summary>
/// Types that we do not expose in reflection.
/// </summary>
Expand Down

0 comments on commit 9c9eaba

Please # to comment.