|
| 1 | +--- |
| 2 | +description: Use [[=product_name_base=]] Rector, an optional package based on Rector, to remove PHP and JavaScript code deprecations. |
| 3 | +--- |
| 4 | + |
| 5 | +# [[= product_name_base =]] Rector |
| 6 | + |
| 7 | +[[= product_name_base =]] Rector is an optional package based on [Rector](https://getrector.com/) that comes with additional rule sets for working with [[= product_name =]] code. |
| 8 | +You can use it to get rid of PHP code deprecations and to help prepare your project for the next major release. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +Add the Composer dependency: |
| 13 | + |
| 14 | +``` bash |
| 15 | +composer require --dev ibexa/rector:[[= latest_tag_5_0 =]] |
| 16 | +``` |
| 17 | + |
| 18 | +## Refactor PHP code |
| 19 | + |
| 20 | +### Configuration |
| 21 | + |
| 22 | +Adjust the generated `rector.php` file by: |
| 23 | + |
| 24 | +- making it match your project's directory structure |
| 25 | +- selecting the [[= product_name_base =]] rule set that matches your current version, for example [`IbexaSetList::IBEXA_50`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Rector-Sets-IbexaSetList.html#enumcase_IBEXA_50) |
| 26 | +- adding project-specific rules: |
| 27 | + - [PHP rules by using `withPhpSets`](https://getrector.com/documentation/set-lists#content-php-sets) |
| 28 | + - [Symfony, Twig, or Doctrine rules by using `withComposerBased`](https://getrector.com/documentation/composer-based-sets) |
| 29 | + |
| 30 | +It's recommended to activate one rule set at a time and preview the output before applying it. |
| 31 | + |
| 32 | +An example configuration looks as follows: |
| 33 | + |
| 34 | +``` php |
| 35 | +use Ibexa\Contracts\Rector\Sets\IbexaSetList; |
| 36 | +use Rector\Config\RectorConfig; |
| 37 | + |
| 38 | +return RectorConfig::configure() |
| 39 | + ->withPaths( |
| 40 | + [ |
| 41 | + __DIR__ . '/src', |
| 42 | + ] |
| 43 | + ) |
| 44 | + ->withSets( |
| 45 | + [ |
| 46 | + IbexaSetList::IBEXA_50->value, |
| 47 | + ] |
| 48 | + ) |
| 49 | + ->withPhpSets(php83: true) |
| 50 | + ->withComposerBased(symfony: true) |
| 51 | +; |
| 52 | +``` |
| 53 | + |
| 54 | +### Usage |
| 55 | + |
| 56 | +Run Rector in dry-run mode to preview the changes it would make: |
| 57 | + |
| 58 | +``` bash |
| 59 | +vendor/bin/rector --dry-run |
| 60 | +``` |
| 61 | + |
| 62 | +Once you're satisfied with the proposed changes, apply them: |
| 63 | + |
| 64 | +``` bash |
| 65 | +vendor/bin/rector |
| 66 | +``` |
| 67 | + |
| 68 | +## Refactor JavaScipt code |
| 69 | + |
| 70 | +[[= product_name_base =]] Rector also comes with transform module to help you maintain your JavaScript code. |
| 71 | + |
| 72 | +### Configuration |
| 73 | + |
| 74 | +To adjust the default configuration, plugins, or rules, modify the `rector.config.js` file present in your project or bundle directory: |
| 75 | + |
| 76 | +``` js |
| 77 | +module.exports = { |
| 78 | + config: { |
| 79 | + paths: [{ |
| 80 | + input: 'src/bundle/Resources/public', |
| 81 | + output: 'src/bundle/Resources/public', |
| 82 | + }], |
| 83 | + prettierConfigPath: './prettier.js', |
| 84 | + } |
| 85 | + plugins: (plugins) => { |
| 86 | + // modify enabled plugins |
| 87 | + |
| 88 | + return plugins; |
| 89 | + }, |
| 90 | + pluginsConfig: (config) => { |
| 91 | + // modify plugins config |
| 92 | + |
| 93 | + return config; |
| 94 | + } |
| 95 | +}; |
| 96 | +``` |
| 97 | + |
| 98 | +### Configuration options |
| 99 | + |
| 100 | +#### `paths` |
| 101 | + |
| 102 | +Array of objects with input and output directories for transformed files, relative to your project or bundle root. |
| 103 | +Directory structure is not modified during the transformation. |
| 104 | + |
| 105 | +#### `prettierConfigPath` |
| 106 | + |
| 107 | +[Prettier](https://prettier.io/) is run at the end of the transformation. |
| 108 | +You can provide the path to your own configuration file, otherwise [the default file](https://github.com/ibexa/eslint-config-ibexa/blob/main/prettier.js) is used. |
| 109 | + |
| 110 | +#### `plugins` |
| 111 | + |
| 112 | +Use it to modify enabled plugins. |
| 113 | +To learn more about plugins, see [the list of plugins](#list-of-plugins). |
| 114 | + |
| 115 | +#### `pluginsConfig` |
| 116 | + |
| 117 | +Use this setting to modify the plugins configuration, as in the example below: |
| 118 | + |
| 119 | +``` json |
| 120 | +{ |
| 121 | + "ibexa-rename-string-values": { |
| 122 | + "ez-form-error": "ibexa-form-error", |
| 123 | + "ez-selection-settings": { |
| 124 | + "to": "ibexa-selection-settings", |
| 125 | + "exactMatch": true |
| 126 | + }, |
| 127 | + "(^|\\s)\\.ez-": { |
| 128 | + "to": ".ibexa-", |
| 129 | + "regexp": true |
| 130 | + }, |
| 131 | + "ibexa-field-edit--ez([A-Za-z0-9]+)": { |
| 132 | + "to": "ibexa-field-edit--ibexa$1", |
| 133 | + "regexp": true |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +The plugin configuration is an object with plugin names as keys, for example `ibexa-rename-string-values`. |
| 140 | +Inside a single plugin configuration, the property names are values that should be replaced. |
| 141 | +They can be specified explicitly (`ezform-error`) or by using regexp (`(^|\\s)\\.ez-`). |
| 142 | + |
| 143 | +#### Shorthand expression |
| 144 | + |
| 145 | +You can use a shorthand form to specify the configuration: |
| 146 | + |
| 147 | +- `"ez-form-error": "ibexa-form-error"` - changes all `ez-form-error` occurrences to `ibexa-form-error` |
| 148 | + |
| 149 | +#### Complete plugin configuration |
| 150 | + |
| 151 | +When not using the shorthand configuration, the following options are available: |
| 152 | + |
| 153 | +- `"to": "ibexa-selection-settings"` - specifies the new value |
| 154 | +- `"regexp": true/false` - use regexp to find the matching values. Use capture groups to reuse parts of the original value in the new value |
| 155 | +- `"exactMatch": true` - replace matching values only when the whole value is matched. Using the example configuration, `ez-selection-settings__field` would not be replaced as it doesn't match `ez-selection-settings` exactly |
| 156 | + |
| 157 | +#### Shared configuration |
| 158 | + |
| 159 | +You can create a shared configuration for all plugins by using the `shared` keyword, as in the example below: |
| 160 | + |
| 161 | +``` json |
| 162 | +{ |
| 163 | + "shared": { |
| 164 | + "ez": { |
| 165 | + "to": "ibexa", |
| 166 | + "exactMatch": true, |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +Values specifies in the `shared` configuration can be overwritten by using configuration for specific plugins. |
| 173 | + |
| 174 | +### List of plugins |
| 175 | + |
| 176 | +#### Rename eZ global variables |
| 177 | + |
| 178 | +This plugin changes all `eZ` variables to `ibexa`. |
| 179 | + |
| 180 | +Identifier: `ibexa-rename-ez-global` |
| 181 | + |
| 182 | +Configuration: none |
| 183 | + |
| 184 | +#### Rename variables |
| 185 | + |
| 186 | +This plugin allows to rename any variable. |
| 187 | + |
| 188 | +Identifier: `ibexa-rename-variables` |
| 189 | + |
| 190 | +Configuration example: |
| 191 | + |
| 192 | +``` json |
| 193 | +{ |
| 194 | + "^Ez(.*?)Validator$": { |
| 195 | + "to": "Ibexa$1Validator", |
| 196 | + "regexp": true |
| 197 | + }, |
| 198 | + "^EZ_": { |
| 199 | + "to": "IBEXA_", |
| 200 | + "regexp": true |
| 201 | + } |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +**Example:** |
| 206 | + |
| 207 | +| Before | After | |
| 208 | +|---|---| |
| 209 | +| `class EzBooleanValidator extends eZ.BaseFieldValidator` | `class IbexaBooleanValidator extends ibexa.BaseFieldValidator` | |
| 210 | +| `const EZ_INPUT_SELECTOR = 'ezselection-settings__input';` | `const IBEXA_INPUT_SELECTOR = 'ezselection-settings__input';` | |
| 211 | + |
| 212 | +#### Rename string values |
| 213 | + |
| 214 | +This plugin changes any string value except translations. You can use it to transform selectors and other values. |
| 215 | + |
| 216 | +Identifier: `ibexa-rename-string-values` |
| 217 | + |
| 218 | +Configuration example: |
| 219 | + |
| 220 | +``` json |
| 221 | +{ |
| 222 | + "(^|\\s)\\.ez-": { |
| 223 | + "to": ".ibexa-", |
| 224 | + "regexp": true |
| 225 | + }, |
| 226 | + "ibexa-field-edit--ez([A-Za-z0-9]+)": { |
| 227 | + "to": "ibexa-field-edit--ibexa$1", |
| 228 | + "regexp": true |
| 229 | + }, |
| 230 | + "ezselection-settings": "ibexaselection-settings" |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +**Example output:** |
| 235 | + |
| 236 | +| Before | After | |
| 237 | +|---|---| |
| 238 | +| `const SELECTOR_FIELD = '.ez-field-edit--ezboolean';` | `const SELECTOR_FIELD = '.ibexa-field-edit--ezboolean'` | |
| 239 | +| `const SELECTOR_FIELD = '.ibexa-field-edit--ezboolean';` | `const SELECTOR_FIELD = '.ibexa-field-edit--ibexaboolean';` | |
| 240 | + |
| 241 | +#### Rename translation IDs |
| 242 | + |
| 243 | +This plugin allows to change translation ids. |
| 244 | +Extract translations after running this transformation. |
| 245 | + |
| 246 | +Identifier: `ibexa-rename-trans-id` |
| 247 | + |
| 248 | +Configuration example: |
| 249 | + |
| 250 | +``` json |
| 251 | +{ |
| 252 | + "^ez": { |
| 253 | + "to": "ibexa", |
| 254 | + "regexp": true |
| 255 | + } |
| 256 | +} |
| 257 | +``` |
| 258 | + |
| 259 | +**Example output:** |
| 260 | + |
| 261 | +| Before | After | |
| 262 | +|---|---| |
| 263 | +|`'ez_boolean.limitation.pick.ez_error'` | `'ibexa_boolean.limitation.pick.ez_error'` | |
| 264 | + |
| 265 | +#### Rename translation strings |
| 266 | + |
| 267 | +This plugin changes values in translations. |
| 268 | +Extract translations after running this transformation. |
| 269 | + |
| 270 | +Identifier: `ibexa-rename-in-translations` |
| 271 | + |
| 272 | +Configuration example: |
| 273 | + |
| 274 | +``` json |
| 275 | +{ |
| 276 | + "to": "ibexa-not-$1--show-modal", |
| 277 | + "regexp": true, |
| 278 | + "selectors-only": true |
| 279 | +} |
| 280 | +``` |
| 281 | + |
| 282 | +If the `selectors-only` property is set to `true`, this plugin changes only strings inside HTML tags. |
| 283 | +Set to `false` or remove property to change text values as well. |
| 284 | + |
| 285 | +**Example output:** |
| 286 | + |
| 287 | +| `selectors-only` value | Before | After | |
| 288 | +|---|---|---| |
| 289 | +| true | `/*@Desc("<p class='ez-not-error--show-modal'>Show message</p> for ez-not-error--show-modal")*/` | `/*@Desc("<p class='ibexa-not-error--show-modal'>Show message</p> for ez-not-error--show-modal")*/` | |
| 290 | +| false | `/*@Desc("<p class='ez-not-error--show-modal'>Show message</p> for ez-not-error--show-modal")*/` | `/*@Desc("<p class='ibexa-not-error--show-modal'>Show message</p> for ibexa-not-error--show-modal")*/` | |
| 291 | + |
| 292 | +#### Rename icons names used in getIconPath method |
| 293 | + |
| 294 | +This plugin allows you to rename any icon name that is passed as an argument to the `getIconPath` method. |
| 295 | + |
| 296 | +Identifier: `ibexa-rename-icons` |
| 297 | + |
| 298 | +Configuration example: |
| 299 | + |
| 300 | +In this plugin, the `exactMatch` default value is set `true` when using the shorthand expression. |
| 301 | + |
| 302 | +``` json |
| 303 | +{ |
| 304 | + "browse": "folder-browse", |
| 305 | + "content-": { |
| 306 | + "to": "file-", |
| 307 | + "exactMatch": false |
| 308 | + } |
| 309 | +} |
| 310 | +``` |
| 311 | + |
| 312 | +**Example:** |
| 313 | + |
| 314 | +| Before | After | |
| 315 | +|---|---| |
| 316 | +| `getIconPath('browse')` | `getIconPath('folder-browse')` | |
| 317 | + |
| 318 | +### Usage |
| 319 | + |
| 320 | +To install the dependencies, execute the following command: |
| 321 | + |
| 322 | +``` bash |
| 323 | +yarn --cwd ./vendor/ibexa/rector/js install |
| 324 | +``` |
| 325 | + |
| 326 | +Then, run the transform: |
| 327 | + |
| 328 | +``` bash |
| 329 | +yarn --cwd ./vendor/ibexa/rector/js transform |
| 330 | +``` |
| 331 | + |
| 332 | +The `--cwd` argument must point to the directory where the transform module is installed, by default `./vendor/ibexa/rector/js`. |
0 commit comments