]> granicus.if.org Git - php/commitdiff
Canonicalize typed properties error message
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 23 Sep 2019 13:48:49 +0000 (15:48 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 23 Sep 2019 13:48:49 +0000 (15:48 +0200)
We have lots of other typed properties related error messages of
the form "assign X to typed property Y::$z of type A", so use th
same format for the primary message as well. Special-casing things
like classes and nullability is not going to scale with future
type-system extensions, and I don't think it really adds clarity
either.

26 files changed:
Zend/tests/type_declarations/typed_properties_004.phpt
Zend/tests/type_declarations/typed_properties_005.phpt
Zend/tests/type_declarations/typed_properties_020.phpt
Zend/tests/type_declarations/typed_properties_029.phpt
Zend/tests/type_declarations/typed_properties_030.phpt
Zend/tests/type_declarations/typed_properties_034.phpt
Zend/tests/type_declarations/typed_properties_038.phpt
Zend/tests/type_declarations/typed_properties_039.phpt
Zend/tests/type_declarations/typed_properties_040.phpt
Zend/tests/type_declarations/typed_properties_046.phpt
Zend/tests/type_declarations/typed_properties_047.phpt
Zend/tests/type_declarations/typed_properties_051.phpt
Zend/tests/type_declarations/typed_properties_056.phpt
Zend/tests/type_declarations/typed_properties_057.phpt
Zend/tests/type_declarations/typed_properties_058.phpt
Zend/tests/type_declarations/typed_properties_060.phpt
Zend/tests/type_declarations/typed_properties_068.phpt
Zend/tests/type_declarations/typed_properties_069.phpt
Zend/tests/type_declarations/typed_properties_070.phpt
Zend/tests/type_declarations/typed_properties_074.phpt
Zend/tests/type_declarations/typed_properties_075.phpt
Zend/tests/type_declarations/typed_properties_078.phpt
Zend/tests/type_declarations/typed_properties_093.phpt
Zend/tests/type_declarations/typed_properties_095.phpt
Zend/tests/type_declarations/typed_properties_096.phpt
Zend/zend_execute.c

index 03d9e41aac4739732252068ea5b7a904683a45ff..2df693aa997c8a6a1c7d22e23ededa6e810f928b 100644 (file)
@@ -11,8 +11,8 @@ new class("PHP 7 is better than you, and it knows it ...") {
 };
 ?>
 --EXPECTF--
-Fatal error: Uncaught TypeError: Typed property class@anonymous::$int must be int, string used in %s:6
+Fatal error: Uncaught TypeError: Cannot assign string to property class@anonymous::$int of type int in %s:%d
 Stack trace:
-#0 %s(2): class@anonymous->__construct('PHP 7 is better...')
+#0 %s(%d): class@anonymous->__construct('PHP 7 is better...')
 #1 {main}
-  thrown in %s on line 6
+  thrown in %s on line %d
index b521963000d52ea1765d5c54b1f09c5f8f21eb4c..104f4bd57ee3e1865c107bd5b55a005c23b6acc6 100644 (file)
@@ -13,8 +13,8 @@ new class(new Dummy) {
 };
 ?>
 --EXPECTF--
-Fatal error: Uncaught TypeError: Typed property class@anonymous::$std must be an instance of stdClass, Dummy used in %s:8
+Fatal error: Uncaught TypeError: Cannot assign Dummy to property class@anonymous::$std of type stdClass in %s:%d
 Stack trace:
-#0 %s(4): class@anonymous->__construct(Object(Dummy))
+#0 %s(%d): class@anonymous->__construct(Object(Dummy))
 #1 {main}
-  thrown in %s on line 8
+  thrown in %s on line %d
index d32d17b1c8d4a403903762686c6e6ebf6626cfcb..3b16a9d81d0ea61bea222a0e30f8117c72ce4f1e 100644 (file)
@@ -22,5 +22,5 @@ $foo = new Foo();
 var_dump($foo->bar);
 ?>
 --EXPECT--
-Typed property Foo::$bar must be int, float used
+Cannot assign float to property Foo::$bar of type int
 int(2)
index a5418c91a85c90766506748d28eab0f865c49315..9beca106279ac3daea9892119b4098937b379a8d 100644 (file)
@@ -12,7 +12,7 @@ $foo = new Foo;
 $foo->bar = "1";
 ?>
 --EXPECTF--
-Fatal error: Uncaught TypeError: Typed property Foo::$bar must be int, string used in %s:9
+Fatal error: Uncaught TypeError: Cannot assign string to property Foo::$bar of type int in %s:%d
 Stack trace:
 #0 {main}
-  thrown in %s on line 9
+  thrown in %s on line %d
index a835ae5fae790ddc029f945ac909d948a5b95e04..8feda02316d22adac61c911b22141887e6940c43 100644 (file)
@@ -19,7 +19,7 @@ unset($foo->bar); # ok
 var_dump($foo->bar); # not okay, __get is nasty
 ?>
 --EXPECTF--
-Fatal error: Uncaught TypeError: Typed property Foo::$bar must be int, string used in %s:16
+Fatal error: Uncaught TypeError: Cannot assign string to property Foo::$bar of type int in %s:%d
 Stack trace:
 #0 {main}
-  thrown in %s on line 16
+  thrown in %s on line %d
index c3885af39f8d4c9ace2d7827eabe0fdced108dab..79317553ab861bfd42bdd3b47c975793f31969b1 100644 (file)
@@ -38,7 +38,7 @@ var_dump($foo);
 ?>
 --EXPECT--
 int(42)
-Typed property class@anonymous::$baz must be int, null used
+Cannot assign null to property class@anonymous::$baz of type int
 int(1)
 int(10)
 int(10)
index 44e5d04c7fd46e9e23ca7c822e994d7ed6a0c19e..94a00d0d8cd8f6be761b1fd64f92d98c6695be3b 100644 (file)
@@ -44,7 +44,7 @@ object(class@anonymous)#1 (1) {
   ["bar"]=>
   int(%d)
 }
-string(60) "Typed property class@anonymous::$bar must be int, float used"
+string(65) "Cannot assign float to property class@anonymous::$bar of type int"
 object(class@anonymous)#1 (1) {
   ["bar"]=>
   int(%d)
@@ -54,7 +54,7 @@ object(class@anonymous)#1 (1) {
   ["bar"]=>
   int(%d)
 }
-string(60) "Typed property class@anonymous::$bar must be int, float used"
+string(65) "Cannot assign float to property class@anonymous::$bar of type int"
 object(class@anonymous)#1 (1) {
   ["bar"]=>
   int(%d)
index 62413d804c19c0dc36ad55d1e1e35f338560afe5..47ae17f0ab82c46a09aaf1cd11e0c4cd3419dd2c 100644 (file)
@@ -25,7 +25,7 @@ var_dump($objs);
 
 ?>
 --EXPECTF--
-Fatal error: Uncaught TypeError: Typed property A::$foo must be int, A used in %s:%d
+Fatal error: Uncaught TypeError: Cannot assign A to property A::$foo of type int in %s:%d
 Stack trace:
 #0 {main}
   thrown in %s on line %d
index db2b90903c73683f2f08db4d300bc0ff0c337116..0b47c92a872f946f77b84518287b34f9c6b24586 100644 (file)
@@ -19,7 +19,7 @@ var_dump($foo->bar);
 --EXPECTF--
 string(3) "bar"
 
-Fatal error: Uncaught TypeError: Typed property Foo::$bar must be int, null used in %s:14
+Fatal error: Uncaught TypeError: Cannot assign null to property Foo::$bar of type int in %s:%d
 Stack trace:
 #0 {main}
-  thrown in %s on line 14
+  thrown in %s on line %d
index 050e64a2a4ed433ef86d876f9783e95d5dd6b96f..d95fe8dc24794f67660deeb89abc2a626dd01fef 100644 (file)
@@ -22,8 +22,8 @@ for ($i = 0; $i < 5; $i++) {
        }
 }
 --EXPECT--
-Typed property Foo::$bbb must be int, string used
-Typed property Foo::$bbb must be int, string used
-Typed property Foo::$bbb must be int, string used
-Typed property Foo::$bbb must be int, string used
-Typed property Foo::$bbb must be int, string used
+Cannot assign string to property Foo::$bbb of type int
+Cannot assign string to property Foo::$bbb of type int
+Cannot assign string to property Foo::$bbb of type int
+Cannot assign string to property Foo::$bbb of type int
+Cannot assign string to property Foo::$bbb of type int
index 0b48cd1cd4554fcc7192cbd0fdca53f15e89d433..db78143b02eb2b94e7c9fe6bc17c818001610a47 100644 (file)
@@ -37,4 +37,4 @@ NULL
 int(5)
 NULL
 Typed property Foo::$foo must not be accessed before initialization
