Skip to content

Commit bd6067b

Browse files
glbessaclaude
andcommitted
Fix GH-16643: Improve incompatible trait property error message
When a class and a trait (or two traits) define the same property in an incompatible way, the error message now states why the definitions are considered incompatible: differing visibility, static or readonly modifiers, types, or default values, including the case where only one of them has a default value (e.g. a constructor promoted property). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 82b01e2 commit bd6067b

12 files changed

Lines changed: 48 additions & 20 deletions

‎Zend/tests/readonly_props/readonly_trait_mismatch.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ class C {
1515

1616
?>
1717
--EXPECTF--
18-
Fatal error: T1 and T2 define the same property ($prop) in the composition of C. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
18+
Fatal error: T1 and T2 define the same property ($prop) in the composition of C. However, the definition differs and is considered incompatible: only one of them is readonly. Class was composed in %s on line %d

‎Zend/tests/readonly_props/readonly_with_default_trait_mismatch.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ class C {
1717

1818
?>
1919
--EXPECTF--
20-
Fatal error: T1 and T2 define the same property ($prop) in the composition of C. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
20+
Fatal error: T1 and T2 define the same property ($prop) in the composition of C. However, the definition differs and is considered incompatible: the default values differ. Class was composed in %s on line %d

‎Zend/tests/traits/bug60369.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ class Properties {
1414

1515
?>
1616
--EXPECTF--
17-
Fatal error: Properties and PropertiesTrait define the same property ($same) in the composition of Properties. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
17+
Fatal error: Properties and PropertiesTrait define the same property ($same) in the composition of Properties. However, the definition differs and is considered incompatible: only one of them is static. Class was composed in %s on line %d

‎Zend/tests/traits/bug60536_002.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ $t->hello = "foo";
3434
PRE-CLASS-GUARD
3535
POST-CLASS-GUARD
3636

37-
Fatal error: TraitsTest and THello1 define the same property ($hello) in the composition of TraitsTest. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
37+
Fatal error: TraitsTest and THello1 define the same property ($hello) in the composition of TraitsTest. However, the definition differs and is considered incompatible: the visibility differs. Class was composed in %s on line %d

‎Zend/tests/traits/bug74269.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ class PropertiesExample
1414
}
1515
?>
1616
--EXPECTF--
17-
Fatal error: PropertiesExample and PropertiesTrait define the same property ($same) in the composition of PropertiesExample. However, the definition differs and is considered incompatible. Class was composed in %s
17+
Fatal error: PropertiesExample and PropertiesTrait define the same property ($same) in the composition of PropertiesExample. However, the definition differs and is considered incompatible: the default values differ. Class was composed in %s

‎Zend/tests/traits/bugs/overridding-conflicting-property-initializer.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ $obj = new baz();
2020
echo $obj->zoo, "\n";
2121
?>
2222
--EXPECTF--
23-
Fatal error: baz and foo define the same property ($zoo) in the composition of baz. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
23+
Fatal error: baz and foo define the same property ($zoo) in the composition of baz. However, the definition differs and is considered incompatible: the default values differ. Class was composed in %s on line %d

‎Zend/tests/traits/gh16643.phpt‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-16643: Incompatible trait property error message states the reason
3+
--FILE--
4+
<?php
5+
6+
trait FooTrait {
7+
private array $options = [];
8+
}
9+
10+
class Foo {
11+
use FooTrait;
12+
13+
public function __construct(private array $options = []) {}
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Foo and FooTrait define the same property ($options) in the composition of Foo. However, the definition differs and is considered incompatible: only one of them has a default value. Class was composed in %s on line %d

‎Zend/tests/traits/property003.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ $t->hello = "foo";
2626
--EXPECTF--
2727
PRE-CLASS-GUARD
2828

29-
Fatal error: THello1 and THello2 define the same property ($hello) in the composition of TraitsTest. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
29+
Fatal error: THello1 and THello2 define the same property ($hello) in the composition of TraitsTest. However, the definition differs and is considered incompatible: the visibility differs. Class was composed in %s on line %d

‎Zend/tests/traits/property004.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ $t = new TraitsTest;
2626
--EXPECTF--
2727
PRE-CLASS-GUARD
2828

29-
Fatal error: THello1 and THello2 define the same property ($hello) in the composition of TraitsTest. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
29+
Fatal error: THello1 and THello2 define the same property ($hello) in the composition of TraitsTest. However, the definition differs and is considered incompatible: the visibility differs. Class was composed in %s on line %d

‎Zend/tests/traits/property005.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ $t->hello = "foo";
3434
PRE-CLASS-GUARD
3535
POST-CLASS-GUARD
3636

37-
Fatal error: TraitsTest and THello1 define the same property ($hello) in the composition of TraitsTest. However, the definition differs and is considered incompatible. Class was composed in %s on line %d
37+
Fatal error: TraitsTest and THello1 define the same property ($hello) in the composition of TraitsTest. However, the definition differs and is considered incompatible: the visibility differs. Class was composed in %s on line %d

0 commit comments

Comments
 (0)