Rector: [Rector] StaticCallOnNonStaticToInstanceCallRector - ReflectionException - Class does not exist when accessing included class statically

Created on 10 Jan 2020  路  8Comments  路  Source: rectorphp/rector

Hi, I'm working on an old codebase that still requires what it needs and all interaction with the database is done like so:

include_once('database.class.php'); // class Database is inside this file
$safeVar = Database::getInstance()->safe($var);

I am trying to upgrade the codebase to PHP7.2 running the following sets:

"php52"
"php53"
"php54"
"php55"
"php56"
"php70"
"php71"
"php72"

Using -vv I can see that it parses all the files fine but when it comes to refactor it errors with

[refactoring] my/file/here.php

In StaticCallOnNonStaticToInstanceCallRector.php line 150:

  [ReflectionException (-1)]
  Class Database does not exist

If I change the code to be like below it passes fine but then fails the next time it encounters Database::. The problem is there are 7246 results of Database::.

include_once('database.class.php');
$db = new Database;
$safeVar = $db::getInstance()->safe($var);

Is there anything I'm doing wrong that is triggering the error and is there any way to resolve it?

All 8 comments

You need to autoload the Database class (file) in Rector: https://github.com/rectorphp/rector#extra-autoloading

Hmm... I am loading our main autoloader but maybe that is for some reason not including the Database class. I'll take a look.

If there is something like include_once('database.class.php');, it's probably missed.

If that doesn't help, try to create minimal reproducible code snippet with minimal rector.yaml config.

Adding it to the rector autoloader fixed it. Onto fixing the rest of the errors now! Your tool is amazingly helpful. Taking a codebase of 7k+ files and cleaning up 15 years of mess!

Wow, that's great to hear! :+1:

If you open to it, I'd like to hear more and maybe make case study from it, so people now how they can use it even old projects.

I'll speak to the boss and send you an email 馃憤

Awesome :+1:

Email sent! 馃憖

Was this page helpful?
0 / 5 - 0 ratings