Skip to content
This repository was archived by the owner on Jul 16, 2021. It is now read-only.
This repository was archived by the owner on Jul 16, 2021. It is now read-only.

Better comments in the code #1579

@halaei

Description

@halaei

There are places in Laravel code that I wish for better comments. To me better is not necessarily more, but it is most of the time. I try to list some cases here. Examples are from laravel/horizon unless stated otherwise.

1. Architectural documentation

when you run artisan horizon, an instance of a master supervisor is created. Then, the master supervisor runs some artisan horizon:supervise commands to make supervisor processes. Each supervisor process runs some artisan horizon:work commands to create some worker processes. In other words master supervisor creates, monitors, controls and kills some supervisors and each supervisor creates, monitors, controls and kills some workers.

The problem

You only can know about this very high-level architecture by reading the code. To me as a user it is not a problem at first. The trouble starts when the current state of the code doesn't meet my specific needs in a project. Now I have to fix horizon, and in order to fix it, the first thing I need to know is to figure out the high level architecture. Please note that the terms master and supervisor are used in laravel horizon docs but they are not defined.

2. Classes needs to have comments

For supervisor, we have 2 classes: Supervisor and SupervisorProcess. It would make my understanding of code faster if the classes have some comments mentioning their responsibilities and relations with other classes so that I can easily understand them:

/**
 * A supervisor process is an object created by the master supervisor that contains
 * one child process which has a single supervisor object.
 *
 * @see MasterSupervisor
 * @see Supervisor
 */
class SupervisorProcess extends WorkerProcess
/**
 * A supervisor object manages a set of process pools each containing many worker process objects.
 *
 * @see ProcessPool
 * @see WorkerProcess
 */
class Supervisor implements Pausable, Restartable, Terminable

3. Don't document the obvious

The following comments about what __destruct() doesn't add value. Empty spaces will do better:

    /**
     * Destruct the object.
     *
     * @return void
     */
    public function __destruct()
    {
        //
    }

In this example, a documentation better than nothing is a why documentation, which is in the following section.

4. More why documentations

An empty __destruct() is probably there to disable the parent destructor:

class BackgroundProcess extends Process
{
    /**
     * An empty __destruct() to prevent the parent destructor from killing the child process.
     *
     * @return void
     */
    public function __destruct()
    {
        //
    }
}

For another example, Horizon\RedisQueue extending from Illuminate\Queue\RedisQueue and overrides getRandomId() function. The comments on this method are just a copy paste from the parent.

    /**
     * Get a random ID string.
     *
     * @return string
     */
    protected function getRandomId()
    {
        return JobId::generate();
    }

But a better comment would be the reason behind overriding, which I can't figure out yet:

    /**
     * Returns an auto-increment integer id instead of random string.
     * Horizon needs job ids to be auto-incremental because **I DON'T KNOW WHY**.
     *
     * @return string
     */
    protected function getRandomId()
    {
        return JobId::generate();
    }

5. Type-hinting closure parameters

This is not something entirely missing in Laravel, but I wish for more.

        return collect($this->processes)->filter(function (WorkerProcess $process) {
            return $process->process->isRunning();
        });

6. Inline docblocks to type hint local variables

Apart from tests, I have seen zero inline phpdoc for local variables in Laravel sources. I have no specific example for this one, but sometimes I feel Laravel core developers are against inline dockblocks. Maybe they have some good reason for that, or I am totally wrong on this case.

7. Type hint array/collection elements

Basically, string[] is more useful than array. It is also nice to write Collection|ProcessPool[] instead of just Collection:

    /**
     * All of the active processes.
     *
     * @var WorkerProcess[]
     */
    public $processes = [];
    /**
     * All of the process pools being managed.
     *
     * @var \Illuminate\Support\Collection|ProcessPool[]
     */
    public $processPools;

7. Documenting magic properties

Maybe this is also not entirely missing, but it is missing in many model classes across the laravel projects. Here is an example in laravel/passport:

/**
 * @property int $id
 * @property int $user_id
 * @property int $client_id
 * @property string $name
 * @property string $scopes
 * @property bool $revoked
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property Carbon $expires_at
 *
 * @property-read Passport $client
 * @property-read $user
 */
class Token extends Model

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions