Skip to content

Deprecation on implicit bool conversions to true for values other than 1 #8565

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
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Zend/tests/scalar_to_bool/explicit_casts_should_not_warn.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Explicit (bool) cast must not warn
--FILE--
<?php

$values =[
3.0,
3.5,
"hello",
"false",
5,
99,
-33,
];

foreach($values as $value) {
var_dump((bool) $value);
}

?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
38 changes: 38 additions & 0 deletions Zend/tests/scalar_to_bool/no_warning_compatible_float.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Compatible float to bool conversions with values 0 or 1
--FILE--
<?php

echo 'Function calls:' . \PHP_EOL;
function foo(bool $a) {
return $a;
}
var_dump(foo(1.0));
var_dump(foo(0.0));
var_dump(foo(-0.0));

echo 'Function returns:' . \PHP_EOL;
function bar(): bool {
return 0.0;
}
var_dump(bar());

echo 'Typed property assignment:' . \PHP_EOL;
class Test {
public bool $a;
}

$instance = new Test();
$instance->a = -0.0;
var_dump($instance->a);

?>
--EXPECTF--
Function calls:
bool(true)
bool(false)
bool(false)
Function returns:
bool(false)
Typed property assignment:
bool(false)
38 changes: 38 additions & 0 deletions Zend/tests/scalar_to_bool/no_warning_compatible_int.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Compatible int to bool conversions with values 0 or 1
--FILE--
<?php

echo 'Function calls:' . \PHP_EOL;
function foo(bool $a) {
return $a;
}
var_dump(foo(1));
var_dump(foo(0));
var_dump(foo(-0));

echo 'Function returns:' . \PHP_EOL;
function bar(): bool {
return 0;
}
var_dump(bar());

echo 'Typed property assignment:' . \PHP_EOL;
class Test {
public bool $a;
}

$instance = new Test();
$instance->a = 1;
var_dump($instance->a);

?>
--EXPECTF--
Function calls:
bool(true)
bool(false)
bool(false)
Function returns:
bool(false)
Typed property assignment:
bool(true)
38 changes: 38 additions & 0 deletions Zend/tests/scalar_to_bool/no_warning_compatible_string.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Compatible string to bool conversions with values "", "0" or "1"
--FILE--
<?php

echo 'Function calls:' . \PHP_EOL;
function foo(bool $a) {
return $a;
}
var_dump(foo(""));
var_dump(foo("1"));
var_dump(foo("0"));

echo 'Function returns:' . \PHP_EOL;
function bar(): bool {
return "0";
}
var_dump(bar());

echo 'Typed property assignment:' . \PHP_EOL;
class Test {
public bool $a;
}

$instance = new Test();
$instance->a = "1";
var_dump($instance->a);

?>
--EXPECTF--
Function calls:
bool(false)
bool(true)
bool(false)
Function returns:
bool(false)
Typed property assignment:
bool(true)
40 changes: 40 additions & 0 deletions Zend/tests/scalar_to_bool/warnings_float_non_zero_or_one.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Implicit float to bool conversions with values other than 0 or 1 should warn for variables
--FILE--
<?php

echo 'Function calls:' . \PHP_EOL;
function foo(bool $a) {
return $a;
}
var_dump(foo(1.1));

echo 'Function returns:' . \PHP_EOL;
function bar(): bool {
return -0.3;
}
var_dump(bar());

echo 'Typed property assignment:' . \PHP_EOL;
class Test {
public bool $a;
}

$instance = new Test();
$instance->a = 13.0;
var_dump($instance->a);

?>
--EXPECTF--
Function calls:

Deprecated: Implicit conversion from float 1.1 to true, only 0 or 1 are allowed in %s on line %d
bool(true)
Function returns:

Deprecated: Implicit conversion from float -0.3 to true, only 0 or 1 are allowed in %s on line %d
bool(true)
Typed property assignment:

Deprecated: Implicit conversion from float 13 to true, only 0 or 1 are allowed in %s on line %d
bool(true)
40 changes: 40 additions & 0 deletions Zend/tests/scalar_to_bool/warnings_int_non_zero_or_one.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Implicit int to bool conversions with values other than 0 or 1 should warn for variables
--FILE--
<?php

echo 'Function calls:' . \PHP_EOL;
function foo(bool $a) {
return $a;
}
var_dump(foo(5));

echo 'Function returns:' . \PHP_EOL;
function bar(): bool {
return 3;
}
var_dump(bar());

echo 'Typed property assignment:' . \PHP_EOL;
class Test {
public bool $a;
}

$instance = new Test();
$instance->a = 13;
var_dump($instance->a);

?>
--EXPECTF--
Function calls:

Deprecated: Implicit conversion from int 5 to true, only 0 or 1 are allowed in %s on line %d
bool(true)
Function returns:

Deprecated: Implicit conversion from int 3 to true, only 0 or 1 are allowed in %s on line %d
bool(true)
Typed property assignment:

Deprecated: Implicit conversion from int 13 to true, only 0 or 1 are allowed in %s on line %d
bool(true)
40 changes: 40 additions & 0 deletions Zend/tests/scalar_to_bool/warnings_string_non_zero_or_one.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Implicit string to bool conversions with values other than empty string, "0" or "1" should warn for variables
--FILE--
<?php

echo 'Function calls:' . \PHP_EOL;
function foo(bool $a) {
return $a;
}
var_dump(foo("on"));

echo 'Function returns:' . \PHP_EOL;
function bar(): bool {
return "inactive";
}
var_dump(bar());

echo 'Typed property assignment:' . \PHP_EOL;
class Test {
public bool $a;
}

