1.2.1048. Retyped Reference

A parameter with a reference may be typed differently, at the end of a method call.

It is possible for a referenced and typed parameter to be retyped during a method call. As such, the type of the used variable might both be checked and changed.

Using such syntax will lead to confusion in the code. This works on all types, scalars or objects.

This rule will detect variables which are defined with a placeholder value, or even undefined, and are filled during the method call.

<?php

$a = [1];
foo($a);
echo $a; // Now, $a is a string

function foo(array &$a) {
    $a = "Now, I am a string";
}

?>

1.2.1048.1. Suggestions

  • Do not change a referenced variable’s type

  • Set the called value to a compatible type.

1.2.1048.2. Specs

Short name

Functions/RetypedReference

Rulesets

All, Analyze

Exakat since

2.4.3

PHP Version

All

Severity

Major

Time To Fix

Slow (1 hour)

Precision

High

Features

typehint

Available in

Entreprise Edition, Exakat Cloud