Skip to content

Commit fc775f6

Browse files
carusogabrielcmb69
authored andcommitted
Report unknown variables passed to compact()
1 parent ecc1a7c commit fc775f6

File tree

6 files changed

+22
-11
lines changed

6 files changed

+22
-11
lines changed

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ SimpleXML:
8383
treated as integers unconditionally.
8484

8585
Standard:
86+
. Undefined variables passed to compact() will now be reported as a notice.
8687
. getimagesize() and related functions now report the mime type of BMP images
8788
as image/bmp instead of image/x-ms-bmp, since the former has been registered
8889
with the IANA (see RFC 7903).

ext/standard/array.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,14 +2598,15 @@ static void php_compact_var(HashTable *eg_active_symbol_table, zval *return_valu
25982598
ZVAL_DEREF(value_ptr);
25992599
ZVAL_COPY(&data, value_ptr);
26002600
zend_hash_update(Z_ARRVAL_P(return_value), Z_STR_P(entry), &data);
2601-
}
2602-
if (zend_string_equals_literal(Z_STR_P(entry), "this")) {
2601+
} else if (zend_string_equals_literal(Z_STR_P(entry), "this")) {
26032602
zend_object *object = zend_get_this_object(EG(current_execute_data));
26042603
if (object) {
26052604
GC_ADDREF(object);
26062605
ZVAL_OBJ(&data, object);
26072606
zend_hash_update(Z_ARRVAL_P(return_value), Z_STR_P(entry), &data);
26082607
}
2608+
} else {
2609+
php_error_docref(NULL, E_NOTICE, "Undefined variable: %s", ZSTR_VAL(Z_STR_P(entry)));
26092610
}
26102611
} else if (Z_TYPE_P(entry) == IS_ARRAY) {
26112612
if (Z_REFCOUNTED_P(entry)) {

ext/standard/tests/array/bug69198.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ if (false) {
77
}
88
$result = compact('willNeverBeDefined');
99
var_dump($result, empty($result), $result === array(), empty($willNeverBeDefined));
10-
--EXPECT--
10+
?>
11+
--EXPECTF--
12+
Notice: compact(): Undefined variable: willNeverBeDefined in %s on line %d
1113
array(0) {
1214
}
1315
bool(true)

ext/standard/tests/array/compact.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ $location_vars = array("c\\u0327ity", "state");
1212
$result = compact("event", $location_vars);
1313
var_dump($result);
1414
?>
15-
--EXPECT--
15+
--EXPECTF--
16+
Notice: compact(): Undefined variable: c\u0327ity in %s on line %d
1617
array(2) {
1718
["event"]=>
1819
string(8) "SIGGRAPH"

ext/standard/tests/array/compact_basic.phpt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
--TEST--
2-
Test compact() function : basic functionality
2+
Test compact() function : basic functionality
33
--FILE--
44
<?php
55
/* Prototype : proto array compact(mixed var_names [, mixed ...])
6-
* Description: Creates a hash containing variables and their values
6+
* Description: Creates a hash containing variables and their values
77
* Source code: ext/standard/array.c
8-
* Alias to functions:
8+
* Alias to functions:
99
*/
1010

1111
/*
@@ -34,7 +34,7 @@ var_dump (compact(array("g")));
3434

3535
echo "Done";
3636
?>
37-
--EXPECT--
37+
--EXPECTF--
3838
*** Testing compact() : basic functionality ***
3939
array(6) {
4040
["a"]=>
@@ -80,6 +80,8 @@ array(0) {
8080
}
8181
array(0) {
8282
}
83+
84+
Notice: compact(): Undefined variable: g in %s on line %d
8385
array(0) {
8486
}
85-
Done
87+
Done

ext/standard/tests/array/compact_variation2.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,22 @@ f();
2323

2424
?>
2525
==Done==
26-
--EXPECT--
26+
--EXPECTF--
2727
*** Testing compact() : usage variations - variables outside of current scope ***
28+
29+
Notice: compact(): Undefined variable: a in %s on line %d
2830
array(2) {
2931
["b"]=>
3032
string(3) "f.b"
3133
["c"]=>
3234
string(3) "f.c"
3335
}
36+
37+
Notice: compact(): Undefined variable: a in %s on line %d
3438
array(2) {
3539
["b"]=>
3640
string(3) "f.b"
3741
["c"]=>
3842
string(3) "f.c"
3943
}
40-
==Done==
44+
==Done==

0 commit comments

Comments
 (0)