Examples are written in PHP, but should be followed for all relevant languages.
Groups include and should be in the following order:
- Third party modules (vendor/node_modules)
- Absolute imports
- Relative imports
- Aliased imports
❌ Incorrect:
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use App\Models\Traits\SoftDeletes\SoftDeletes;
use ReflectionMethod;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use ReflectionClass;
use Carbon\Carbon;
use App\Models\HistoryPivotChangeAttribute;
use App\Models\History;
use Auth;
✔️ Correct:
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Carbon\Carbon;
use App\Models\Traits\SoftDeletes\SoftDeletes;
use App\Models\HistoryPivotChangeAttribute;
use App\Models\History;
use ReflectionMethod;
use ReflectionClass;
use Auth;
❌ Incorrect:
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
✔️ Correct:
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
❌ Incorrect:
function add(int $x, int $y): int {
return $x + $y;
}
✔️ Correct:
function add(int $x, int $y): int {
return $x + $y;
}
❌ Incorrect:
function add(int $x, int $y): int
{
return $x + $y;
}
✔️ Correct:
function add(int $x, int $y): int {
return $x + $y;
}
❌ Incorrect:
function add(int $x, int $y): int {
$result = $x + $y;
return $result;
}
function subtract(int $x, int $y): int {
$result = $x - $y;
return $result;
}
✔️ Correct:
function add(int $x, int $y): int {
$result = $x + $y;
return $result;
}
function subtract(int $x, int $y): int {
$result = $x - $y;
return $result;
}
❌ Incorrect:
function add($x, $y) {
return $x + $y;
}
✔️ Correct:
function add(int $x, int $y): int {
return $x + $y;
}
❌ Incorrect:
function appendGreeting(string $name): string {
return "Hey there, " . $name;
}
✔️ Correct:
function appendGreeting(string $name): string {
return 'Hey there, ' . $name;
}
❌ Incorrect:
$str='hello'+'world';
✔️ Correct:
$str = 'hello' + 'world';
❌ Incorrect:
$arr = ['hello'] + ['world'];
$str = 'hello' + 'world';
✔️ Correct:
$arr = array_merge(['hello'], ['world']);
$str = 'hello' . 'world';
❌ Incorrect:
function validate(mixed $x): bool {
if (is_null($x)) return false;
elseif (is_string($x) return false;
else return true;
}
✔️ Correct:
function validate(mixed $x): bool {
if (is_null($x)) return false;
if (is_string($x)) return false;
return true;
}
❌ Incorrect:
function validate_a_variable(mixed $the_value): bool {
$is_valid = true;
if (is_string($the_value)) {
$is_valid = false;
};
return $is_valid;
}
✔️ Correct:
function validateAVariable(mixed $theValue): bool {
$isValid = false;
if (is_string($theValue)) {
$isValid = true;
};
return $isValid;
}
❌ Incorrect:
function calculateCircumference(float $radius): float {
return 2 * 3.141592654 * $radius;
}
✔️ Correct:
const PI = 3.141592654;
function calculateCircumference(float $radius): float {
return 2 * PI * $radius;
}
❌ Incorrect:
const pi = 3.141592654;
function calculateCircumference(float $radius): float {
return 2 * pi * $radius;
}
✔️ Correct:
const PI = 3.141592654;
function calculateCircumference(float $radius): float {
return 2 * PI * $radius;
}
❌ Incorrect:
$table->string('firstName');
✔️ Correct:
$table->string('first_name');
Secret values (passwords, salts, encryption keys) should NEVER be committed to GitHub and should be stored in an environment (.env) file
❌ Incorrect:
$secret = 'secret_value';
✔️ Correct:
$secret = config('app.secret_value');