$instance = new Test();
$instance->a = "0.0";
var_dump($instance->a);

?>
--EXPECTF--
Function calls:

Deprecated: Implicit conversion from string "on" to true, only "", "0" or "1" are allowed in %s on line %d
bool(true)
Function returns:

Deprecated: Implicit conversion from string "inactive" to true, only "", "0" or "1" are allowed in %s on line %d
bool(true)
Typed property assignment:

Deprecated: Implicit conversion from string "0.0" to true, only "", "0" or "1" are allowed in %s on line %d
bool(true)
5 changes: 5 additions & 0 deletions Zend/tests/type_declarations/scalar_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,26 @@ bool(true)
bool(true)

*** Trying float(1.5)
E_DEPRECATED: Implicit conversion from float 1.5 to true, only 0 or 1 are allowed on line %d
bool(true)

*** Trying string(2) "1a"
E_DEPRECATED: Implicit conversion from string "1a" to true, only "", "0" or "1" are allowed on line %d
bool(true)

*** Trying string(1) "a"
E_DEPRECATED: Implicit conversion from string "a" to true, only "", "0" or "1" are allowed on line %d
bool(true)

*** Trying string(0) ""
bool(false)

*** Trying int(%d)
E_DEPRECATED: Implicit conversion from int %d to true, only 0 or 1 are allowed on line %d
bool(true)

*** Trying float(NAN)
E_DEPRECATED: Implicit conversion from float NAN to true, only 0 or 1 are allowed on line %d
bool(true)

*** Trying bool(true)
Expand Down
5 changes: 5 additions & 0 deletions Zend/tests/type_declarations/scalar_return_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,21 @@ bool(true)
*** Trying float(1)
bool(true)
*** Trying float(1.5)
E_DEPRECATED: Implicit conversion from float 1.5 to true, only 0 or 1 are allowed on line %d
bool(true)
*** Trying string(2) "1a"
E_DEPRECATED: Implicit conversion from string "1a" to true, only "", "0" or "1" are allowed on line %d
bool(true)
*** Trying string(1) "a"
E_DEPRECATED: Implicit conversion from string "a" to true, only "", "0" or "1" are allowed on line %d
bool(true)
*** Trying string(0) ""
bool(false)
*** Trying int(2147483647)
E_DEPRECATED: Implicit conversion from int 2147483647 to true, only 0 or 1 are allowed on line %d
bool(true)
*** Trying float(NAN)
E_DEPRECATED: Implicit conversion from float NAN to true, only 0 or 1 are allowed on line %d
bool(true)
*** Trying bool(true)
bool(true)
Expand Down
5 changes: 5 additions & 0 deletions Zend/tests/type_declarations/scalar_return_basic_64bit.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,21 @@ bool(true)
*** Trying float(1)
bool(true)
*** Trying float(1.5)
E_DEPRECATED: Implicit conversion from float 1.5 to true, only 0 or 1 are allowed on line %d
bool(true)
*** Trying string(2) "1a"
E_DEPRECATED: Implicit conversion from string "1a" to true, only "", "0" or "1" are allowed on line %d
bool(true)
*** Trying string(1) "a"
E_DEPRECATED: Implicit conversion from string "a" to true, only "", "0" or "1" are allowed on line %d
bool(true)
*** Trying string(0) ""
bool(false)
*** Trying int(9223372036854775807)
E_DEPRECATED: Implicit conversion from int 9223372036854775807 to true, only 0 or 1 are allowed on line %d
bool(true)
*** Trying float(NAN)
E_DEPRECATED: Implicit conversion from float NAN to true, only 0 or 1 are allowed on line %d
bool(true)
*** Trying bool(true)
bool(true)
Expand Down
24 changes: 12 additions & 12 deletions Zend/tests/type_declarations/union_types/type_checking_weak.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ Type int|float|bool:
INF => INF
"42" => 42
"42.0" => 42.0
"42x" => true
"x" => true
"42x" => true (Implicit conversion from string "42x" to true, only "", "0" or "1" are allowed)
"x" => true (Implicit conversion from string "x" to true, only "", "0" or "1" are allowed)
"" => false
true => true
false => false
Expand All @@ -111,11 +111,11 @@ new WithToString => Argument ... must be of type int|float|bool, WithToString gi
Type int|bool:
42 => 42
42.0 => 42
INF => true
INF => true (Implicit conversion from float INF to true, only 0 or 1 are allowed)
"42" => 42
"42.0" => 42
"42x" => true
"x" => true
"42x" => true (Implicit conversion from string "42x" to true, only "", "0" or "1" are allowed)
"x" => true (Implicit conversion from string "x" to true, only "", "0" or "1" are allowed)
"" => false
true => true
false => false
Expand Down Expand Up @@ -189,13 +189,13 @@ new stdClass => Argument ... must be of type array|string, stdClass given
new WithToString => "__toString()"

Type bool|array:
42 => true
42.0 => true
INF => true
"42" => true
"42.0" => true
"42x" => true
"x" => true
42 => true (Implicit conversion from int 42 to true, only 0 or 1 are allowed)
42.0 => true (Implicit conversion from float 42 to true, only 0 or 1 are allowed)
INF => true (Implicit conversion from float INF to true, only 0 or 1 are allowed)
"42" => true (Implicit conversion from string "42" to true, only "", "0" or "1" are allowed)
"42.0" => true (Implicit conversion from string "42.0" to true, only "", "0" or "1" are allowed)
"42x" => true (Implicit conversion from string "42x" to true, only "", "0" or "1" are allowed)
"x" => true (Implicit conversion from string "x" to true, only "", "0" or "1" are allowed)
"" => false
true => true
false => false
Expand Down
Loading