Swiss knife in pocket of every upgrade architect!
composer require rector/swiss-knife --devDo you use Git? Then merge conflicts is not what you want in your code ever to see in pushed code:
<<<<<<< HEADAdd 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/fixturesHave you ever forgot commented code in your code?
// 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'To make PSR-4 work properly, each class must be in its own file. This command makes it easy to spot multiple classes in single file:
vendor/bin/swiss-knife find-multi-classes srcIs 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;
...Do you want to finalize all classes that don't have children?
vendor/bin/swiss-knife finalize-classes src testsDo you use mocks but not bypass final yet?
vendor/bin/swiss-knife finalize-classes src tests --skip-mockedThis 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.phpSkip is also support with fnmatch() patterns:
vendor/bin/swiss-knife finalize-classes src tests --skip-file '*Controller.php'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 testThis 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.
Imagine there is a service that has 6 dependencies in __construct():
final class RealClass
{
public function __construct(
private readonly FirstService $firstService,
private readonly SecondService $secondService,
private readonly ThirdService $thirdService,
private readonly FourthService $fourthService,
private readonly FifthService $fifthService,
private readonly SixthService $sixthService
) {
}
}But we want to mock only one of them:
use Rector\SwissKnife\Testing\MockWire;
// pass a mock
$thirdDependencyMock = $this->createMock(ThirdDependency::class);
$thirdDependencyMock->method('someMethod')->willReturn('some value');
$realClass = MockWire::create(RealClass::class, [
$thirdDependencyMock
]);Or pass direct instance:
$realClass = MockWire::create(RealClass::class, [
new ThirdDependency()
]);The rest of argument will be mocked automatically.
This way we:
- can easily change the class constructor, without having burden of changing all the tests.
- see what is really being used in the constructor
- avoid any mock-mess clutter properties all over our test
Data beats guess. Do you need a quick idea how many files contain $this->get('...') calls? Or another anti-pattern you want to remove?
PhpStorm helps with similar search, but stops counting at 100+. To get exact data about your codebase, use this command:
vendor/bin/swiss-knife search-regex "#this->get\((.*)\)#"↓
Going through 1053 *.php files
Searching for regex: #this->get\((.*)\)#
1053/1053 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
* src/Controller/ProjectController.php: 15
* src/Controller/OrderController.php: 5
[OK] Found 20 cases in 2 files
The nelmio/alice package allows to use PHP for test fixture definitions. It's much better format, because Rector and PHPStan can understand it.
But what if we have 100+ YAML files in our project?
vendor/bin/swiss-knife convert-alice-yaml-to-php fixturesThat's it!
What is trait has 5 lines and used in single service? We know it's better to be inlined, to empower IDE, Rector and PHPStan. But don't have time to worry about these details.
We made a command to automate this process and spot the traits most likely to be inlined:
vendor/bin/swiss-knife spot-lazy-traits srcBy default, the commands look for traits used max 2-times. To change that:
vendor/bin/swiss-knife spot-lazy-traits src --max-used 4That's it! Run this command once upon a time or run it in CI to eliminate traits with low value to exists. Your code will be more robust and easier to work with.
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/devAfter - 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.
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-testsThe 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
);
}
}That's it!
Happy coding!