-Typed property Foo::$foo must be int or null, string used
+Cannot assign string to property Foo::$foo of type ?int
index 1c8673d0d3d44188b270ad03353d68fe1ef58330..977570b2385c8f2dc26372ab586513bd34a46caf 100644 (file)
@@ -24,4 +24,4 @@ try {
 ?>
 --EXPECT--
 string(4) "okok"
-Typed property A::$a must be string, C used
+Cannot assign C to property A::$a of type string
index 54013f44f2295679cefff037c71a4db3454046fb..4d2930cc3bca9db907ac2a84d1c429459d83573d 100644 (file)
@@ -19,5 +19,5 @@ var_dump($o->foo);
 unset($o);
 ?>
 --EXPECT--
-Typed property A::$foo must be string, int used
+Cannot assign int to property A::$foo of type string
 string(3) "100"
index 6b53a42600a67194dc339940aed01d5230fab482..ebec0f48bfae6ba457b4e3e4895f97a6361242d5 100644 (file)
@@ -25,7 +25,7 @@ var_dump($o->foo);
 unset($o);
 ?>
 --EXPECT--
-Typed property A::$foo must be string, int used
+Cannot assign int to property A::$foo of type string
 string(3) "100"
-Typed property A::$foo must be string, int used
+Cannot assign int to property A::$foo of type string
 string(3) "100"
index d9283bdbef641010791885f10e341ac4898caa9e..a6d65daa4c6e228f2aa6c6164421413e669e7558 100644 (file)
@@ -28,5 +28,5 @@ for ($i = 0; $i < 2; $i++) {
 ?>
 --EXPECT--
 int(5)
-Typed property B::$foo must be string, int used
-Typed property B::$foo must be string, int used
+Cannot assign int to property B::$foo of type string
+Cannot assign int to property B::$foo of type string
index 559349b7a97b17d1877adfa354e244d3075dbcb5..5b3c9d098190d2a7548588a2416fc9848ec6da41 100644 (file)
@@ -16,7 +16,7 @@ $o->a = "a";
 --EXPECTF--
 int(1)
 
-Fatal error: Uncaught TypeError: Typed property A::$a must be int, string used in %s:%d
+Fatal error: Uncaught TypeError: Cannot assign string to property A::$a of type int in %s:%d
 Stack trace:
 #0 {main}
   thrown in %s on line %d
index 7ed58d2a00893db941ff1d4732bcf444d4cbe230..dc5cb1b5ffe9f1ce98b6d065b9156166819619ce 100644 (file)
@@ -69,14 +69,14 @@ int(4)
 Cannot assign null to reference held by property Foo::$i of type int
 int(4)
 int(4)
-Typed property Foo::$i must be int, null used
+Cannot assign null to property Foo::$i of type int
 int(4)
 int(4)
 string(1) "5"
 string(1) "5"
 int(0)
 int(0)
-Typed property Foo::$i must be int, string used
+Cannot assign string to property Foo::$i of type int
 int(0)
 string(1) "x"
 Reference with value of type string held by property Foo::$s of type string is not compatible with property Foo::$i of type int
index 77cfc02487887cacf17ae750928830f9a2d9a704..0f804f8f06f6ca04252dc24e32b8ed10b452f1e7 100644 (file)
@@ -22,6 +22,6 @@ var_dump(nonNumericStringRef());
 
 ?>
 --EXPECT--
-Typed property Foo::$i must be int, string used
+Cannot assign string to property Foo::$i of type int
 Typed static property Foo::$i must not be accessed before initialization
 string(1) "x"
index dd77c68355e71a525e3aa58de35df243d2ce2691..ec2d6bf8c69f5e4c90bcb9361930d0ae023606cc 100644 (file)
@@ -43,7 +43,7 @@ string(2) "11"
 string(2) "13"
 string(2) "12"
 int(1)
-Typed property Foo::$i must be int, float used
+Cannot assign float to property Foo::$i of type int
 int(1)
-Typed property Foo::$i must be int, string used
+Cannot assign string to property Foo::$i of type int
 int(1)
index 8e6be312c6d89192bc2e70c4ae52313f8a3d4f3d..5a84435550e45e042d8a693deb409622d358d57b 100644 (file)
@@ -31,7 +31,7 @@ object(Test)#1 (1) {
   ["val"]=>
   uninitialized(int)
 }
-Typed property Test::$val must be int, string used
+Cannot assign string to property Test::$val of type int
 object(Test)#1 (1) {
   ["prop"]=>
   &string(1) "x"
index 5f3943cbee2b96c6a666b1159af451283212fb0e..def86f369516ac6ef94c528d3d04d05ced0cce18 100644 (file)
@@ -45,9 +45,9 @@ var_dump(Foo::$bar);
 --EXPECT--
 string(70) "Cannot increment property Foo::$bar of type int past its maximal value"
 int(9223372036854775807)
-string(48) "Typed property Foo::$bar must be int, float used"
+string(53) "Cannot assign float to property Foo::$bar of type int"
 int(9223372036854775807)
 string(70) "Cannot increment property Foo::$bar of type int past its maximal value"
 int(9223372036854775807)
-string(48) "Typed property Foo::$bar must be int, float used"
+string(53) "Cannot assign float to property Foo::$bar of type int"
 int(9223372036854775807)
index 6645970dd412154a16c81228e76b8d56bb53c416..d8fc67d466f74e33bbe8d485828001b7fff429b8 100644 (file)
@@ -45,7 +45,7 @@ var_dump($ref instanceof ArrayIterator);
 --EXPECT--
 array(0) {
 }
-string(89) "Typed property class@anonymous::$t must be an instance of Traversable or null, array used"
+string(72) "Cannot assign array to property class@anonymous::$t of type ?Traversable"
 array(0) {
 }
 array(1) {
index f0c7ef51a9009240c1c64047edcfb4775a427bbb..1f4bf4cb8caee90e8ba599dfb2899a1b05d92b0b 100644 (file)
@@ -24,7 +24,7 @@ var_dump($test);
 
 ?>
 --EXPECT--
-Typed property Test::$prop must be int, string used
+Cannot assign string to property Test::$prop of type int
 object(Test)#2 (0) {
   ["prop"]=>
   uninitialized(int)
index edbef8dfc75ac2ff2f04ce0f03df37050db3bd72..3f1027f08f8b3afb2929e4987765fdb08aa19336 100644 (file)
@@ -60,8 +60,8 @@ var_dump(_ZendTestClass::$staticIntProp);
 ?>
 --EXPECT--
 int(123)
-Typed property _ZendTestClass::$intProp must be int, string used
-Typed property _ZendTestClass::$classProp must be an instance of stdClass or null, _ZendTestClass used
+Cannot assign string to property _ZendTestClass::$intProp of type int
+Cannot assign _ZendTestClass to property _ZendTestClass::$classProp of type ?stdClass
 object(_ZendTestClass)#1 (2) {
   ["intProp"]=>
   int(456)
@@ -70,8 +70,8 @@ object(_ZendTestClass)#1 (2) {
   }
 }
 int(123)
