rector / swiss-knife
Swiss knife in pocket of every upgrade architect
Requires
- php: >=8.4
- ext-tokenizer: *
- entropy/entropy: ^0.4.14
- nette/robot-loader: ^4.1
- nette/utils: ^4.1
- nikic/php-parser: ^5.9
- webmozart/assert: ^2.4
Requires (Dev)
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^12.5
- rector/jack: ^1.1
- rector/rector: ^2.4
- shipmonk/composer-dependency-analyser: ^1.8
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symplify/easy-coding-standard: ^13.2
- symplify/phpstan-rules: ^14.12
- tomasvotruba/class-leak: ^2.1
- tomasvotruba/unused-public: ^2.2
- tracy/tracy: ^2.12
Suggests
None
Provides
None
Conflicts
None
Replaces
None
- dev-main
- 2.6.0
- 2.5.0
- 2.4.8
- 2.4.7
- 2.4.6
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.15
- 2.1.14
- 2.1.13
- 2.1.12
- 2.1.10
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.0
- 0.2.35
- 0.2.34
- 0.2.33
- 0.2.32
- 0.2.31
- 0.2.30
- 0.2.29
- 0.2.28
- 0.2.27
- 0.2.26
- 0.2.25
- 0.2.24
- 0.2.23
- 0.2.22
- 0.2.21
- 0.2.20
- 0.2.19
- 0.2.18
- 0.2.17
- 0.2.16
- 0.2.15
- 0.2.14
- 0.2.13
- 0.2.12
- 0.2.11
- 0.2.10
- 0.2.9
- 0.2.8
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.14
- 0.1.13
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-docs-jack-readme
- dev-move-jack
- dev-php-74-support
- dev-remove-webmozart
- dev-remove-lines-of-code
- dev-remove-robot-loader
- dev-tv-no-symf
- dev-tv-cosn
This package is auto-updated.
Last update: 2026-10-02 09:30:41 UTC
README
Swiss knife in the pocket of every upgrade architect!
Install
composer require rector/swiss-knife --dev
Usage
- 1. Check your Code for Git Merge Conflicts
- 2. Detect Commented Code
- 3. Reach full PSR-4
- 4. Finalize classes without children
- 5. Privatize local class constants
- 6. Spots Fake Traits
- 7. Split huge Symfony config to per-package in directory
- 8. Generate Symfony Smoke Tests
- 9. Detect Duplicated Code
1. Check your Code for Git Merge Conflicts
Do you use Git? Then merge conflict markers are the last thing you want to see in pushed code:
<<<<<<< HEAD
Add this command to CI to spot these:
vendor/bin/swiss-knife check-conflicts .
You can skip paths with the --exclude option:
vendor/bin/swiss-knife check-conflicts . --exclude vendor --exclude tests/fixtures
2. Detect Commented Code
Have you ever forgotten commented code in your project?
// foreach ($matches as $match) { // $content = str_replace($match[0], $match[2], $content); // }
No more! Add this command to CI to spot these:
vendor/bin/swiss-knife check-commented-code <directory> vendor/bin/swiss-knife check-commented-code packages --line-limit 5 --skip-file '*Controller.php'
3. Reach full PSR-4
Find multiple classes in single file
To make PSR-4 work properly, each class must be in its own file. This command makes it easy to spot multiple classes in a single file:
vendor/bin/swiss-knife find-multi-classes src
Update Namespace to match PSR-4 Root
Is your class in wrong namespace? Make it match your PSR-4 root:
vendor/bin/swiss-knife namespace-to-psr-4 src --namespace-root "App\\"
This will update all files in your /src directory, to starts with App\\ and follow full PSR-4 path:
# file path: src/Repository/TalkRepository.php -namespace Model; +namespace App\Repository; ...
4. Finalize classes without children
Do you want to finalize all classes that don't have children?
vendor/bin/swiss-knife finalize-classes src tests
Do you use mocks but not bypass final yet?
vendor/bin/swiss-knife finalize-classes src tests --skip-mocked
This will keep mocked classes non-final, so PHPUnit can extend them internally.
Do you want to skip file or two?
vendor/bin/swiss-knife finalize-classes src tests --skip-file src/SpecialProxy.php
Skip is also support with fnmatch() patterns:
vendor/bin/swiss-knife finalize-classes src tests --skip-file '*Controller.php'
5. Privatize local class constants
PHPStan can report unused private class constants, but it skips all the public ones. Do you have lots of class constants, all of them public but want to narrow scope to privates?
vendor/bin/swiss-knife privatize-constants src test
This command will:
- find all class constant usages
- scans classes and constants
- makes those constant used locally
private
That way all the constants not used outside will be made private safely.
6. Split huge Symfony config to per-package in directory
Do you have a huge Symfony config file that is hard to navigate? Do you want to split it to per-package files?
Before - one huge config/config_dev.php with many extensions in a single file:
<?php declare(strict_types=1); use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->extension('framework', [ 'secret' => '%env(APP_SECRET)%', 'test' => true, ]); $containerConfigurator->extension('doctrine', [ 'dbal' => [ 'url' => '%env(DATABASE_URL)%', ], ]); $containerConfigurator->extension('monolog', [ 'handlers' => [ 'main' => [ 'type' => 'stream', 'path' => '%kernel.logs_dir%/%kernel.environment%.log', ], ], ]); };
Run the command:
vendor/bin/swiss-knife split-config-per-package config/config_dev.php --output-dir config/packages/dev
After - the original config only imports the per-package files:
<?php declare(strict_types=1); use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(__DIR__ . '/packages/dev/*'); };
And each extension lives in its own file, e.g. config/packages/dev/doctrine.php:
<?php declare(strict_types=1); return static function (Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator $containerConfigurator): void { $containerConfigurator->extension('doctrine', [ 'dbal' => [ 'url' => '%env(DATABASE_URL)%', ], ]); };
All the extensions will be extracted to separate files in config/packages/dev directory, making them much more readable.
7. Generate Symfony Smoke Tests
Cover your Symfony app with smoke tests in seconds. This command scans your composer.json, picks the matching test templates, and drops them under tests/Unit/Smoke (or your project's equivalent unit-tests directory).
vendor/bin/swiss-knife generate-symfony-smoke-tests
The command will:
- detect your unit tests directory and create a
Smokesub-directory - generate a
ServiceContainerTestthat boots the kernel and instantiates every service to catch container misconfiguration early - add a shared
AbstractContainerTestCasewith a typedgetService()helper - adjust the namespace and
Kernelclass in the templates to match your project (usesApp\Kernel,AppKernel, orKernel, whichever exists)
Existing files are never overwritten, so the command is safe to re-run.
The generated ServiceContainerTest boots the kernel and asserts every service can be instantiated:
<?php declare(strict_types=1); namespace App\Tests\Unit\Smoke; use Throwable; final class ServiceContainerTest extends AbstractContainerTestCase { public function testServiceConstruction(): void { $serviceIds = self::$container->getServiceIds(); $checkedServiceCount = 0; foreach ($serviceIds as $serviceId) { if ($this->isDynamicService($serviceId)) { continue; } try { self::$container->get($serviceId); } catch (Throwable $throwable) { $this->fail(sprintf('Service "%s" could not be created because:%s%s', $serviceId, PHP_EOL, $throwable->getMessage())); } ++$checkedServiceCount; } // @todo update this number to match your service count $this->assertSame(100000, $checkedServiceCount); } private function isDynamicService(string $serviceId): bool { if (str_contains($serviceId, 'session')) { return true; } if (str_starts_with($serviceId, 'doctrine.')) { return true; } return in_array( $serviceId, ['kernel', 'database_connection', 'event_dispatcher'], true ); } }
8. Detect Duplicated Code
Spot copy-pasted code blocks with a token-based detector, a small clone of phpcpd. Add it to CI to fail when a large copy-pasted block is added:
vendor/bin/swiss-knife duplicated-code src vendor/bin/swiss-knife duplicated-code src rules --min-tokens 150 --min-lines 5
Options:
--min-linesminimum lines of a reported clone (default5)--min-tokensminimum tokens of a reported clone (default70)--fuzzyignore variable names when matching--skip-filefile paths or masks to skip
Exit code is 1 when clones are found, 0 otherwise.
10. Measure Code Size
Measure lines of code and structure size of your project - files, classes, methods, constants and more:
vendor/bin/swiss-knife measure src vendor/bin/swiss-knife measure src tests --exclude tests/fixtures
Options:
--excludepaths to exclude--shortprint short metrics only--longestshow top 10 longest files--allow-vendorallow the/vendordirectory to be scanned--jsonoutput in JSON format
11. Count PHP Features
Count PHP features used in the project, grouped by the PHP version that introduced them. Handy to see how modern your code is before an upgrade:
vendor/bin/swiss-knife features src
Options:
--jsonoutput in JSON format
12. Check for Outdated Dependencies in CI
Postponing upgrades leads to large, risky jumps. The breakpoint command catches outdated major packages early, right in your CI pipeline:
vendor/bin/swiss-knife breakpoint
↓
If there are more than 5 major outdated packages, the CI will fail.
Options:
--limitraise or lower the bar, e.g.--limit 3--devcheck dev packages only (safer to upgrade first)
13. Open up Next Versions
We know we're behind, but where to start? Instead of guessing, let Composer handle it - open up package versions to the next nearest step:
vendor/bin/swiss-knife open-versions
This opens up 5 versions to their next step, e.g.:
{
"require": {
"php": "^7.4",
- "symfony/console": "5.1.*"
+ "symfony/console": "5.1.*|5.2.*"
},
"require-dev": {
- "phpunit/phpunit": "^9.0"
+ "phpunit/phpunit": "^9.0|^10.0"
}
}
Then run composer update - if no blockers exist, Composer updates packages to their next version.
Options:
--limitnumber of packages to open, e.g.--limit 3--package-prefixupgrade only a group, e.g.--package-prefix symfony--devlow-risk dev packages first--dry-runpreview changes without modifyingcomposer.json
14. Raise to Installed Versions
Sometimes it's the opposite - dependencies are new, but composer.json is outdated:
Here illuminate/container allows 12.0 but we already use 12.14, and symfony/finder allows 6.4 but we use 7.2. Running composer update could pull unnecessary older dependencies.
Raise composer.json to the installed versions:
{
"require": {
"php": "^7.4",
- "illuminate/container": "^12.0",
+ "illuminate/container": "^12.14",
- "symfony/finder": "^6.4|^7.2",
+ "symfony/finder": "^7.2"
}
}
vendor/bin/swiss-knife raise-to-installed
Options:
--dry-runpreview changes without applying
That's it!
Happy coding!

