Skip to content

Get the benefits of final classes in Rector

Jan 15, 2026 1 min read #ide-and-web-dev-tools #php

Originally published on Mastering Laravel.

Rector is a powerful tool for automated code refactoring and upgrades. Among its many features, it can enforce coding standards and apply fixes across your entire codebase.

One setting I find particularly useful is ->withTreatClassesAsFinal(). When enabled, Rector analyzes your code as if all classes were declared final, even when they’re not.

return RectorConfig::configure()
  ->withTreatClassesAsFinal();
php

There’s a whole debate about whether classes should be final by default. I’ll sidestep that. But this setting helps Rector in a very useful and specific way.

It tells Rector to assume your classes won’t be extended and this lets it make smarter decisions about method visibility and inheritance. You get the stricter analysis without actually committing to final in your code.