General
See Laravel General for general Laravel development information.
Return Types
- Always specify return types for functions and methods
- Use
voidfor functions that do not return a value
Typed Properties
- Always specify types for properties
- Use
mixedwhen the type is unknown - Use
?Typefor nullable types
Type Declarations
- Always specify type declarations for function and method parameters
- Use
mixedwhen the type is unknown - Use
?Typefor nullable types
Example
function sum(int $a, int $b): int
{
return $a + $b;
}
Type Casting
- Use type casting when necessary
- Use the
(int),(float),(string),(bool),(array),(object),(unset)functions for type casting - Do not use the php functions such as
intval()
Example
$age = (int) $age;
$price = (float) $price;
Comments
- Should be avoided where possible
- Use comments to explain code that is not self-explanatory
- Comments should be clear, concise, and written in complete sentences
- Use comments to explain the "why" behind the code, not the "what"
Docblocks
- Do not use docblocks unless absolutely necessary
- Do use when documenting the contents of an array or collection