-Typed property _ZendTestClass::$intProp must be int, string used
-Typed property _ZendTestClass::$classProp must be an instance of stdClass or null, Test used
+Cannot assign string to property _ZendTestClass::$intProp of type int
+Cannot assign Test to property _ZendTestClass::$classProp of type ?stdClass
 object(Test)#4 (2) {
   ["intProp"]=>
   int(456)
@@ -80,5 +80,5 @@ object(Test)#4 (2) {
   }
 }
 int(123)
-Typed property _ZendTestClass::$staticIntProp must be int, string used
+Cannot assign string to property _ZendTestClass::$staticIntProp of type int
 int(456)
index 32191b241e90db05693d3b27477c242601051265..83f086d345a536955fd594f81d34a02c93b7c636 100644 (file)
@@ -31,7 +31,7 @@ var_dump($test);
 
 ?>
 --EXPECT--
-Typed property Test1::$prop must be an instance of Foobar, int used
+Cannot assign int to property Test1::$prop of type Foobar
 object(Test1)#1 (1) {
   ["prop"]=>
   uninitialized(Foobar)
index cf32ba55aca743c17a67b4d281706dd9ef830595..1534222e5ea5ba06bfcceff2a97fc133fbaf033f 100644 (file)
@@ -899,23 +899,12 @@ ZEND_COLD zend_never_inline void zend_verify_property_type_error(zend_property_i
                return;
        }
 
-       // TODO Switch to a more standard error message?
-       type_str = zend_type_to_string(ZEND_TYPE_WITHOUT_NULL(info->type));
-       if (ZEND_TYPE_IS_CLASS(info->type)) {
-               zend_type_error("Typed property %s::$%s must be an instance of %s%s, %s used",
-                       ZSTR_VAL(info->ce->name),
-                       zend_get_unmangled_property_name(info->name),
-                       ZSTR_VAL(type_str),
-                       ZEND_TYPE_ALLOW_NULL(info->type) ? " or null" : "",
-                       Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property)));
-       } else {
-               zend_type_error("Typed property %s::$%s must be %s%s, %s used",
-                       ZSTR_VAL(info->ce->name),
-                       zend_get_unmangled_property_name(info->name),
-                       ZSTR_VAL(type_str),
-                       ZEND_TYPE_ALLOW_NULL(info->type) ? " or null" : "",
-                       Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property)));
-       }
+       type_str = zend_type_to_string(info->type);
+       zend_type_error("Cannot assign %s to property %s::$%s of type %s",
+               Z_TYPE_P(property) == IS_OBJECT ? ZSTR_VAL(Z_OBJCE_P(property)->name) : zend_get_type_by_const(Z_TYPE_P(property)),
+               ZSTR_VAL(info->ce->name),
+               zend_get_unmangled_property_name(info->name),
+               ZSTR_VAL(type_str));
        zend_string_release(type_str);
 }