-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add true as a type #8326
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
Add true as a type #8326
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
10 changes: 10 additions & 0 deletions
10
Zend/tests/type_declarations/intersection_types/invalid_types/invalid_true_type.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-- | ||
true type cannot take part in an intersection type | ||
--FILE-- | ||
<?php | ||
|
||
function foo(): true&Iterator {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Type true cannot be part of an intersection type in %s on line %d |
28 changes: 28 additions & 0 deletions
28
Zend/tests/type_declarations/literal_types/false_no_coercion.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,28 @@ | ||
--TEST-- | ||
No coercion should be applied to type false | ||
--FILE-- | ||
<?php | ||
|
||
function test(false $v) { var_dump($v); } | ||
|
||
try { | ||
test(0); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
try { | ||
test(''); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
try { | ||
test([]); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
test(): Argument #1 ($v) must be of type false, int given, called in %s on line %d | ||
test(): Argument #1 ($v) must be of type false, string given, called in %s on line %d | ||
test(): Argument #1 ($v) must be of type false, array given, called in %s on line %d |
31 changes: 31 additions & 0 deletions
31
Zend/tests/type_declarations/literal_types/false_no_coercion_on_overload.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,31 @@ | ||
--TEST-- | ||
No coercion should be applied to type false even if it's an overide | ||
--FILE-- | ||
<?php | ||
|
||
class P { | ||
public function foo($v): array|bool { | ||
return $v; | ||
} | ||
} | ||
|
||
class C { | ||
public function foo($v): array|false { | ||
return $v; | ||
} | ||
} | ||
|
||
$p = new P(); | ||
$c = new C(); | ||
|
||
var_dump($p->foo(0)); | ||
try { | ||
var_dump($c->foo(0)); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
bool(false) | ||
C::foo(): Return value must be of type array|false, int returned |
14 changes: 14 additions & 0 deletions
14
Zend/tests/type_declarations/literal_types/false_standalone.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-- | ||
False can be used as a standalone type | ||
--FILE-- | ||
<?php | ||
|
||
function test(false $v): false { | ||
return $v; | ||
} | ||
|
||
var_dump(test(false)); | ||
|
||
?> | ||
--EXPECT-- | ||
bool(false) |
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
34 changes: 34 additions & 0 deletions
34
Zend/tests/type_declarations/literal_types/true_no_coercion.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,34 @@ | ||
--TEST-- | ||
No coercion should be applied to type true | ||
--FILE-- | ||
<?php | ||
|
||
function test(true $v) { var_dump($v); } | ||
|
||
try { | ||
test(1); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
try { | ||
test('1'); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
try { | ||
test([1]); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
try { | ||
test(new stdClass()); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
test(): Argument #1 ($v) must be of type true, int given, called in %s on line %d | ||
test(): Argument #1 ($v) must be of type true, string given, called in %s on line %d | ||
test(): Argument #1 ($v) must be of type true, array given, called in %s on line %d | ||
test(): Argument #1 ($v) must be of type true, stdClass given, called in %s on line %d |
31 changes: 31 additions & 0 deletions
31
Zend/tests/type_declarations/literal_types/true_no_coercion_on_overload.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,31 @@ | ||
--TEST-- | ||
No coercion should be applied to type true even if it's an overide | ||
--FILE-- | ||
<?php | ||
|
||
class P { | ||
public function foo($v): array|bool { | ||
return $v; | ||
} | ||
} | ||
|
||
class C { | ||
public function foo($v): array|true { | ||
return $v; | ||
} | ||
} | ||
|
||
$p = new P(); | ||
$c = new C(); | ||
|
||
var_dump($p->foo(1)); | ||
try { | ||
var_dump($c->foo(1)); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), \PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
bool(true) | ||
C::foo(): Return value must be of type array|true, int returned |
14 changes: 14 additions & 0 deletions
14
Zend/tests/type_declarations/literal_types/true_standalone.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-- | ||
true can be used as a standalone type | ||
--FILE-- | ||
<?php | ||
|
||
function test(true $v): true { | ||
return $v; | ||
} | ||
|
||
var_dump(test(true)); | ||
|
||
?> | ||
--EXPECT-- | ||
bool(true) |
13 changes: 13 additions & 0 deletions
13
Zend/tests/type_declarations/literal_types/true_standalone_implicit_nullability.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,13 @@ | ||
--TEST-- | ||
true can be used as a standalone type even with implicit nullability | ||
--FILE-- | ||
<?php | ||
|
||
function test(true $v = null) { return $v; } | ||
|
||
var_dump(test(true)); | ||
var_dump(test(null)); | ||
?> | ||
--EXPECT-- | ||
bool(true) | ||
NULL |
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,19 @@ | ||
--TEST-- | ||
Test typed properties allow true | ||
--FILE-- | ||
<?php | ||
class Foo { | ||
public true $value; | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
try { | ||
$foo->value = false; | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot assign bool to property Foo::$value of type true |
11 changes: 11 additions & 0 deletions
11
Zend/tests/type_declarations/union_types/redundant_types/bool_and_true.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,11 @@ | ||
--TEST-- | ||
Using both bool and true in a union | ||
--FILE-- | ||
<?php | ||
|
||
function test(): bool|true { | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Duplicate type true is redundant in %s on line %d |
11 changes: 11 additions & 0 deletions
11
Zend/tests/type_declarations/union_types/redundant_types/bool_instead_false_and_true.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,11 @@ | ||
--TEST-- | ||
Using both false and true in a union instead of bool | ||
--FILE-- | ||
<?php | ||
|
||
function test(): false|true { | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Type contains both true and false, bool should be used instead in %s on line %d |
11 changes: 11 additions & 0 deletions
11
Zend/tests/type_declarations/union_types/redundant_types/bool_instead_true_and_false.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,11 @@ | ||
--TEST-- | ||
Using both true and false in a union instead of bool | ||
--FILE-- | ||
<?php | ||
|
||
function test(): true|false { | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Type contains both true and false, bool should be used instead in %s on line %d |
11 changes: 0 additions & 11 deletions
11
Zend/tests/type_declarations/union_types/standalone_false.phpt
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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.