devel:coding_style
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
devel:coding_style [2019-01-07 20:30] – [Checking for Coding Style Violations] Add 3.x standards path phy25 | devel:coding_style [2023-11-13 21:13] (current) – [Checking for Coding Style Violations] andi | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Coding Style ====== | ====== Coding Style ====== | ||
- | There are currently no really strict rules in how the code should be formatted, but a few basic things should be attended | + | DokuWiki is a very old project and coding styles vary within |
+ | |||
+ | **The official coding style to apply to all new code is [[https:// | ||
+ | |||
+ | When editing existing code you're encouraged | ||
- | It should be noted that for the DokuWiki core applying of the PSR2 coding style is work in progress (branch '' | ||
===== Brackets and Indentations ===== | ===== Brackets and Indentations ===== | ||
- | You should indent your code by 4 **spaces** to mark logical blocks. Please **do not use tabs**. | + | You should indent your code by 4 **spaces** to mark logical blocks. Please **do not use tabs**. |
Opening brackets should start on the same line as the keyword, closing bracket should be aligned below the first letter of the starting keyword. E.g.: | Opening brackets should start on the same line as the keyword, closing bracket should be aligned below the first letter of the starting keyword. E.g.: | ||
Line 20: | Line 23: | ||
</ | </ | ||
+ | |||
+ | Refer to PSR-12 for more details. | ||
===== Line Endings ===== | ===== Line Endings ===== | ||
- | Lines should end with a single linefeed character (UNIX style). Please try to avoid trailing whitespace. Have a look at our page about [[Vim]] to see how to set up Vim to spot these easily. | + | Lines should end with a single linefeed character (UNIX style). Please try to avoid trailing whitespace. Have a look at our page about [[Vim]] to see how to set up Vim to spot these easily. [[IntelliJ Idea]] will also help with keeping a consistent style. |
===== Commenting ===== | ===== Commenting ===== | ||
- | Each function and class should have a PHPDocumentor style comment, giving at least the function' | + | Each function and class should have a PHPDocumentor style comment, giving at least the function' |
+ | |||
+ | Author lines are optional, since we track authorship in git anyway. | ||
Example: | Example: | ||
Line 43: | Line 50: | ||
* | * | ||
*/ | */ | ||
- | function is_foo($in) { | + | function is_foo($in) |
+ | { | ||
... | ... | ||
} | } | ||
Line 49: | Line 57: | ||
These comments are used for the [[xref> | These comments are used for the [[xref> | ||
+ | |||
===== PHP Closing Tags ===== | ===== PHP Closing Tags ===== | ||
Line 55: | Line 64: | ||
> **Note:** The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() | > **Note:** The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() | ||
- | ===== Visibility ===== | + | ===== Visibility |
- | + | ||
- | Even though some core code still uses PHP4 compatible '' | + | |
Please do not use '' | Please do not use '' | ||
- | ===== Checking | + | When using type hints be aware of the minimal supported PHP version |
- | The development [[Git]] checkout contains a coding standard setup for the use with [[http:// | + | In general be wary of overusing type hinting, especially in all public facing APIs. Changing it changes method signatures which has effects on all inheriting classes. A simple type hint change may break swathes of plugins |
- | === Setup === | + | ===== Checking for Coding Style Violations ===== |
- | To install | + | Code styles can be checked with [[https:// |
- | pear install PHP_CodeSniffer | + | === Setup === |
- | + | ||
- | Link the DokuWiki coding standard to the CodeSniffer directory. | + | |
- | + | ||
- | # PHP_CodeSniffer Version 2.x | + | |
- | ln -s / | + | |
- | + | ||
- | # PHP_CodeSniffer Version 3.x | + | |
- | ln -s / | + | |
- | Set DokuWiki to be the default standard. | + | Install via [[composer]]: |
- | | + | |
+ | composer install | ||
- | Note that the provided DokuWiki coding standard is for PHP_CodeSniffer 2.x. PHP_CodeSniffer 3.x has breaking changes which result in errors indicating that the interface '' | ||
=== Usage === | === Usage === | ||
Line 90: | Line 89: | ||
# Check a single file | # Check a single file | ||
- | | + | |
+ | composer check ../path/to/myfile.php | ||
| | ||
# Check all files in a directory | # Check all files in a directory | ||
- | | + | |
+ | composer check ../ | ||
devel/coding_style.1546889432.txt.gz · Last modified: 2019-01-07 20:30 by phy25