Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript coding standards #113

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Split global and PHP coding standards
cconard96 committed Feb 12, 2022
commit e7a2644caee7f659405de8b2fc8128bc03c76668
2 changes: 1 addition & 1 deletion source/checklists/reviewprocess.rst
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ Here is the process you must follow when you are reviewing a PR.

2. Check if unit tests are not failing,
3. Check if coding standards checks are not failing,
4. Review the code itself. It must follow :doc:`GLPI's coding standards <../codingstandards>`,
4. Review the code itself. It must follow :doc:`GLPI's coding standards <../coding_standards/index>`,
5. Using the Github review process, approve, request changes or just comment the PR,

* If some new methods are added, or if the request made important changes in the code, you should ask the developer to write some more unit tests
90 changes: 90 additions & 0 deletions source/coding_standards/global.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Global Coding standards
=======================

Indentation
-----------

- 3 spaces
- Max line width: 100

.. code-block:: php
<?php
// base level
// level 1
// level 2
// level 1
// base level
Spacing
-------

We've adopted "french spacing" rules in the code. The rule is:

* for *simple* punctuation (``,``, ``.``): use *one space after* the punctuation sign
* for *double* punctuation (``!``, ``?``, ``:``): use *one space after and one space before* the punctuation sign
* for *opening* punctuation (``(``, ``{``, ``[``): use *one space before* the punctuation sign
* for *closing* punctuation ( ``)``, ``}``, ``]``): use *one space after* the punctuation sign, excepted for line end, when followed by a semi-colon (``;``)

Of course, this rules only aplies on the source code, not on the strings (translatable strings, comments, ...)!

Control structures
------------------

Multiple conditions in several idented lines

.. code-block:: php
<?php
if ($test1) {
for ($i=0 ; $i<$end ; $i++) {
echo "test ".( $i<10 ? "0$i" : $i )."<br>";
}
}
if ($a==$b
|| ($c==$d && $e==$f)) {
...
} else if {
...
}
switch ($test2) {
case 1 :
echo "Case 1";
break;
case 2 :
echo "Case 2";
// No break here : because...
default :
echo "Default Case";
}
true, false and null
--------------------
``true``, ``false`` and ``null`` constants mut be lowercase.
Variables and Constants
-----------------------
* Variable names must be as descriptive and as short as possible, stay clear and concise.
* In case of multiple words, use the ``_`` separator,
* Variables must be **lower case**,
* Global PHP variables and constants must be **UPPER case**.
.. code-block:: php
<?php
$user = 'glpi';
// put elements in alphabetic order
$users = ['glpi', 'glpi2', 'glpi3'];
$users = ['glpi1' => 'valeur1',
'nexglpi' => ['down' => '1',
'up' => ['firstfield' => 'newvalue']],
'glpi2' => 'valeur2'];
$users_groups = ['glpi', 'glpi2', 'glpi3'];
$CFG_GLPI = [];
10 changes: 10 additions & 0 deletions source/coding_standards/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Coding standards
================

GLPI has a general set of coding standards and then additional requirements for specific languages/file types (PHP, JavaScript, Twig, etc).

.. toctree::
:maxdepth: 2

global.rst
php.rst
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ Coding standards

As of GLPI 10, we rely on `PSR-12 <https://www.php-fig.org/psr/psr-12/>`_ for coding standards.


Call static methods
-------------------

@@ -40,7 +39,6 @@ When you do not have any object instance yet; the first solution is probably the

On the other hand; if you already have an object instance; you should better use any of the solution but the late static binding. That way; you will save performances since this way to go do have a cost.


Comments
--------

2 changes: 1 addition & 1 deletion source/index.rst
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ GLPI Developer Documentation
:maxdepth: 2

sourcecode
codingstandards
coding_standards/index
devapi/index
checklists/index
plugins/index
2 changes: 1 addition & 1 deletion source/plugins/requirements.rst
Original file line number Diff line number Diff line change
@@ -237,7 +237,7 @@ For instance, a plugin need both an install and an uninstall hook calls. Here is
Coding standards
^^^^^^^^^^^^^^^^

You must respect GLPI's :doc:`global coding standards <../codingstandards>`.
You must respect GLPI's :doc:`global coding standards <../coding_standards/index>`.

In order to check for coding standards compliance, you can add the `glpi-project/coding-standard` to your composer file, using: