From 7ea7715bd34e7961991d4b7cdabbf38977407c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20M=C3=AD=C5=A1ek?= Date: Sun, 2 Jun 2024 12:55:09 +0200 Subject: [PATCH] nullref warning fixes --- .../Reflection/PhpPropertyInfo.cs | 16 ++++++++++++---- .../Reflection/PhpRoutineInfo.cs | 10 ++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Peachpie.Runtime/Reflection/PhpPropertyInfo.cs b/src/Peachpie.Runtime/Reflection/PhpPropertyInfo.cs index 38beda336a..a2432219e4 100644 --- a/src/Peachpie.Runtime/Reflection/PhpPropertyInfo.cs +++ b/src/Peachpie.Runtime/Reflection/PhpPropertyInfo.cs @@ -275,8 +275,9 @@ public override FieldAttributes Attributes { get { + var getter = Property.GetMethod ?? throw new InvalidOperationException($"getter is null"); var flags = (FieldAttributes)0; - var attr = Property.GetMethod.Attributes; + var attr = getter.Attributes; switch (attr & MethodAttributes.MemberAccessMask) { @@ -313,9 +314,16 @@ public override FieldAttributes Attributes public override Expression Bind(Expression ctx, Expression target) { - return Expression.Property( - Property.GetMethod.IsStatic ? null : target, - Property); + var getter = Property.GetMethod; + if (getter != null) + { + return Expression.Property( + getter.IsStatic ? null : target, + Property + ); + } + + throw new InvalidOperationException($"Property '{ContainingType.Name}::{Property.Name}' does not have a getter."); } } diff --git a/src/Peachpie.Runtime/Reflection/PhpRoutineInfo.cs b/src/Peachpie.Runtime/Reflection/PhpRoutineInfo.cs index 94b92d0f98..3d212114b7 100644 --- a/src/Peachpie.Runtime/Reflection/PhpRoutineInfo.cs +++ b/src/Peachpie.Runtime/Reflection/PhpRoutineInfo.cs @@ -120,7 +120,7 @@ internal class PhpRoutineInfo : RoutineInfo public override int GetHashCode() => _handle.GetHashCode(); - public override MethodInfo[] Methods => new[] { (MethodInfo)MethodBase.GetMethodFromHandle(_handle) }; + public override MethodInfo[] Methods => new[] { GetMethodFromHandle(_handle) }; public override PhpTypeInfo? DeclaringType => null; @@ -128,6 +128,8 @@ internal class PhpRoutineInfo : RoutineInfo PhpCallable BindDelegate() => _lazyDelegate = Dynamic.BinderHelpers.BindToPhpCallable(Methods); + private static MethodInfo/*!*/GetMethodFromHandle(RuntimeMethodHandle handle) => (MethodInfo)(MethodBase.GetMethodFromHandle(handle) ?? throw new InvalidOperationException()); + internal static PhpRoutineInfo Create(string name, RuntimeMethodHandle handle, params RuntimeMethodHandle[] overloads) { return (overloads.Length == 0) @@ -162,10 +164,10 @@ public override MethodInfo[] Methods // [Method] + Overloads var methods = new MethodInfo[1 + overloads.Length]; - methods[0] = (MethodInfo)MethodBase.GetMethodFromHandle(Handle); + methods[0] = GetMethodFromHandle(Handle); for (int i = 0; i < overloads.Length; i++) { - methods[1 + i] = (MethodInfo)MethodBase.GetMethodFromHandle(overloads[i]); + methods[1 + i] = GetMethodFromHandle(overloads[i]); } // @@ -203,7 +205,7 @@ internal class DelegateRoutineInfo : RoutineInfo public override PhpTypeInfo? DeclaringType => null; - internal override object Target => _delegate.Target; + internal override object? Target => _delegate.Target; public override int GetHashCode() => _delegate.GetHashCode();