2016-08-24 08:12:20 +01:00
|
|
|
source: Developing/Code-Guidelines.md
|
2018-10-27 23:04:34 +01:00
|
|
|
path: blob/master/doc/
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2015-04-05 01:15:52 +01:00
|
|
|
# Coding guidelines
|
2015-02-14 18:06:55 +00:00
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
This document is here to help code standards for contributions towards
|
|
|
|
LibreNMS. The original code base that we forked from had a lack of
|
|
|
|
standards and as such the code base has a variety of different
|
|
|
|
styles. Whilst we don't want to restrict how people write code, these
|
|
|
|
guidelines should mean we have a good standard going forward that
|
|
|
|
makes reading the code easier. All modern day ide's should be able to
|
|
|
|
assist in these guidelines without breaking your usual workflow.
|
2015-02-14 18:06:55 +00:00
|
|
|
|
2016-07-05 11:14:19 -05:00
|
|
|
## PHP-FIG PSR-2 Coding Style
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2016-07-05 11:14:19 -05:00
|
|
|
All new code should follow the [PHP-FIG PSR-2 standard](http://www.php-fig.org/psr/psr-2/).
|
2019-06-20 13:53:45 -05:00
|
|
|
Below are a few key items from that specification, please make sure to
|
|
|
|
follow the full spec.
|
2016-07-05 11:14:19 -05:00
|
|
|
|
|
|
|
### [Indentation](http://www.php-fig.org/psr/psr-2/#2-4-indenting)
|
2019-06-20 13:53:45 -05:00
|
|
|
|
|
|
|
Please use four (4) spaces to indent code rather than a tab. Ensure
|
|
|
|
you increase indentation for nested code blocks.
|
|
|
|
|
2015-02-14 18:06:55 +00:00
|
|
|
```php
|
|
|
|
if ($foo == 5) {
|
|
|
|
if ($foo == 5) {
|
|
|
|
if ($foo == 5) {
|
|
|
|
```
|
|
|
|
|
2016-07-05 11:14:19 -05:00
|
|
|
### [Line length](http://www.php-fig.org/psr/psr-2/#1-overview)
|
2019-06-20 13:53:45 -05:00
|
|
|
|
|
|
|
Try to keep the length of a line under 80 characters. If you must
|
|
|
|
exceed 80 characters, please keep it under 120 characters. This makes
|
|
|
|
reading the code easier and also enables compatibility for all screen sizes.
|
2015-02-14 18:06:55 +00:00
|
|
|
|
2016-07-05 11:14:19 -05:00
|
|
|
### [Control structures](http://www.php-fig.org/psr/psr-2/#5-control-structures)
|
2019-06-20 13:53:45 -05:00
|
|
|
|
|
|
|
A space must be used both before and after the parenthesis and also
|
|
|
|
surrounding the condition operator.
|
|
|
|
|
2015-02-14 18:06:55 +00:00
|
|
|
```php
|
|
|
|
if ($foo == 5) {
|
|
|
|
```
|
|
|
|
|
2016-07-05 11:14:19 -05:00
|
|
|
Do not put blocks of code on a single line, do use parenthesis
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2015-02-14 18:06:55 +00:00
|
|
|
```php
|
|
|
|
if ($foo == 5) {
|
|
|
|
echo 'foo is 5';
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-07-05 11:14:19 -05:00
|
|
|
else and elseif should start on the same line as ending of the previous code block.
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2015-07-10 21:22:40 +10:00
|
|
|
```php
|
|
|
|
if ($foo == 5) {
|
|
|
|
echo 'foo is 5';
|
2016-07-05 11:14:19 -05:00
|
|
|
} elsif ($foo == 4) {
|
2015-07-10 21:22:40 +10:00
|
|
|
echo 'foo is 4';
|
2016-07-05 11:14:19 -05:00
|
|
|
} else {
|
2015-07-10 21:22:40 +10:00
|
|
|
echo 'foo is something else';
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-02-14 18:06:55 +00:00
|
|
|
### Including files
|
2019-06-20 13:53:45 -05:00
|
|
|
|
|
|
|
Using parenthesis around file includes isn't required, instead just
|
|
|
|
place the file in between ''
|
|
|
|
|
2015-02-14 18:06:55 +00:00
|
|
|
```php
|
|
|
|
require_once 'includes/snmp.inc.php';
|
|
|
|
```
|
|
|
|
|
2016-07-05 11:14:19 -05:00
|
|
|
### [PHP tags](http://www.php-fig.org/psr/psr-1/#1-overview)
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2015-02-14 18:06:55 +00:00
|
|
|
Ensure you use <?php rather than the shorthand version <?.
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2015-02-14 18:06:55 +00:00
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
```
|
2015-05-05 22:17:22 +01:00
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
The `?>` [must be
|
|
|
|
excluded](http://www.php-fig.org/psr/psr-2/#2-2-files) from all files
|
|
|
|
that only include PHP (no html). For instance anything in includes/ or
|
|
|
|
html/includes don't need the tag along with config.php.
|