-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Typed class constants #10444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Typed class constants #10444
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
87a22b2
Implement typed class constants
kocsismate 827d412
Some fixes
kocsismate 05ee202
Add support for trait constants
kocsismate 7b6f3ca
Fixes
kocsismate 6d51940
Newer fixes
kocsismate e4498cc
New review round
kocsismate 715b5bd
Implement delayed variance checks and other fixes
kocsismate 0158e65
Remove forgotten code
kocsismate 4d7639a
Code review fixes
kocsismate e68b560
Remove already removed field
kocsismate 5a6443d
[skip ci] Add upgrading note
kocsismate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Zend/tests/type_declarations/typed_class_constants_diamond_error1.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
Typed class constants (diamond error with self) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const self CONST1 = C; | ||
} | ||
|
||
try { | ||
define("C", new A()); | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
?> | ||
--EXPECT-- | ||
Undefined constant "C" |
14 changes: 14 additions & 0 deletions
14
Zend/tests/type_declarations/typed_class_constants_inheritance_error1.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
Typed class constants (incompatible inheritance; simple) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const int CONST1 = 1; | ||
} | ||
|
||
class B extends A { | ||
public const string CONST1 = 'a'; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Type of B::CONST1 must be compatible with A::CONST1 of type int in %s on line %d |
14 changes: 14 additions & 0 deletions
14
Zend/tests/type_declarations/typed_class_constants_inheritance_error2.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
Typed class constants (incompatible inheritance; missing type in child) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const int CONST1 = 1; | ||
} | ||
|
||
class B extends A { | ||
public const CONST1 = 0; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Type of B::CONST1 must be compatible with A::CONST1 of type int in %s on line %d |
18 changes: 18 additions & 0 deletions
18
Zend/tests/type_declarations/typed_class_constants_inheritance_error3.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Typed class constants (incompatible composition; traits) | ||
--FILE-- | ||
<?php | ||
|
||
trait T { | ||
public const ?array CONST1 = []; | ||
} | ||
|
||
class C { | ||
use T; | ||
|
||
public const CONST1 = []; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: C and T define the same constant (CONST1) in the composition of C. However, the definition differs and is considered incompatible. Class was composed in %s on line %d |
18 changes: 18 additions & 0 deletions
18
Zend/tests/type_declarations/typed_class_constants_inheritance_error4.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Typed class constants (incompatible covariant composition; traits) | ||
--FILE-- | ||
<?php | ||
|
||
trait T { | ||
public const ?array CONST1 = []; | ||
} | ||
|
||
class C { | ||
use T; | ||
|
||
public const array CONST1 = []; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: C and T define the same constant (CONST1) in the composition of C. However, the definition differs and is considered incompatible. Class was composed in %s on line %d |
18 changes: 18 additions & 0 deletions
18
Zend/tests/type_declarations/typed_class_constants_inheritance_error5.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Typed class constants (incompatible contravariant composition; traits) | ||
--FILE-- | ||
<?php | ||
|
||
trait T { | ||
public const array CONST1 = []; | ||
} | ||
|
||
class C { | ||
use T; | ||
|
||
public const ?array CONST1 = []; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: C and T define the same constant (CONST1) in the composition of C. However, the definition differs and is considered incompatible. Class was composed in %s on line %d |
29 changes: 29 additions & 0 deletions
29
Zend/tests/type_declarations/typed_class_constants_inheritance_success1.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--TEST-- | ||
Typed class constants (inheritance success) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const CONST1 = 1; | ||
public const CONST2 = 1; | ||
public const mixed CONST3 = 1; | ||
public const iterable CONST4 = []; | ||
} | ||
|
||
class B extends A { | ||
public const int CONST1 = 0; | ||
public const mixed CONST2 = 0; | ||
public const mixed CONST3 = 0; | ||
public const array CONST4 = []; | ||
} | ||
|
||
var_dump(B::CONST1); | ||
var_dump(B::CONST2); | ||
var_dump(B::CONST3); | ||
var_dump(B::CONST4); | ||
?> | ||
--EXPECT-- | ||
int(0) | ||
int(0) | ||
int(0) | ||
array(0) { | ||
} |
48 changes: 48 additions & 0 deletions
48
Zend/tests/type_declarations/typed_class_constants_inheritance_success2.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--TEST-- | ||
Typed class constants (inheritance success - object types) | ||
--FILE-- | ||
<?php | ||
class S implements Stringable { | ||
public function __toString() { | ||
return ""; | ||
} | ||
} | ||
|
||
class Z extends S {} | ||
|
||
class A { | ||
public const object CONST1 = S; | ||
public const S CONST2 = S; | ||
public const S|Stringable CONST3 = S; | ||
public const S CONST4 = S; | ||
public const ?S CONST5 = S; | ||
} | ||
|
||
class B extends A { | ||
public const S CONST1 = Z; | ||
public const Z CONST2 = Z; | ||
public const S CONST3 = Z; | ||
public const S&Stringable CONST4 = Z; | ||
public const (S&Stringable)|null CONST5 = Z; | ||
} | ||
|
||
define("S", new S()); | ||
define("Z", new Z()); | ||
|
||
var_dump(B::CONST1); | ||
var_dump(B::CONST2); | ||
var_dump(B::CONST3); | ||
var_dump(B::CONST4); | ||
var_dump(B::CONST5); | ||
?> | ||
--EXPECTF-- | ||
object(Z)#%d (%d) { | ||
} | ||
object(Z)#%d (%d) { | ||
} | ||
object(Z)#%d (%d) { | ||
} | ||
object(Z)#%d (%d) { | ||
} | ||
object(Z)#%d (%d) { | ||
} |
16 changes: 16 additions & 0 deletions
16
Zend/tests/type_declarations/typed_class_constants_inheritance_success3.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
Typed class constants (inheritance; private constants) | ||
--FILE-- | ||
<?php | ||
class A { | ||
private const int CONST1 = 1; | ||
} | ||
|
||
class B extends A { | ||
public const string CONST1 = 'a'; | ||
} | ||
|
||
var_dump(B::CONST1); | ||
?> | ||
--EXPECT-- | ||
string(1) "a" |
39 changes: 39 additions & 0 deletions
39
Zend/tests/type_declarations/typed_class_constants_inheritance_success4.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--TEST-- | ||
Typed class constants (composition; traits) | ||
--FILE-- | ||
<?php | ||
|
||
const G = new stdClass(); | ||
|
||
enum E { | ||
case Case1; | ||
} | ||
|
||
trait T { | ||
public const int CONST1 = 1; | ||
public const ?array CONST2 = []; | ||
public const E CONST3 = E::Case1; | ||
public const stdClass CONST4 = G; | ||
} | ||
|
||
class C { | ||
use T; | ||
|
||
public const int CONST1 = 1; | ||
public const ?array CONST2 = []; | ||
public const E CONST3 = E::Case1; | ||
public const stdClass CONST4 = G; | ||
} | ||
|
||
var_dump(C::CONST1); | ||
var_dump(C::CONST2); | ||
var_dump(C::CONST3); | ||
var_dump(C::CONST4); | ||
?> | ||
--EXPECT-- | ||
int(1) | ||
array(0) { | ||
} | ||
enum(E::Case1) | ||
object(stdClass)#1 (0) { | ||
} |
26 changes: 26 additions & 0 deletions
26
Zend/tests/type_declarations/typed_class_constants_inheritance_success5.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Typed class constants (redefinition; interfaces and traits) | ||
--FILE-- | ||
<?php | ||
enum E { | ||
case Case1; | ||
} | ||
|
||
interface I { | ||
public const E CONST1 = E::Case1; | ||
} | ||
|
||
trait T { | ||
public const E CONST1 = E::Case1; | ||
} | ||
|
||
class C implements I { | ||
use T; | ||
|
||
public const E CONST1 = E::Case1; | ||
} | ||
|
||
var_dump(C::CONST1); | ||
?> | ||
--EXPECT-- | ||
enum(E::Case1) |
10 changes: 10 additions & 0 deletions
10
Zend/tests/type_declarations/typed_class_constants_type_error1.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
Typed class constants (type mismatch; simple) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const string CONST1 = 1; | ||
} | ||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use int as value for class constant A::CONST1 of type string in %s on line %d |
15 changes: 15 additions & 0 deletions
15
Zend/tests/type_declarations/typed_class_constants_type_error10.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
Typed class constants (type error) | ||
--FILE-- | ||
<?php | ||
class A1 { | ||
const ?B1 C = null; | ||
} | ||
|
||
class A2 extends A1 { | ||
const ?B2 C = null; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Type of A2::C must be compatible with A1::C of type ?B1 in %s on line %d |
21 changes: 21 additions & 0 deletions
21
Zend/tests/type_declarations/typed_class_constants_type_error11.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Typed class constants (static type error) | ||
--FILE-- | ||
<?php | ||
enum E1 { | ||
const static C = E2::Foo; | ||
} | ||
|
||
enum E2 { | ||
case Foo; | ||
} | ||
|
||
try { | ||
var_dump(E1::C); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot assign E2 to class constant E1::C of type static |
15 changes: 15 additions & 0 deletions
15
Zend/tests/type_declarations/typed_class_constants_type_error12.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
Typed class constants with static in union | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const static|B CONST1 = B; | ||
public const static|stdClass|B CONST2 = B; | ||
} | ||
class B {} | ||
define("B", new B()); | ||
var_dump(new A()); | ||
?> | ||
--EXPECT-- | ||
object(A)#2 (0) { | ||
} |
26 changes: 26 additions & 0 deletions
26
Zend/tests/type_declarations/typed_class_constants_type_error2.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Typed class constants (type mismatch; runtime simple) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const int CONST1 = C; | ||
} | ||
|
||
define("C", "c"); | ||
|
||
try { | ||
var_dump(A::CONST1); | ||
} catch (TypeError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
var_dump(A::CONST1); | ||
} catch (TypeError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot assign string to class constant A::CONST1 of type int | ||
Cannot assign string to class constant A::CONST1 of type int |
25 changes: 25 additions & 0 deletions
25
Zend/tests/type_declarations/typed_class_constants_type_error3.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
Typed class constants (type mismatch; runtime object) | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const string CONST1 = C; | ||
} | ||
|
||
define('C', new stdClass); | ||
|
||
try { | ||
var_dump(A::CONST1); | ||
} catch (TypeError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
var_dump(A::CONST1); | ||
} catch (TypeError $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
?> | ||
--EXPECT-- | ||
Cannot assign stdClass to class constant A::CONST1 of type string | ||
Cannot assign stdClass to class constant A::CONST1 of type string |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.