/// <summary> /// Reports an error when a variable should be PHP object but it is not. /// </summary> /// <param name="reference">Whether a reference modifier (=&) is used.</param> /// <param name="var">The variable which was misused.</param> /// <exception cref="PhpException"><paramref name="var"/> is <see cref="PhpArray"/> (Warning).</exception> /// <exception cref="PhpException"><paramref name="var"/> is scalar type (Warning).</exception> /// <exception cref="PhpException"><paramref name="var"/> is a string (Warning).</exception> public static void VariableMisusedAsObject(PhpValue var, bool reference) { if (var.IsEmpty) { Throw(PhpError.Notice, ErrResources.empty_used_as_object); } else if (var.IsArray) { Throw(PhpError.Warning, ErrResources.array_used_as_object); } else if (var.TypeCode == PhpTypeCode.String || var.TypeCode == PhpTypeCode.MutableString) { Throw(PhpError.Warning, reference ? ErrResources.string_item_used_as_reference : ErrResources.string_used_as_object); } else if (var.IsAlias) { VariableMisusedAsObject(var.Alias.Value, reference); } else { Throw(PhpError.Warning, ErrResources.scalar_used_as_object, PhpVariable.GetTypeName(var)); } }
public static int Compare(double dx, PhpValue y) { switch (y.TypeCode) { case PhpTypeCode.Double: return(Compare(dx, y.Double)); case PhpTypeCode.Long: return(Compare(dx, (double)y.Long)); case PhpTypeCode.Boolean: return(Compare(dx != 0.0, y.Boolean)); case PhpTypeCode.String: return(-Compare(y.String, dx)); case PhpTypeCode.MutableString: return(-Compare(y.MutableString.ToString(), dx)); case PhpTypeCode.PhpArray: return(-1); case PhpTypeCode.Alias: return(Compare(dx, y.Alias.Value)); case PhpTypeCode.Null: return((dx == 0.0) ? 0 : 1); case PhpTypeCode.Object: if (y.Object == null) { goto case PhpTypeCode.Null; } // Notice: Object of class {0} could not be converted to int PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetTypeName(y), PhpVariable.TypeNameDouble)); return(Compare(dx, 1.0)); // object is treated as '1' } throw new NotImplementedException($"compare(Double, {y.TypeCode})"); }
public static int Compare(long lx, PhpValue y) { switch (y.TypeCode) { case PhpTypeCode.Long: return(Compare(lx, y.Long)); case PhpTypeCode.Boolean: return(Compare(lx != 0, y.Boolean)); case PhpTypeCode.Double: return(Compare((double)lx, y.Double)); case PhpTypeCode.String: return(-Compare(y.String, lx)); case PhpTypeCode.MutableString: return(-Compare(y.MutableString.ToString(), lx)); case PhpTypeCode.PhpArray: return(-1); case PhpTypeCode.Alias: return(Compare(lx, y.Alias.Value)); case PhpTypeCode.Null: return((lx == 0) ? 0 : 1); case PhpTypeCode.Object: Debug.Assert(y.Object != null); // Notice: Object of class {0} could not be converted to int PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetTypeName(y), PhpVariable.TypeNameInt)); return(Compare(lx, 1L)); // object is treated as '1' } throw new NotImplementedException($"compare(Long, {y.TypeCode})"); }
public static void ThrowIfArgumentNotCallable(Context ctx, RuntimeTypeHandle callerCtx, PhpValue value, bool nullAllowed, int arg) { if (nullAllowed && value.IsNull) { return; } var callable = value.AsCallable(callerCtx); if (callable == null || (callable is PhpCallback phpcallback && !phpcallback.IsValidBound(ctx))) { throw TypeErrorException(string.Format(ErrResources.argument_not_callable, arg, PhpVariable.GetTypeName(value))); } }