Refactor codeigniter 3 #8881
-
Hi! I'm trying to refactor some codeigniter 3 code. I don't know the name of this pattern, but it uses an old school big global object with dynamic properties where all other controllers/models/libraries are loaded. So rector can't really know what type of class or class method is used in an object at $this->Test_model->some_method(). Can I still refactor with rector somehow? Can I manually tell rector that "$this->Test_model" is an object of type Test_model? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi, could you share your rule so far? Via https://getrector.com/custom-rule/ |
Beta Was this translation helpful? Give feedback.
-
For legacy CodeIgniter 3 that lack of composer autoloading, that's not possible, unless you may need to set it via composer classmap definition:
then run then using |
Beta Was this translation helpful? Give feedback.
-
I actually found a way to solve my issue. I leave it here if anyone else happens to have the same problem. It both makes rector work without making custom rules (atleast the things I have tried to do so far) and it makes the code work better in your editor (autocomplete, go to definition, etc). It is basically just a matter of adding all the dynamic properties as actual properties with the type declared.
class CI_Controller
{
// CI Core
public CI_DB $db;
public CI_Input $input;
public MY_Lang $lang; // since I use a customized version, otherwise CI_Lang
public MY_Log $log; // MY since custom
public CI_Output $output;
public CI_Security $security;
public CI_Config $config;
public MY_Router $router; // MY since custom
public CI_URI $uri;
public CI_Benchmark $benchmark;
// CI Libraries
public CI_Email $email;
public CI_Image_lib $image_lib;
public CI_Form_validation $form_validation;
public CI_Parser $parser;
public CI_Upload $upload;
public CI_Session $session;
[there are a few more you can add from in system/libraries that I did not use]
// Your models
...
// Your libraries
...
// All the code of the controller from system/core/Controller.php
...
} |
Beta Was this translation helpful? Give feedback.
For legacy CodeIgniter 3 that lack of composer autoloading, that's not possible, unless you may need to set it via composer classmap definition:
then run
composer dump-autoload
then using
\PHPStan\Reflection\ReflectionProvider
to pull the class based onPropertyFetch->name