AlumnForce / coding-conventions

[DEPRECATED] Style guide & coding conventions for AlumnForce projects
Creative Commons Attribution 4.0 International
11 stars 2 forks source link
code-style mysql naming-conventions php style-guide

Deprecated

This repository is deprecated since we follow PSR 1 and PSR 2 recommandations

AlumnForce Coding Conventions

Table of Contents

MySQL

Schemas

Queries

Patches

Because IF EXISTS doesn't exist for many DDL statements we need to encapsulate some SQL instructions in a stored procedure.

Systematically make sure to remove the possible existing in order to reconstruct and guarantee the expected result: for example make a DROP INDEX before the CREATE INDEX because perhaps it does not target the same columns.

Approach adopted after vote:

    DROP PROCEDURE IF EXISTS clean;

    DELIMITER //
    CREATE PROCEDURE clean()
    BEGIN
        SET @exists = (
            SELECT count(*)
            FROM information_schema.statistics
            WHERE table_name = 'bounce' AND index_name = 'campaign_id' AND table_schema = database()
        );
        IF @exists > 0 THEN
            DROP INDEX campaign_id ON bounces;
        END IF;
    END //
    DELIMITER ;

    CALL clean();
    DROP PROCEDURE IF EXISTS clean;

    -- True payload:
    CREATE INDEX campaign_id ON bounce (campaign_id);

PHP

General

We follow the standards defined in the PSR-0, PSR-1 and PSR-2 documents.

Composer

Notation

OOP

PHPDoc

Practical cases

On-the-fly assignment

On-the-fly assignment must be avoided because not very readable: if (($myId = $obj->getById())) {...}.

Write:

    $myId = $obj->getById();
    if ($myId !== null) {
        // ...
    }

Implicit if

Implicit if must be avoided because not very readable:

Write:

    if (<expression>) {
        <statement>
    }

Approximate test

Let: if (\ === true) {...}, if <expression> is of boolean type, then simply write: if (<expression>) {…}.

If <expression> is not a boolean then be specific, e.g. if (preg_match(...) > 0) {...}

Switch hack

To avoid: switch (true) {...}

If case expressions are not deductible from the same variable, then use if/elseif.

Remote return

Instead of:

    if ($expression) {
        return $foo;
    }

    // many lines...

    return $bar;

Write:

    if ($expression) {
        return $foo;
    } else {
        // many lines...
        return $bar;
    }

Or better, only one return if possible:

    if ($expression) {
        $value = $foo;
    } else {
        // many lines...
        $value = $bar;
    }

    return $value;

Mixed return types

For a given function, do not use more than one data type for return value.

Bad, because of the mixture between array and int:

    /**
     * @return array|int
     */
    function (…) {
        if (…) {
            return [1, 2, 3];
        } else {
            return 5;
        }
    }

Rewrite as follows:

    /**
     * @return array
     */
    function (…) {
        if (…) {
            return [1, 2, 3];
        } else {
            return [5];
        }
    }

Emptyness

Redundant: if (! isset($params['foo']) || empty($params['foo'])) {…}.

This is equivalent to: if (empty($params['foo'])) {…}.

Variable name differentiation

Not very readable: foreach ($items as $item) {…}.

For example, improve it this way: foreach ($allItems as $item) {…}.