Skip to content

Instantly share code, notes, and snippets.

@saltandvinegarcrisps
Last active November 28, 2017 16:13
Show Gist options
  • Save saltandvinegarcrisps/9b3c03a66c9a88210b6b973f8fe79f58 to your computer and use it in GitHub Desktop.
Save saltandvinegarcrisps/9b3c03a66c9a88210b6b973f8fe79f58 to your computer and use it in GitHub Desktop.
Rules

Code Style Guide

PHP

  • Features that have been added in version 7 should be evident in code submitted.
    • Scalar type declarations
    • Return type declarations
    • Null coalescing operator
  • Code will be formated using the PSR-2 standard
  • Functions will not be indented more than 3 times and less than 100 lines. The example below would be rejected
function whatsMyAgeAgain(string $name, string $friend): int
{
    if($name != 'admin') {
        if($friend != 'admin') {
            $guess = getAgeGuess($name, $friend);
            if($guess > 1) {
                if($guess < 100) {
                    return $guess;
                }
            }
        }
    }
}
  • Data validation will not be nested
function nameIsValid(string $name): bool
{
    if(strlen($name) > 3) {
        if(strlen($name) < 20) {
            return true;
        }
    }

    return false;
}

Should be refactored into ...

function nameIsValid(string $name): bool
{
    if(strlen($name) < 3) {
        return false;
    }

    if(strlen($name) > 20) {
        return false;
    }

    return true;
}
  • Functions will be type hinted where ever possible
function assemble(CarInterface $car, EngineInterface $engine, TransmissionInterface $transmission, ColorInterface $color): Car
{
    $engine->with($transmission);
    $car->engine($engine)->colour($colour);
    return $car;
}
  • Docblocks on functions and properties, the very minimum a description
  • Function and Class names shall be camel case
class SomeClass
{
    abstract public function makeSomeThingAwesome(): SomeAwesomeThing;
}
  • Input parameters shall be snake case
{
    "snake_case_readable_in_data": "This is how it goes"
}
  • Exceptions should be used at every posible case
function fetchUserById(int $id): UserInterface
{
    if($id < 0) {
        throw new InvalidArgumentException('User ID must be a positive integer');
    }
    
    $user = $this->repo->find($id);
    
    if(! $user) {
        throw new RuntimeException('User not found');
    }
    
    return $user;
}
  • Tests will be written in PHPSpec or PHPUnit
  • Composer should always be constrained to PHP7
{
    "require": {
        "php": ">=7"
    }
}

Database

  • Database changes will always be written in migrations
  • Date, Datetime, Timestamp will always be DateTime::W3C format, e.g. 2005-08-15T15:52:01+00:00 or date('Y-m-d\TH:i:sO')
  • Booleans shall be integers (TINYINT) of 1 or 0 (not strings of "yes", "no", "maybe", "perhaps" or "sorta")
  • Character Set will always be utfmb4
  • String, Varchar, Char length will not exceed 191 characters (maximum multibyte string to fit into 256 bytes datatype)

Repos

  • All projects will contain a .editorconfig file
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.{js,yml}]
indent_size = 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment