Clean code in PHP using object calisthenics

razielrodrigues

Raziel Rodrigues

Posted on May 13, 2024

Clean code in PHP using object calisthenics

Now you gonna put your code to do some push ups and pull ups, let's see how much your code can do!

Object Calisthenics provides invaluable guidelines for writing maintainable and comprehensible code. Let's explore each guideline within the context of PHP 8 to understand how they contribute to code quality and readability.

Image description

Clone the repository

git clone https://github.com/RazielRodrigues/30-tips-of-php

cd tip7/

Enter fullscreen mode Exit fullscreen mode

The guidelines

  1. Use only one level of indentation per method.
  2. Don’t use the else keyword.
  3. Wrap all primitives and strings.
  4. Use only one dot per line.
  5. Don’t abbreviate.
  6. Keep all entities small.
  7. Don’t use any classes with more than two instance variables.
  8. Use first-class collections.
  9. Don’t use any getters/setters/properties.

1. Use only one level of indentation per method.

it keeps more readable and helpful to understand what is happening, use the extract method stategy to achive that.

# Other
function moreIdentationFunction()
{
    $counter = [];
    for ($i = 0; $i < 10; $i++) { # level zero
        $counter[] = $i;

        foreach ($counter as $value) { # level one

            if ($value == 5) { # level two
                continue;
            }

            $counter[$value] = $value . ' result';
        }
    }
}

# Guideline
function lessIdentationFunction()
{
    $counter = [];

    for ($i = 0; $i < 10; $i++) { # level zero
        $counter[] = $i;
        $counter = generateResult($counter);
    }
}

function generateResult($counter)
{
    foreach ($counter as $value) { # level zero

        if ($value == 5) { # level one
            continue;
        }

        $counter[$value] = $value . ' result';
    }

    return $counter;
}
Enter fullscreen mode Exit fullscreen mode

2. Don’t use the else keyword.

Most of cases you could avoid the else keyword when you adapt your algorithm.

