rector/docs/MagicDisclosureRectors.md
2018-03-06 21:32:37 +01:00

1.6 KiB

How to turn Magic to Explicit with Rectors?

Replace get/set magic methods with real ones

services:
    Rector\Rector\MagicDisclosure\GetAndSetToMethodCallRector:
        $typeToMethodCalls:
            # class
            'Nette\DI\Container':
                # magic method (prepared keys): new real method
                'get': 'getService'
                'set': 'addService'

For example:

- $result = $container['key'];
+ $result = $container->getService('key');
- $container['key'] = $value;
+ $container->addService('key', $value);

Replace isset/unset magic methods with real ones

services:
    Rector\Rector\MagicDisclosure\UnsetAndIssetToMethodCallRector:
        $typeToMethodCalls:
            # class
            'Nette\DI\Container':
                # magic method (prepared keys): new real method
                'isset': 'hasService'
                'unset': 'removeService'

For example:

- isset($container['key']);
+ $container->hasService('key');
- unset($container['key']);
+ $container->removeService('key');

Replace toString magic method with real one

services:
    Rector\Rector\MagicDisclosure\ToStringToMethodCallRector:
        $typeToMethodCalls:
            # class
            'Symfony\Component\Config\ConfigCache':
                # magic method (prepared key): new real method
                'toString': 'getPath'

For example:

- $result = (string) $someValue;
+ $result = $someValue->getPath();
- $result = $someValue->__toString();
+ $result = $someValue->getPath();