Dependency filing in immutable legacy code

I have an inherited class that I quickly want to write for several tests. Unfortunately, we have a single call in the constructor, and currently there is not enough time to reorganize it.

function __construct(){

   $this->_dbConnect = DbConnect::getInstance();
  // very long constructer (sigh) omitted below ...
}

It is acceptable practice for this to have elegant legacy code:

function __construct(DbConnect $dbConnect = null){

    $this->_dbConnect = isset($dbConnect) ? $dbConnect : DbConnect::getInstance(); 

   // <snip>
}
+3
source share
2 answers

If you want to test only this class - yes , this is regular code.

Sorry for the 1-line answer, but your question already contains the answer :)

+1
source

It is acceptable practice for this to have elegant legacy code:

, . , , DatabaseConnection , . , .

+2

All Articles