-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Strict operators directive #4375
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
15f6983
Implemented `declare(strict_operators=1)`
jasny 10dd07c
Implemented strict operators for comparison
jasny 9374ab0
Added tests for operators
jasny 8ad5a45
Operator tests for strict operators (WIP)
jasny 12ace51
Strict operator support for increment/decrement operator
jasny 0571785
Strict operator support for concatenation operator
jasny 55a834f
Strict operators for bitwise ops.
jasny b3895ff
Fixed strict equal operator.
jasny f13984c
Strict operators for case (in switch).
jasny 839de82
Implemented strict_operators support for objects
jasny 2a35e72
Also throw `TypeError` for using NULL with concat operator in strict_…
jasny db905a6
Better error messages for strict operators
jasny 578dff9
More consistent error messages for strict_operators.
jasny ba955e7
Normalize output for spaceship operator when using strict_operators.
jasny 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
function get_test_values() { | ||
return [ | ||
false, | ||
true, | ||
0, | ||
10, | ||
0.0, | ||
10.0, | ||
3.14, | ||
'0', | ||
'10', | ||
'010', | ||
'10 elephants', | ||
'foo', | ||
[], | ||
[1], | ||
[1, 100], | ||
['foo' => 1, 'bar' => 2], | ||
['bar' => 1, 'foo' => 2], | ||
(object)[], | ||
(object)['foo' => 1, 'bar' => 2], | ||
(object)['bar' => 1, 'foo' => 2], | ||
new DateTime(), | ||
fopen('php://temp', 'r+'), | ||
null, | ||
]; | ||
} | ||
|
||
function var_out($value) { | ||
if (is_resource($value)) { | ||
return 'resource'; | ||
} | ||
if ($value instanceof DateTime) { | ||
return 'DateTime'; | ||
} | ||
if ($value instanceof stdClass) { | ||
$pre = '(object) '; | ||
$value = (array)$value; | ||
} | ||
return ($pre ?? '') . preg_replace(['/\n\s*/', '/, \)/'], [' ', ' )'], var_export($value, true)); | ||
} | ||
|
||
function var_out_base64($value) { | ||
if (is_string($value)) { | ||
return 'base64:' . base64_encode($value); | ||
} | ||
return var_out($value); | ||
}; | ||
|
||
function error_to_exception($errno, $errstr, $errfile, $errline) { | ||
if ($errno === E_RECOVERABLE_ERROR) { | ||
throw new ErrorException($errstr, 0, $errno); | ||
} | ||
return false; | ||
} | ||
|
||
function err_out($err) { | ||
$errTypes = [E_RECOVERABLE_ERROR => 'Catchable error', E_WARNING => 'Warning', E_NOTICE => 'Notice']; | ||
return $err !== null ? ' - ' . $errTypes[$err['type']] . ' ' . $err['message'] : ''; | ||
} | ||
|
||
function one_operand($statement, $fn, $a, $var_out = 'var_out') { | ||
error_clear_last(); | ||
echo strtr($statement, ['$a' => var_out($a)]); | ||
|
||
try { | ||
$res = @$fn($a); | ||
} catch (ErrorException $e) { | ||
echo err_out(['type' => $e->getSeverity(), 'message' => $e->getMessage()]), "\n"; | ||
} catch (Throwable $e) { | ||
echo ' - ', get_class($e), ' ', $e->getMessage(), "\n"; | ||
return; | ||
} | ||
|
||
$err = error_get_last(); | ||
echo ' = ', $var_out($res, $a), err_out($err), "\n"; | ||
} | ||
|
||
function test_one_operand($statement, $fn, $var_out = 'var_out') { | ||
$values = get_test_values(); | ||
|
||
foreach ($values as $a) { | ||
one_operand($statement, $fn, $a, $var_out); | ||
} | ||
} | ||
|
||
function two_operands($statement, $fn, $a, $b, $var_out = 'var_out') { | ||
error_clear_last(); | ||
echo strtr($statement, ['$a' => var_out($a), '$b' => var_out($b)]); | ||
|
||
try { | ||
$res = @$fn($a, $b); | ||
} catch (ErrorException $e) { | ||
echo err_out(['type' => $e->getSeverity(), 'message' => $e->getMessage()]), "\n"; | ||
} catch (Throwable $e) { | ||
echo ' - ', get_class($e), ' ', $e->getMessage(), "\n"; | ||
return; | ||
} | ||
|
||
$err = error_get_last(); | ||
echo ' = ', $var_out($res, $a, $b), err_out($err), "\n"; | ||
} | ||
|
||
function test_two_operands($statement, $fn, $var_out = 'var_out') { | ||
$values = get_test_values(); | ||
|
||
foreach ($values as $a) { | ||
foreach ($values as $b) { | ||
two_operands($statement, $fn, $a, $b, $var_out); | ||
} | ||
} | ||
} |
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.