- What is rector/swiss-knife and what does it do?
- rector/swiss-knife is a framework-agnostic CLI toolkit installed as a dev dependency for PHP and Laravel upgrade work. It detects Git conflict markers and commented code, finds multiple classes per file, aligns namespaces to PSR-4 roots, finalizes childless classes, and provides measure and features reporting. It also includes jack commands for automated PHP version upgrade and downgrade transforms. It runs from vendor/bin and does not couple to your Laravel runtime or container.
- How do I install rector/swiss-knife in a Laravel project?
- Install it as a development dependency with composer require rector/swiss-knife --dev. Invoke commands through vendor/bin/swiss-knife, for example vendor/bin/swiss-knife check-conflicts . in CI or a pre-commit hook. It coexists with Pint, PHPStan, and PHPUnit without requiring runtime changes. Pin the lockfile per repository when rolling it out across multiple Laravel apps.
- Does rector/swiss-knife support Laravel 8 through Laravel 12?
- The package is framework-agnostic and its PHP 7.4 through 8.5 support range spans the runtime floor and ceiling of Laravel 8 through Laravel 12. That means one toolchain version can serve a mixed-version Laravel repo estate. Still validate both ends of your matrix: the lowest PHP version for the oldest Laravel branch and the highest PHP version for the newest. PHP 8.5 support is forward-looking from the tool perspective and should not be treated as app-level validation.
- How do I add Swiss Knife merge conflict detection to CI?
- Run vendor/bin/swiss-knife check-conflicts . as a CI step to fail builds when Git conflict markers such as <<<<<<< HEAD are present. Use --exclude to skip paths like vendor and tests/fixtures, for example vendor/bin/swiss-knife check-conflicts . --exclude vendor --exclude tests/fixtures. This is a low-coupling check that catches conflict markers before pushed code reaches review. It is safe to run alongside existing Laravel CI jobs.
- How does check-commented-code work and can I skip files?
- Run vendor/bin/swiss-knife check-commented-code <directory> to detect commented-out code in your PHP files. You can use --line-limit to control how many lines are considered and --skip-file with patterns such as *Controller.php to exclude files. The command is intended for CI and refactoring workflows rather than runtime enforcement. Start with a narrow directory or line limit to avoid noisy first runs.
- Can rector/swiss-knife find multiple classes per file and fix PSR-4 namespaces?
- Yes. Use vendor/bin/swiss-knife find-multi-classes src to spot files containing more than one class, which breaks proper PSR-4 mapping. Use vendor/bin/swiss-knife namespace-to-psr-4 src with --namespace-root set to your app namespace root, such as App, to rewrite namespaces so they follow the PSR-4 path. Review the diff before committing because namespace rewrites can affect autoloading and references. These checks map cleanly onto Laravel app and package directory conventions.
- What does finalize-classes do and how do I skip mocked classes?
- The finalize-classes command adds final to classes that have no children, helping enforce stricter inheritance rules. Run vendor/bin/swiss-knife finalize-classes src tests. If PHPUnit mocks non-final classes, add --skip-mocked to keep mocked classes non-final; you can also skip specific files with --skip-file and fnmatch-style patterns. Review the diff because finalizing classes can break code that extends them unintentionally.
- Are measure and features safe to use as CI gates?
- measure and features are report generators ported from tomasvotruba/lines, adding class and method LoC, complexity, and language-feature usage reporting. Treat them initially as artifact or JSON outputs for upgrade readiness and tech-debt reporting, not as pass/fail gates. Features output shows which PHP language features are in use before a version bump, while measure output can feed code-quality dashboards. Assign an owner for the metrics because collection without ownership becomes noise.
- What changed in rector/swiss-knife 2.7.0 and what commands were removed?
- Release 2.7.0 improves PHP 7.4 through 8.5 support and adds measure and features reporting, but it also removes spot-lazy-traits, the MockWire util, and an unspecified set of low-value commands referenced as issue 142. Any composer.json script, Makefile target, or CI job that calls those removed commands will fail hard rather than degrade. Before upgrading, inventory references to spot-lazy-traits, MockWire, and dropped commands. If you relied on spot-lazy-traits, establish a PHPStan rule for parity first because that replacement only works if PHPStan is already a pipeline gate.
- How does rector/swiss-knife compare with Pint, PHPStan, and PHPUnit?
- Swiss Knife is not a formatter, static analyzer, or test runner; it is a CLI toolkit for upgrade hygiene and refactoring checks. It coexists with Pint, PHPStan, and PHPUnit, and its PHPStan rule replacing spot-lazy-traits can consolidate tooling if PHPStan is already present. Use it for conflict detection, commented code, PSR-4 checks, finalizing classes, metrics, and jack PHP version transforms. It is a dev-only Composer dependency invoked via vendor/bin, so it does not replace runtime or production tooling.