1.2.839. No Valid Cast

This cast generates an error, as there is no way to convert an object to an int.

The result will be 1. This rule applies to float and int. This doesn’t apply to string cast, as the magic method __toString() allows for such conversions.

<?php

$a = (int) foo();

function foo() : A {}


?>

1.2.839.1. Suggestions

  • Create a method that convert the original object to the target type

1.2.839.2. Specs

Short name

Structures/NoValidCast

Rulesets

All, Analyze

Exakat since

2.5.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Features

cast

Available in

Entreprise Edition, Exakat Cloud