1.2.279. Could Inject Parameter

The parameter is immediately used to create an object. It could be interesting to replace it with an injection of that object’s type to keep the method generic.

<?php

class x {

     // The directory is immediately injected
     function foo(Directory $dir) {
             $this->dir = $dir;
     }

     // Path is injected, then turned into a directory
     function bar(string $path) {
             $this->dir = new Directory($path);
     }
}
?>

1.2.279.1. Suggestions

  • Use the instantiation as the type of the parameter.

1.2.279.2. Specs

Short name

Classes/CouldInjectParam

Rulesets

All, Analyze, Changed Behavior, Class Review

Exakat since

2.4.7

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

injection, typehint

Available in

Entreprise Edition, Exakat Cloud