Skip to content

Commit

Permalink
Make ?? consistent with isset()
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Jun 18, 2021
1 parent 21663e8 commit 185fc35
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 75 deletions.
32 changes: 20 additions & 12 deletions Zend/tests/bug60362.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ if (isset($arr['exists'][1])) {
}

echo "-------------------\n";
if (isset($arr['exists']['non_existent']['sub_sub'])) {
echo "sub-key 'sub_sub' is set: ";
var_dump($arr['exists']['non_existent']['sub_sub']);
} else {
echo "sub-sub-key 'sub_sub' is not set.\n";
try {
if (isset($arr['exists']['non_existent']['sub_sub'])) {
echo "sub-key 'sub_sub' is set: ";
var_dump($arr['exists']['non_existent']['sub_sub']);
} else {
echo "sub-sub-key 'sub_sub' is not set.\n";
}
} catch (\TypeError $e) {
echo $e->getMessage(), \PHP_EOL;
}
if (isset($arr['exists'][1][0])) {
echo "sub-sub-key 0 is set: ";
Expand Down Expand Up @@ -55,11 +59,15 @@ if (empty($arr['exists'][1])) {
}

echo "-------------------\n";
if (empty($arr['exists']['non_existent']['sub_sub'])) {
echo "sub-sub-key 'sub_sub' is empty.\n";
} else {
echo "sub-sub-key 'sub_sub' is not empty: ";
var_dump($arr['exists']['non_existent']['sub_sub']);
try {
if (empty($arr['exists']['non_existent']['sub_sub'])) {
echo "sub-sub-key 'sub_sub' is empty.\n";
} else {
echo "sub-sub-key 'sub_sub' is not empty: ";
var_dump($arr['exists']['non_existent']['sub_sub']);
}
} catch (\TypeError $e) {
echo $e->getMessage(), \PHP_EOL;
}
if (empty($arr['exists'][1][0])) {
echo "sub-sub-key 0 is empty.\n";
Expand All @@ -73,12 +81,12 @@ echo "DONE";
Cannot access offset of type string on string
sub-key 1 is set: string(1) "o"
-------------------
sub-sub-key 'sub_sub' is not set.
Cannot access offset of type string on string
sub-sub-key 0 is set: string(1) "o"
-------------------
Cannot access offset of type string on string
sub-key 1 is not empty: string(1) "o"
-------------------
sub-sub-key 'sub_sub' is empty.
Cannot access offset of type string on string
sub-sub-key 0 is not empty: string(1) "o"
DONE
16 changes: 12 additions & 4 deletions Zend/tests/bug62680.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ Bug #62680 (Function isset() throws fatal error on set array if non-existent key
--FILE--
<?php
$array = array("");
var_dump(isset($array[0]["a"]["b"]));
var_dump(isset($array[0]["a"]["b"]["c"]));
try {
var_dump(isset($array[0]["a"]["b"]));
} catch (\TypeError $e) {
echo $e->getMessage(), \PHP_EOL;
}
try {
var_dump(isset($array[0]["a"]["b"]["c"]));
} catch (\TypeError $e) {
echo $e->getMessage(), \PHP_EOL;
}
?>
--EXPECT--
bool(false)
bool(false)
Cannot access offset of type string on string
Cannot access offset of type string on string
8 changes: 6 additions & 2 deletions Zend/tests/bug69889.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ var_dump($foo[0] ?? "default");
var_dump($foo[5] ?? "default");
var_dump(isset($foo[5]) ? $foo[5] : "default");

var_dump($foo["str"] ?? "default"); // TODO Make this also throw a TypeError?
try {
var_dump($foo["str"] ?? "default");
} catch (\TypeError $e) {
echo $e->getMessage(), \PHP_EOL;
}

try {
var_dump(isset($foo["str"]) ? $foo["str"] : "default");
Expand All @@ -22,5 +26,5 @@ try {
string(1) "t"
string(7) "default"
string(7) "default"
string(7) "default"
Cannot access offset of type string on string
Cannot access offset of type string on string
20 changes: 18 additions & 2 deletions Zend/tests/bug81160.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ try {
} catch (\Throwable $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($s[$o] ?? 'default');
} catch (\Throwable $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump(isset($s[$a]));
} catch (\Throwable $e) {
Expand All @@ -27,6 +32,11 @@ try {
} catch (\Throwable $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($s[$a] ?? 'default');
} catch (\Throwable $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump(isset($s[[]]));
} catch (\Throwable $e) {
Expand All @@ -37,13 +47,19 @@ try {
} catch (\Throwable $e) {
echo $e->getMessage(), "\n";
}

// TODO handle ??
try {
var_dump($s[[]] ?? 'default');
} catch (\Throwable $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
Cannot access offset of type stdClass on string
Cannot access offset of type stdClass on string
Cannot access offset of type stdClass on string
Cannot access offset of type array on string
Cannot access offset of type array on string
Cannot access offset of type array on string
Cannot access offset of type array on string
Cannot access offset of type array on string
Expand Down
4 changes: 4 additions & 0 deletions Zend/tests/empty_str_offset.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ try {
print "- bool ---\n";
var_dump(empty($str[true]));
var_dump(empty($str[false]));
echo "Sub-keys:\n";
var_dump(empty($str[false][true]));
print "- null ---\n";
var_dump(empty($str[null]));
Expand Down Expand Up @@ -142,6 +143,9 @@ bool(false)

Warning: String offset cast occurred in %s on line %d
bool(false)
Sub-keys:

Warning: String offset cast occurred in %s on line %d

Warning: String offset cast occurred in %s on line %d
bool(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ Float variable
Warning: String offset cast occurred in %s on line %d
string(1) "H"
Float casted to string compile

Warning: Uninitialized string offset 9223372036854775807 in %s on line %d
TypeError
Float string variable

Warning: Uninitialized string offset 9223372036854775807 in %s on line %d
TypeError
Attempt to assign
Float
Expand Down
4 changes: 4 additions & 0 deletions Zend/tests/isset_str_offset.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ try {
print "- bool ---\n";
var_dump(isset($str[true]));
var_dump(isset($str[false]));
echo "Sub-keys:\n";
var_dump(isset($str[false][true]));
print "- null ---\n";
var_dump(isset($str[null]));
Expand Down Expand Up @@ -139,6 +140,9 @@ bool(true)

Warning: String offset cast occurred in %s on line %d
bool(true)
Sub-keys:

Warning: String offset cast occurred in %s on line %d

Warning: String offset cast occurred in %s on line %d
bool(false)
Expand Down
2 changes: 0 additions & 2 deletions Zend/tests/offset_string.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ string(1) "i"
Warning: String offset cast occurred in %s on line %d
string(1) "S"
Cannot access offset of type resource on string

Warning: Object of class stdClass could not be converted to int in %s on line %d
Cannot access offset of type stdClass on string
Cannot access offset of type array on string
Done
51 changes: 6 additions & 45 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
goto try_again;
default:
zend_illegal_string_offset(dim);
break;
return 0;
}

offset = zval_get_long_func(dim, /* is_strict */ false);
Expand Down Expand Up @@ -2405,51 +2405,12 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
if (!is_list && EXPECTED(Z_TYPE_P(container) == IS_STRING)) {
zend_long offset;

try_string_offset:
if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) {
switch (Z_TYPE_P(dim)) {
case IS_STRING:
{
bool trailing_data = false;
/* For BC reasons we allow errors so that we can warn on leading numeric string */
if (IS_LONG == is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset,
NULL, /* allow errors */ true, NULL, &trailing_data)) {
if (UNEXPECTED(trailing_data)) {
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
}
goto out;
}
if (type == BP_VAR_IS) {
ZVAL_NULL(result);
return;
}
zend_illegal_string_offset(dim);
break;
}
case IS_UNDEF:
ZVAL_UNDEFINED_OP2();
ZEND_FALLTHROUGH;
case IS_DOUBLE:
case IS_NULL:
case IS_FALSE:
case IS_TRUE:
if (type != BP_VAR_IS) {
zend_error(E_WARNING, "String offset cast occurred");
}
break;
case IS_REFERENCE:
dim = Z_REFVAL_P(dim);
goto try_string_offset;
default:
zend_illegal_string_offset(dim);
break;
}

offset = zval_get_long_func(dim, /* is_strict */ false);
} else {
offset = Z_LVAL_P(dim);
offset = zend_check_string_offset(dim, BP_VAR_IS EXECUTE_DATA_CC);
/* Illegal offset */
if (UNEXPECTED(EG(exception) != NULL)) {
ZVAL_NULL(result);
return;
}
out:

if (UNEXPECTED(Z_STRLEN_P(container) < ((offset < 0) ? -(size_t)offset : ((size_t)offset + 1)))) {
if (type != BP_VAR_IS) {
Expand Down
8 changes: 6 additions & 2 deletions tests/strings/offsets_chaining_5.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ try {
echo $e->getMessage(), \PHP_EOL;
}
var_dump($array['expected_array']['0foo']);
var_dump(isset($array['expected_array']['foo']['bar']));
try {
var_dump(isset($array['expected_array']['foo']['bar']));
} catch (\TypeError $e) {
echo $e->getMessage(), \PHP_EOL;
}
var_dump($array['expected_array']['0foo']['0bar']);
?>
--EXPECTF--
Expand All @@ -21,7 +25,7 @@ Cannot access offset of type string on string

Warning: Illegal string offset "0foo" in %s on line %d
string(1) "f"
bool(false)
Cannot access offset of type string on string

Warning: Illegal string offset "0foo" in %s on line %d

Expand Down
8 changes: 6 additions & 2 deletions tests/strings/offsets_general.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ try {
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
var_dump(isset($string["foo"]["bar"]));
try {
var_dump(isset($string["foo"]["bar"]));
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}

?>
--EXPECT--
Expand All @@ -24,4 +28,4 @@ string(1) "o"
bool(true)
bool(true)
Cannot access offset of type string on string
bool(false)
Cannot access offset of type string on string

0 comments on commit 185fc35

Please sign in to comment.