Development: PHP

Coding Style

The PHP code style MUST follow the laravel coding style. This includes the PSR-2 coding standart and the PSR-4 autoloading standard.

A different coding standard can be applied if a development environment in a framework different from laravel requires it.

Types

Original discussion

All methods and functions MUST be type hinted as much as possible.In packages, however, care must be taken to ensure that typehints do not restrict inheritance and overwriting of classes for other uses.

PHPDoc

All methods MUST have docblocks that:

  • MUST contain a full sentence describing the purpose of the method.
  • SHOULD contain links to additional resources
  • MUST contain all parameter types and names
  • SHOULD contain parameter descriptions (when the name is may not be self explaining)
  • MUST contain the return type
  • MUST contain all exception that are thrown directly by the method
  • MUST NOT contain exceptions that are thrown by other methods on a higher level

The following shows an example class method and a corresponding docblock:

/** * Register a binding with the container. * * @see https://example.org * * @param  string|array          $abstract  The abstract that the binding is named after * @param  \Closure|string|null  $concrete * @param  bool                  $shared * @return void * * @throws \Exception */public function bind($abstract, $concrete = null, $shared = false){    //}

Helpers

Helpers are files that contain functions that are not wrapped inside of a class.

  • File names of helpers MUST be kebab-case.
  • The number of helpers within a project SHOULD be reduced to the minimum. (It is recommended to have one helper named helpers.php)
  • Helper functions MUST wrap existing methods and MUST NOT introduce new logic.
  • The purpose of helper methods MUST be solely to increase the readability of the code.

Read the tutorial about how to register helpers.