Code Inspection: 'assertTrue()' with incompatible argument type
Reports the PHPUnit assertTrue()
calls whose arguments are of incompatible types. Since the assertTrue()
method relies on strict types comparison with true
, such assertions will always fail.
In the following example, the performAction()
function returns a value of integer
type, so the PHPUnit assertion will always fail. After the quick-fix is applied, the returned value is cast to boolean
; as a result, the assertion will only fail in case 0
is returned and will pass otherwise. For more information about type conversions in PHP, refer to Type Casting (php.net).
function performAction() : int {}
class Test extends TestCase {
function doTest() {
$this->assertTrue(performAction());
}
}
function performAction() : int {}
class Test extends TestCase {
function doTest() {
$this->assertTrue((bool)performAction());
}
}
Suppress an inspection in the editor
Place the caret at the highlighted line and press Alt+Enter or click .
Click the arrow next to the inspection you want to suppress and select the necessary suppress action.
Last modified: 11 February 2024