When I started PHP in 2004, all you had to do is to learn a few functions to become the most senior dev in your town. Nowadays, devs have to learn a framework, IDE and coding patterns to get at least to an average level.
Instead of reading 346 pages of Clean Code, you need to produce code and learn as you read it at the same time. There will be never less information than it is today.
That's why effective learning is a killer skill. Today we learn how to sort private methods in 2 mins.
This simple class is in your code now:
<?php
class SomeClass
{
public function run()
{
$this->call1();
$this->call2();
}
private function call3()
{
}
private function call2()
{
}
private function call1()
{
$this->call3();
}
}
Thanks to Gregor Harlan, there is a coding standard OrderedClassElementsFixer
, that already takes care about public
/protected
/private
elements order.
But what about these private methods - are they ok for you?
When you read such a code for the firs time, you might think:
private function call3()
- ah, it does this and that... but wait, who is using it?private function call2()
- it does this and that, it was called in run()
methodprivate function call1()
- it does this and... the call3()
is used here, yes... what it actually did?"Be able to read down the file from top to bottom like a newspaper article,
which would naturally suggest that helper methods appear after the methods they are helping.
This would lead to maximum readability of the code structure."
It's easy to spot and correct the example above...
<?php
class SomeClass
{
public function run()
{
$this->call1();
$this->call2();
}
private function call1()
{
$this->call3();
}
private function call2()
{
}
private function call3()
{
}
}
...but in real life pull-requests are usually longer than 20 lines:
Taken from my 5 days-old-PR
Nor junior nor senior dev is able to check the proper private method order in these big chunks of code.
...somebody or something does it for you.
"Automate everything that brings you more value until you die
compared to value lost to create it."
There is now a Rector rule that does exactly what we need in an automated way:
composer require rector/rector --dev
<?php
// rector.php
declare(strict_types=1);
use Rector\Order\Rector\Class_\OrderPrivateMethodsByUseRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderPrivateMethodsByUseRector::class);
};
And run Rector:
vendor/bin/rector process src
At first, you might need to re-run the command few times, because of the new order of private methods will automatically change calling order. But that's it:
Let finish with Martin Fowler quote:
"Any fool can write code that a computer can understand.
Good programmers write code that humans can understand."
I think we're coming to times, where:
"Any computer can write a code that humans can understand."
Happy coding!