Upgrading Code
Moving to a newer Nette, Symfony, Laravel or PHP? DressCode rewrites your code to the new API of your libraries
and to the new PHP version, in the style your code already has, and reports exactly what is left for you to do. Raise the versions
in composer.json and run a single command. For a big jump, let DressCode build a plan: it measures the move first and
then carries it out step by step, with Composer and your tests.
What It Looks Like
This is how DressCode rewrote the code of a Nette 3 application that moved to nette/utils 4.0:
if (!Strings::endsWith($file, '.jpg')) {
return Image::fromString(Image::EMPTY_GIF);
}
$image->resize(300, 200, Image::FIT | Image::SHRINK_ONLY);
file_put_contents($this->file, Json::encode($products, Json::PRETTY));
if (!\str_ends_with($file, '.jpg')) {
return Image::fromString(Image::EmptyGIF);
}
$image->resize(300, 200, Image::OrSmaller | Image::ShrinkOnly);
file_put_contents($this->file, Json::encode($products, pretty: true));
The method Strings::endsWith(), which nette/utils 4 no longer has, gave way to the PHP function
str_ends_with(), the constants of the class Image got their new names, and the flag
Json::PRETTY became a named argument. You did not have to look up or rewrite any of it by hand.
How does DressCode know what changed between versions? From upgrade data: the packages
dresscode/upgrade-nette, upgrade-symfony, upgrade-laravel and upgrade-deegee
describe what was renamed, moved or removed in the libraries, and DressCode itself knows what PHP deprecated. Where there is no
data, the @deprecated annotations in the code of the library serve instead. How Upgrading Libraries Works covers it in detail.
Lower Bounds Decide
The API version targeted by automated rewrites is not determined by what happens to be installed in vendor/. The
deciding factor is exclusively the constraints declared in composer.json. Code must function on every version allowed
by your constraints, so it must be written for the lowest compatible release: using features from newer releases would break
environments that resolve to the lower bound. Given "nette/forms": "^3.2", code is rewritten to the 3.2 API, even if
3.3 is currently installed in vendor/. The PHP version is read from require.php the same way, see PHP and Package Versions.
Upgrading code is therefore not about downloading a newer release into vendor/, but about raising the lower
bound. That is why an upgrade plan raises constraints in
composer.json before rewriting code for the new API.
Changes from higher versions that your constraints also allow are neither rewritten nor even reported. A rewrite to the newer
API would break the code on the very lowest version it has to work with, and a test with the lowest dependencies
(composer update --prefer-lowest) would catch that at once. With "^3.2 || ^4.0", code is therefore
rewritten to the 3.2 API, and the changes of 4.0 wait until you raise the lower bound. Your tests with the highest dependencies
reveal what would break on a higher allowed version.
What the library retired and DressCode cannot rewrite on its own is reported along with the version that retired it:
Function `acme_format()` is deprecated: use `Acme\Formatter`, since acme/lib 3.2.
The keyword deprecated indicates that the currently installed package still contains the symbol, whereas
removed warns that it is gone. A library required without a lower bound (*), or one present purely as
an unconstrained transitive dependency, can resolve to any version. Neither its upgrade data nor the annotations in its code say
anything then, since nobody knows which version the code has to work with. Such libraries are listed on the Unbounded
line in the upgrade today header, while packages
installed at a higher release than their constraint's minimum appear under Lowest.
To inspect where upgrade data begins and where raising lower bounds would lead, run dresscode config:
Packages 3 with upgrading data
nette/application 3.2 upgrading/application.neon of dresscode/upgrade-nette, upgrading further to 3.2.1, 3.2.3, 3.2.9, 3.2.10, 3.3
nette/component-model 3.2 upgrading/component-model.neon of dresscode/upgrade-nette, upgrading further to 4.0
nette/utils 4.1 upgrading/utils.neon of dresscode/upgrade-nette, upgrading further to 4.1.5
Where DressCode Runs
DressCode requires PHP 8.4 and modern versions of its own dependencies, including nette/utils 4.1. Consequently,
it cannot be installed as a local vendor/ dependency inside an application running on an older PHP or on Nette
3. Install it globally instead, alongside PHPStan and the upgrade package for your ecosystem:
composer global require dresscode/dresscode dresscode/upgrade-nette phpstan/phpstan
Substitute upgrade-nette with upgrade-symfony, upgrade-laravel, or
upgrade-deegee (for Dibi and Texy); see the table of packages for full coverage. Once an
application is on the new versions and only wants to guard against deprecated APIs, DressCode can be added directly to the project
as a standard development dependency (see below).
Two Ways to Upgrade
There are two ways to carry out an upgrade:
- An upgrade plan suits a big jump, such as from Nette 3 to Nette 4. DressCode first measures how much work the move
takes, then builds a plan in steps and carries it out: it rewrites the code, raises the versions in
composer.jsonthrough Composer, and after each step it can run your tests and commit the changes. It needs a configuration,dresscode.neon; if the project does not have one yet,dresscode initmeasures it from the code. The Upgrade Plan Step by Step describes the whole procedure. - The command
upgrade todaysuits the case when you have already raised the versions incomposer.jsonyourself: it rewrites the code to the API of the versions the project requires today, and it works even without a configuration. The next chapter describes it.
Every option of both ways is detailed in the command line reference.
When You Have Already Raised the Versions
If you have already updated constraints in composer.json yourself (using composer require, for
example) and simply want to adapt your codebase to the new APIs your project now demands, run one command:
dresscode upgrade today
What dresscode upgrade today does:
- Requires no configuration file. It automatically inspects the entire project, deriving library and PHP targets directly
from
composer.jsonconstraints (see above). - Rewrites outdated APIs exclusively. It executes the
deprecationsset, applying the data of installed upgrade packages with their rules and handling PHP engine deprecations. Adding--modernizealso updates syntax to newer PHP constructs (themodernizationsset). - Preserves existing visual style. Without a configuration file, it enforces no formatting style; with a configuration file, it applies only the formatting decisions your codebase already satisfies (see below).
- Relies on static types from PHPStan, without which method targets cannot be reliably identified. If PHPStan is not installed, the command refuses to run.
- Reports remaining manual tasks, and if edits were made, advises you to run it a second time.
Here is the terminal output from upgrading a Nette application:
DRESS|CODE 1.3.0 Config /var/www/shop/dresscode.neon Target PHP 8.2 from `composer.json` Upgrading nette/application, nette/component-model, nette/forms, nette/http, nette/utils Fixing 214 files in /var/www/shop app/Presentation/Product/ProductPresenter.php rewritten app/Presentation/Sign/SignPresenter.php rewritten app/Model/Referrals.php error 27:26 Method `Nette\Http\Request::getReferer()` is forbidden: use `getOrigin()`, which reads the Origin header and gives only the scheme, the host and the port instead of the whole URL of the referring page. upgrading.api.forbiddenMembers FIXED 18 violations found, 1 remaining in 3 of 214 files, 3.2 s Run `dresscode upgrade today` again: the code it rewrote may hold more to upgrade.
The Upgrading line lists all libraries whose upgrade data was activated. Files marked rewritten were
updated; remaining manual tasks are listed under their respective files. The final line suggests a second run because freshly
rewritten code may expose secondary deprecations that the first run could not detect. A run that makes no edits omits this
suggestion. How to guard against the old API in every check is described in Keeping the Old API from Coming Back.
A Diff You Can Trust
The upgrade today command writes changes directly to disk, letting Git present the resulting diff. Start with a
clean working tree and inspect the diff after the run: you will find only locations where legacy APIs were used, rewritten to
match surrounding conventions, complete with accurate imports. DressCode performs no naive string replacements. A method call is
rewritten only where static analysis proves it belongs to that specific class: getName() is updated on
FileUpload instances and left untouched everywhere else. See How Upgrading Libraries Works for architectural details. To start
cautiously, target a single file first: dresscode upgrade today --paths app/Model/Orders.php.
Always review the diff. Upgrade data describes syntactic patterns, not runtime semantics: if a new method version returns
-1 instead of null, DressCode updates the method call but leaves an adjacent === null
comparison intact. After upgrading, always run your test suite and PHPStan.
Your Style Stays
A project without a dresscode.neon has no defined style, and upgrade today will touch only
deprecated APIs. When a configuration exists, everything relevant to upgrading applies: paths,
excludePaths, target PHP versions, fixRisky, baselines, overrides, and custom replacement maps. For formatting
decisions, however, it applies only those that your code already follows: DressCode tests your code against your configured style
and drops any formatting decision that is violated anywhere in the project. Newly inserted code matches your real conventions, and
modified files are spared unrelated reformatting.
The unit is the decision, and this filtering is project-wide: a single line exceeding length limits anywhere disables
file.longLines across the entire upgrade run. If you want to reformat your codebase as well, run
dresscode fix afterward, ideally as a separate, dedicated commit.
Missing Data
The upgrade command discovers available upgrade packages from Packagist (currently covering packages under the
official dresscode vendor). It queries at most once per day and caches responses locally, allowing it to work offline
(if offline with an empty cache, it simply offers no new packages). The estimate highlights missing packages, and
upgrade today prompts to install them, provided your project requires that library directly in require
or require-dev:
The libraries of the project have upgrading data in `dresscode/upgrade-nette`, which DressCode does not have. Install it beside DressCode in `C:/Users/me/AppData/Roaming/Composer`?
The package installs into your DressCode environment, leaving your project's composer.json untouched. If DressCode
is installed as a direct project dependency (in composer.lock), the package is installed into the project via
composer require --dev, and the prompt notes that composer.json will be modified. Outside an interactive
terminal (in CI or AI agent hooks), it installs nothing, exits with code 3, and prints the exact Composer command; passing
--yes approves installation non-interactively.
One Major Version at a Time, by Hand
If you prefer not to use an automated plan, you can break a major version jump into manual steps, one major release at a time:
- In
composer.json, bump the lower bound to the next major version (e.g. from"nette/utils": "^3.2"to"^4.0"). If upgrading PHP as well, raiserequire.phpandconfig.platform.phpif defined. - Run
composer update --prefer-lowestsovendor/contains the lowest supported versions that code must target at this stage. - Run
dresscode upgrade today(and rerun if recommended). - Review remaining manual tasks, run tests and PHPStan, review the diff, and commit.
- Proceed to the next version, or run a standard
composer updateto bring dependencies to their latest releases.
Why is --prefer-lowest critical? Where upgrade data is absent, DressCode rewrites code based on
@deprecated annotations in library source code. However, annotations speak only for the specific version currently
installed in vendor/. DressCode intentionally ignores annotations from packages installed above their
constraint's lower bound, as proposed replacements might not exist in the minimum allowed release.
Stepping through major versions ensures upgrade data aligns with the library code present in vendor/. New classes
introduced in that version are available, and @deprecated annotations reveal what will disappear in the subsequent
release; DressCode uses them to rewrite the code right away, or at least to show where you will need to change it. How Upgrading Libraries Works
details how to upgrade applications all at once or rewrite code before updating packages.
What Is Left to You
A high-quality upgrade consists of automated rewrites paired with a list of remaining tasks, and that list represents half of its value. DressCode never guesses: where a clean automated replacement does not exist, or where a developer must choose it, it flags the location and provides instructions:
Constant `Nette\Application\IRouter::SECURED` is forbidden: write the protocol, `https://`, into the mask of the route.
Method `Nette\Http\UrlScript::setPath()` is forbidden: the class is immutable; take a changed copy with `withPath()`.
Every message is an actionable directive. You can work through the list manually, or hand it directly to an AI agent:
upgrade today -f agent formats findings specifically for LLM consumption, while --format json outputs
estimates, plans, runs, and tasks as structured data (see AI Agents). DressCode also reports locations where
upgrade data provides a fix that cannot safely be applied in context; the message states the reason, leaving the code
untouched.
Risky fixes wait for explicit approval. Certain syntactic transformations can alter runtime behavior, such as reading a
property on the left of ??, where PHP first checks whether it is set. In a plan, these are categorized as manual tasks. Decisions you trust globally can be
approved via the fixRisky configuration key, and
running dresscode upgrade today --ask-risky lets you review and approve risky fixes one by one with
inline diffs.
What upgrade data cannot see. Upgrade data describes API signatures, not runtime behavior shifts, template syntax, or NEON configuration files; consult official library upgrade guides for those areas. Furthermore, if a library removes something the data does not record, it will only be caught by PHPStan.
Keeping the Old API from Coming Back
An upgrade is a one-time cleanup. To ensure retired APIs don't creep back in, and to guard against regressions in continuous integration and Git hooks, add the deprecations set and type analysis to your
configuration, listing the upgrade package in use:
typeAnalysis: phpstan
use:
- perCs # your standard
- deprecations
- upgrade-nette
Official packages from the dresscode vendor can be written using their short names in use just like
built-in sets (the full package name dresscode/upgrade-nette works as well). Upgrade data applies even without the
package name in use; naming it enables the executable rules bundled with the package, such as converting Nette's
Json::PRETTY flag to a named argument. If omitted from use, both check and fix
will display a one-time reminder:
Warning: Package `dresscode/upgrade-nette` brings rules that run only where `use` names it, as `upgrade-nette`, and in `dresscode upgrade`; skipped, with what its upgrading files set for them.
From that point on, dresscode check flags legacy API usage in newly authored code. And because data tracks
constraints in composer.json, the check will highlight necessary updates the moment you raise a dependency to the
next version.
Upgrading a Library You Maintain
Libraries that depend on third-party packages are upgraded similarly to applications, except their constraints are widened
rather than raised, so the upgrade plan of a library is different (see A Library Widens, It Does Not Raise).
When I upgraded nette/application this way, upgrade today updated setFactory() calls to
setCreator() and converted RouteList::ONE_WAY flags to oneWay: 1 across both source and
test files, flagged a deprecated property and an annotation whose text does not fit into an attribute, and required no changes on
the second run.
However, libraries have a distinct requirement: test suites that invoke deprecated APIs intentionally (such as backward
compatibility tests, deprecated attribute tests, or type tests of interfaces). You do not want those rewritten. Exclude these test
files using an override in
overrides:
typeAnalysis: phpstan
use:
- nette
- deprecations
overrides:
-
paths: [tests/UI/Requires.sameSite.handle.phpt]
upgrading:
packages: no
declarations:
deprecatedMember: keep
Setting packages: no removes package data for those files, together with the rewrites their files turn on, while
deprecatedMember: keep disables rewrites driven by @deprecated annotations; code style and the remainder
of the set remain fully active. For a single line, // dresscode:ignore upgrading is sufficient (see Suppressions). Findings reported in these tests are expected: they
point to old API that is there on purpose.
Internal implementation code inside a class is never rewritten by upgrade data, which describes how classes are consumed
externally rather than how they are built internally. When nette/application data indicates that writes to
$request->post should become setPost($value), the line $this->post = $post inside
Request::setPost() remains untouched; otherwise the method would recursively call itself. Subclasses, however, are
external consumers and are upgraded normally. Upgrade data for magic properties (__get() and __set())
also applies to private property access from outside, since PHP routes external access to private properties through magic methods
as well.
Upgrading in Stages
To upgrade in stages (for example, upgrading Nette first and PHP later), specify a single target in your estimate and plan, or
restrict upgrade today with --only to a single package and its rules:
dresscode upgrade today --only upgrade-nette
Data from other packages takes effect only if covered by another name in --only (such as deprecations
or upgrading.api). You can filter down to a specific library: --only nette/forms isolates files related
to nette/forms regardless of which package supplies them, and --only php targets PHP engine
deprecations. The same --only option is supported by check and fix, and package/library
names are accepted by fixRisky and severity.
Naming a package whose data has no effect in the project triggers an error explaining the cause (either the data
does not match the project's dependency versions, or upgrading.packages is not enabled). A name that represents
both an upgrade package and a library described by another package covers both: the files in that package and files describing
that library.
Where to Go Next
- The Upgrade Plan Step by Step, the estimate, the plan and carrying it out with Composer, tests and commits.
- Nette libraries, Symfony, Laravel, and Dibi and Texy, what their packages cover.
- Upgrading to a Newer PHP, when raising your PHP version alongside your libraries.
- How Upgrading Libraries Works, for an architectural look at how DressCode discovers API shifts and what it can rewrite automatically.
- Types from PHPStan, which enable safe refactoring of constants, methods, and member calls.