wernerkrauss / silverstripe-rector
Rector rules for Silverstripe CMS
Installs: 13 183
Dependents: 13
Suggesters: 0
Security: 0
Stars: 12
Watchers: 3
Forks: 6
Open Issues: 11
Type:rector-extension
pkg:composer/wernerkrauss/silverstripe-rector
Requires
- php: ^7.4 | ^8
- rector/rector: ^2.3.4
Requires (Dev)
- phpstan/phpstan: ^2
- phpunit/phpunit: ^11.3
- squizlabs/php_codesniffer: ^4
This package is auto-updated.
Last update: 2026-01-27 12:31:08 UTC
README
silverstripe-rector
A developer utility for automatically upgrading deprecated code for Silverstripe CMS. With rules for upgrades for Silverstripe 6.
About rector
rector is a tool for automatic code upgrades and refactorings. See rector homepage for more information.
Installation
This module is installable via composer. As Rector uses PHPstan, it's a good idea to install cambis/silverstan, too.
Note: if you need to use PHPStan v1 in your project, use v0.x of this module
composer require phpstan/extension-installer --dev composer require cambis/silverstan --dev composer require wernerkrauss/silverstripe-rector --dev vendor/bin/rector init
Create a basic phpstan.neon file in your project root:
parameters: level: 1 paths: - app/src
This will add all requirements and create a file called rector.php in your project root. You'll need to adjust it, e.g. add the code directories to upgrade and the rules to use.
A basic Rector config can look like
<?php declare(strict_types=1); use Netwerkstatt\SilverstripeRector\Set\SilverstripeLevelSetList; use Netwerkstatt\SilverstripeRector\Set\SilverstripeSetList; use Rector\Config\RectorConfig; return RectorConfig::configure() ->withPreparedSets( deadCode: true, codeQuality: true, codingStyle: true, typeDeclarations: true, instanceOf: true, earlyReturn: true, rectorPreset: true ) ->withPhpSets() //automatically gets the PHP version from composer.json ->withSets([ //silverstripe rector SilverstripeSetList::CODE_STYLE, SilverstripeLevelSetList::UP_TO_SS_6_0 ]);
Silverstripe-rector comes with two types of SetLists: SilverstripeSetList for single sets of rectors (e.g. upgrading from 5.0 to 5.1 or for general Silverstripe code styles) and SilverstripeLevelSetList for combining all set lists up to a given Silverstripe CMS version, e.g. running all upgrades to Silverstripe 5.1.
See also Rector documentation for more configuration possibilities.
Running rector
Once it's configured, you can run Rector in the command line using the following command:
vendor/bin/rector --dry-run
The option --dry-run prints the code changes; if you're happy with the changes, you can remove that option and rector will actually change the files.
Useful options:
--debugfor debugging verbosity. Which files and rules are processed?--xdebugswitch that allows running xdebug.
See vendor/bin/rector --help for more options.
Docs
-
See a list of custom Silverstripe related rectors in the docs.
-
Update from Silverstripe 5 to Silverstripe 6: article on s2-hub.com
-
Better PHP with rector: Talk at StripeConEU 2023 in Hamburg
IDE Support (PHPStorm)
Since Rector uses PHPStan, which is often distributed as a PHAR file, your IDE might not find classes like PHPStan\Type\ObjectType.
To fix this in PHPStorm:
-
Include PHPStan classes:
- Go to
Settings>Languages & Frameworks>PHP. - In the Include Path tab, click
+. - Add the directory
vendor/phpstan/phpstan. PHPStorm will automatically find and index thephpstan.pharinside it. - Alternatively: Install the
phpdoc-parserwhich provides many type definitions:composer require --dev phpstan/phpdoc-parser
- Go to
-
Exclude fixtures: To avoid "Duplicate class" errors from test fixtures, exclude the
tests/**/Fixturedirectories from your project:- Right-click on the
Fixturefolder in the project tree. - Select
Mark Directory as>Excluded.
- Right-click on the
Other useful modules you should know about
Developing
This module is preconfigured to run with ddev and has some useful custom scripts for developing:
PHP Code Sniffer
Code Sniffer is a tool to detect violations of a defined coding standard (mostly PSR-12)
See phpcs.xml.dist for the ruleset used.
Detect violations
ddev composer run lint
or
ddev lint
Fix most violations automatically
ddev composer run fix
or the shortcut
ddev fix
PHPStan
PHP Static Code Analyzer is a tool to detect bugs in your code without running it. It can be used to detect type errors, dead code, and other issues.
Don't be too much annoyed by the errors. Rector can fix a lot of them automatically.
A level of 4 should be doable.
ddev composer run phpstan
or
ddev stan
Rector
Rector is a tool to refactor code automatically. It can be used to upgrade code to a newer version of Silverstripe or PHP.
See rector.php for rules and configuration.
ddev composer run rector-dry #dry run
ddev composer run rector
or
ddev rector --dry-run #dry-run
ddev rector
Everything in one command
There is also a CI tool to run everything in one command
ddev ci
TODO
SS3 to SS4 upgrades (before running official upgrader tool)
- rename
Foo_ControllertoFooController- how can this be made dynamically? via config script that scans the current project?
- configure PSR4 Class To File
- maybe add namespace to
srcdir - various deprecations.
- Is it possible to automate stuff that was once configured in PHP and is now configured in YML?
- easy fix would be to switch to new config layer in PHP and add an annotation to fix this manually
- fix old
Imagefunctions in templates that got deprecated in SS3.2- this needs another file parser for Silverstripe templates
- class
Objectto trait, see ParentClassToTraitsRector
SS4 upgrades
- add
$table_nameif missing - use short classname instead- see similar UnifyModelDatesWithCastsRector
- various deprecations
- to be configured manually in set lists
- fix missing
$ownsfor Image and File relations- configurable exclude list if it's not wanted
- configurable which relations should be automatically owned (e.g. other versioned DataObjects)
General
Misc
- create SetLists for easier configuration
Code Quality
- convert
new Foo()toFoo::create()if it's a Silverstripe / Injectable class - add
@configparam to$db,$has_one, etc. - use Request handler instead of superglobal
$_GETand$_POST
Thanks to
xini for updating this module to Rector V2 and adding a lot of Silverstripe 6 rules.
Need Help?
If you need some help with your Silverstripe project, feel free to contact me ✉️.
See you at next StripeCon 👋