Search code examples
phprefactoringphpstormautomated-refactoring

convert from public property access to getter in PHPStorm


I have a project which contains many instances of public properties of a set of classes being accessed directly via $object->property. After having converted the properties to protected and having generated getters and setters for each property.

I'd now like to refactor each publicly accessed property with the appropriate getter. Is there anyway this can be automated via PHPStorm?

Find and Replace isn't useful because the objects have different names in different places.

PHPStorm has already detected the Member has protected access. Could it detect the appropriate getter is also generated and make the replacement where it occurs?


Solution

  • Unfortunately the "encapsulate fields" refactoring option from IDEA is not available in PhpStorm, so it's not possible to fully automate this process. It may be possible to write a plugin to do so, but as far as I have found, none is currently available.

    If I read your post correctly you've already made the fields protected and generated the getters needed. To quickly go over all occurrences of access to protected properties, you could create a new inspection profile for annotator inspections only:

    1. Go to File -> Settings
    2. Go to Editor -> Inspections
    3. Click Manage -> Copy to create a new inspection profile.
    4. Disable all inspections except of General -> Annotator.
    5. Now right click your project root, click Inspect Code, and perform the inspection with your new profile. All occurrences of protected or private access will be listed.

    I hope this is at least somewhat useful for you :)