function processUser($user)
{
    if (!$user->isActive()) {
        echo 'User is inactive';
        return;
    }

    if ($user->age < 18) {
        echo 'Under 18';
        return;
    }

    if ($user->age >= 18 && $user->age < 60) {
        echo 'Adult';
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Wrap all primitives and strings.

Creating a class that encapsulates and provides a consistent interface for working with primitive values and strings.

class WrapAllPrimitives
{
    private $value;

    public function __construct($value)
    {
        if (!is_string($value) || !is_numeric($value) || !is_bool($value)) {
            throw new InvalidArgumentException('Value must be a string, number or boolean');
        }

        $this->value = $value;
    }

    public function getValue()
    {
        return $this->value;
    }
}

$stringWrapper = new WrapAllPrimitives('Hello, world!');
echo $stringWrapper->getValue();  // Outputs: Hello, world!

$numberWrapper = new WrapAllPrimitives(42);
echo $numberWrapper->getValue();  // Outputs: 42

$boolWrapper = new WrapAllPrimitives(true);
echo $boolWrapper->getValue();  // Outputs: 1
Enter fullscreen mode Exit fullscreen mode

4. Use only one dot per line

Avoid the break of the open closed principle and also avoid mysterious exceptions but don't aplly in fluent API, also is more easy to see on the debug stacktrace.

# Other
class PersonOther
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getName(): string|object
    {
        return $this->name;
    }

    public function getFormattedName(): string|object
    {
        return strtoupper($this->name);
    }
}

$personOther = new PersonOther('Charlie');

// Bad Example: Chained method calls with multiple dots in one line
$personOther->getName()->getFormattedName();

# Guideline
class PersonGuideline
{
    private $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getName(): string
    {
        return $this->name;
    }
}

class CarGuideline
{
    private $owner;

    public function __construct(PersonGuideline $owner)
    {
        $this->owner = $owner;
    }

    public function getOwnerName(): string
    {
        return $this->owner->getName();
    }
}

$personGuideline = new PersonGuideline('Alice');
$carGuideline = new CarGuideline($personGuideline);
$carGuideline->getOwnerName();
Enter fullscreen mode Exit fullscreen mode

5. Don’t abbreviate.

Explain what the method or the variable means, avoid redundant names on class methods.

# Other

$cpm = 199;

class UserOther
{

    # people know the Add is for user, it is redundant.
    function AddUser()
    {
    }
    function DeleteUser()
    {
    }
}

# Guideline

$costsPerMill = 199;

class UserGuideline
{

    # Keep the context
    function Add()
    {
    }
    function Delete()
    {
    }
}
Enter fullscreen mode Exit fullscreen mode

6. Keep all entities small.

Keep classes with 50 lines of code, doing that it shows your class is doing only one thing.

class FiftyLinesClass
{

    public function __construct()
    {
        throw new Exception('50 shades of code');
    }
}
Enter fullscreen mode Exit fullscreen mode

7. Don’t use any classes with more than two instance variables.

Your constructor should have only two injections not more than that also applies to local instances inside the class.

# Other
class TooMuchVariables
{

    public function __construct(
        private array $data1,
        private array $data2,
        private array $data3,
        private array $data4
    ) {
    }
}

# Guideline
class OnlyTwoVariables
{

    public function __construct(
        private array $data1,
        private array $data2
    ) {
    }
}
Enter fullscreen mode Exit fullscreen mode

8. Use first-class collections.

Encapsulate your dataset to avoid misusing of the values or actions that could happen, avoid breaking of business rules as well and directly manipulation of the instance.

# Other
class ClientCollectionOther
{

    private array $clientsData;

    public function __construct()
    {
        $this->clientsData = [
            1 => 'Morfeu',
            2 => 'Neo'
        ];
    }

    function get($id)
    {
        return $this->clientsData[$id];
    }

    function add(string $name)
    {
        return array_push($this->clientsData, $name);
    }
}

class RenderClientOther
{

    public function __construct(public ClientCollectionOther $clients)
    {
    }

    function show($id)
    {
        echo $this->clients->get($id);
    }

    function insert($name)
    {
        echo $this->clients->add($name);
    }
}

$render = new RenderClientOther((new ClientCollectionOther));
$render->show(2);
$render->insert('Trinity');
$render->show(3);

# Guideline
class ClientCollectionGuideline
{
    private array $clients;

    public function __construct()
    {
        $this->clients = [];
    }

    public function addClient(string $name): void
    {
        $this->clients[] = $name;
    }

    public function getClientById(int $id): ?string
    {
        return $this->clients[$id] ?? null;
    }

    public function getAllClients(): array
    {
        return $this->clients;
    }
}

class RenderClientGuideline
{
    private ClientCollectionGuideline $clients;

    public function __construct(ClientCollectionGuideline $clients)
    {
        $this->clients = $clients;
    }

    public function showClientById(int $id): void
    {
        $client = $this->clients->getClientById($id);
        if ($client !== null) {
            echo $client;
        }
    }

    public function insertClient(string $name): void
    {
        $this->clients->addClient($name);
    }
}

$clients = new ClientCollectionGuideline();
$clients->addClient('Morfeu');
$clients->addClient('Neo');

$render = new RenderClientGuideline($clients);
$render->showClientById(1);
$render->insertClient('Trinity');
$render->showClientById(2);
Enter fullscreen mode Exit fullscreen mode

9. Don’t use any getters/setters/properties.

Use methods to do the actions for you instead of the set or get methods.

# Other

class WithGetAndSet
{
    private int $score;

    public function __construct(int $score)
    {
        $this->score = $score;
    }

    public function getScore(): int
    {
        return $this->score;
    }

    public function setScore(int $score): void
    {
        $this->score = $score;
    }

    public function increase(): int
    {
        $this->score += 2;
        return $this->score;
    }

    public function decrease(): int
    {
        $this->score -= 2;
        return $this->score;
    }
}

# Guideline
class WithouttGetAndSet
{
    private int $score;

    public function __construct(int $score)
    {
        $this->score = $score;
    }

    public function increase(): int
    {
        $this->score += 2;
        return $this->score;
    }

    public function decrease(): int
    {
        $this->score -= 2;
        return $this->score;
    }
}

Enter fullscreen mode Exit fullscreen mode

Those guidelines are good to give us directions on how to code better and more cleaner, in my opinion coding is something that needs to be empathy, following some of those guidelines help others and yourself to understand your code better.

Some guidelines can become natural and easy to apply, specially because you see a lot of people using it.

My opinion

Use only one level of indentation per method.
R: This one I consider important to follow because it leads to function extraction providing a better flow in your code.

Don’t use the else keyword.
R: Also easy to apply if you look again to your logic and for sure keeps the code more clean and with less complexity, I like to use the strategy fail first.

Wrap all primitives and strings.
R: Depends on the project architecture, then I should say is not a must.

Use only one dot per line.
R: Also other one that depends, I personally like the idea but sometimes add complexity.

Don’t abbreviate.
R: This is a must, only follow.

Keep all entities small.
R: okay 50 lines sometimes is difficult but 500 is too much, you need to find your average.

Don’t use any classes with more than two instance variables.
Use first-class collections.
R: Again you need to see if is necessary, so I should say keep the average.

Don’t use any getters/setters/properties.
R: I don't agree with "don't" for this one, since is not so bad to have anemic entities, also we have the fluent interface and other patterns that is good to provide those methods.

💖 💪 🙅 🚩
razielrodrigues
Raziel Rodrigues

Posted on May 13, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related