Browse Source

[php] don't allow methods with the same names in different case (closes #8219)

Aleksandr Kuzmenko 6 years ago
parent
commit
a659d1c5e0

+ 16 - 0
src/generators/genphp7.ml

@@ -3284,6 +3284,11 @@ class enum_builder ctx (enm:tenum) =
 class class_builder ctx (cls:tclass) =
 	object (self)
 		inherit type_builder ctx (get_wrapper (TClassDecl cls)) as super
+		(**
+			List of methods names uppercased.
+			Used to check for methods with similar names because PHP function names are case-insensitive.
+		*)
+		val mutable used_method_names = []
 		(**
 			Indicates if type should be declared as `final`
 		*)
@@ -3611,6 +3616,7 @@ class class_builder ctx (cls:tclass) =
 			Writes method to output buffer
 		*)
 		method private write_class_method field is_static =
+			self#validate_method_name field;
 			writer#reset;
 			writer#indent 1;
 			let (args, return_type) = get_function_signature field in
@@ -3636,6 +3642,7 @@ class class_builder ctx (cls:tclass) =
 			Only for non-static methods. Static methods are created as static vars in `__hx__init`.
 		*)
 		method private write_dynamic_method field =
+			self#validate_method_name field;
 			writer#reset;
 			writer#indent 1;
 			let (args, return_type) = get_function_signature field in
@@ -3665,6 +3672,15 @@ class class_builder ctx (cls:tclass) =
 					writer#write_statement ("protected $__hx__default__" ^ (field_name field))
 				| _ -> fail field.cf_pos __POS__
 			);
+		(**
+			Since PHP function names are case-insensitive we must check for method names clashes.
+		*)
+		method private validate_method_name field =
+			let uppercased_name = StringHelper.uppercase field.cf_name in
+			if List.exists (fun n -> n = uppercased_name) used_method_names then
+				ctx.error ("Methods names are case-insensitive in PHP runtime. Cannot redeclare \"" ^ field.cf_name ^ "\".") field.cf_name_pos
+			else
+				used_method_names <- uppercased_name :: used_method_names
 		(**
 			Writes initialization code for instances of this class
 		*)

+ 8 - 0
tests/misc/projects/Issue8219/Main.hx

@@ -0,0 +1,8 @@
+class Main {
+	@:analyzer(ignore)
+	static function main() {
+		mAin();
+	}
+
+	static function mAin() {}
+}

+ 2 - 0
tests/misc/projects/Issue8219/compile-fail.hxml

@@ -0,0 +1,2 @@
+--main Main
+-php bin

+ 1 - 0
tests/misc/projects/Issue8219/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:3: characters 18-22 : Methods names are case-insensitive in PHP runtime. Cannot redeclare "main".