DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:coding_style

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
devel:coding_style [2010-07-19 22:34] – old revision restored achdevel:coding_style [2019-01-06 10:21] – [Coding Style]: added remark about PSR2 LarsDW223
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 to when adding code to DokuWiki.+There are currently no really strict rules in how the code should be formattedbut a few basic things should be attended to when adding code to DokuWiki.
  
 +It should be noted that for the DokuWiki core applying of the PSR2 coding style is work in progress (branch ''psr2''). The rules of this coding style can be viewed at https://www.php-fig.org/psr/psr-2/.
 ===== Brackets and Indentations ===== ===== Brackets and Indentations =====
  
Line 10: Line 11:
  
 <code php> <code php>
-if ($foo == "bar"){+if ($foo == "bar") {
     call_bar();     call_bar();
-}elseif($foo == "baz"){+} elseif ($foo == "baz") {
     call_baz();     call_baz();
-}else{+} else {
     call_other();     call_other();
 } }
Line 20: Line 21:
 </code> </code>
  
-===== Lineendings =====+===== Line Endings =====
  
-Lines should end with a single linefeed character (UNIX style). Please try to avoid trailing whitespace. Have a look at my [[notes>vimrc]] file on how to spot it easily in VIM.+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.
  
  
 ===== Commenting ===== ===== Commenting =====
  
-Each function and class should have a PHPDocumentor style comment, giving at least the function's purpose and its author. Parameter and return value descriptions are nice but only mandatory if they are not obvious. If you enhance an existing function just add another author line.+Each function and class should have a PHPDocumentor style comment, giving at least the function's purpose and its author. Parameter and return value descriptions are encouraged but only mandatory if they are not obvious. If you enhance an existing function just add another author line.
  
 Example: Example:
Line 39: Line 40:
  * @author   Joe Schmoe <joe@example.com>  * @author   Joe Schmoe <joe@example.com>
  * @param    string $in your input  * @param    string $in your input
- * @returns  boolean    true if foo in bar+ * @return   bool       true if foo in bar
  *  *
  */  */
-function is_foo($in){ +function is_foo($in) { 
-...+    ...
 } }
 </code> </code>
  
-These comments are used for the [[http://dev.splitbrain.org/reference/dokuwiki/|autogenerated API Docs]]. +These comments are used for the [[xref>index|autogenerated API Docs]]. 
- +===== PHP Closing Tags =====
-===== PHP closing tags =====+
  
 You should omit PHP's closing tags (''?>'') in all files to avoid premature output. This may sound strange but is actually mentioned in the [[http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php|PHP manual]]: You should omit PHP's closing tags (''?>'') in all files to avoid premature output. This may sound strange but is actually mentioned in the [[http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php|PHP manual]]:
  
-**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()  or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.+**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()  or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files
 + 
 +===== Visibility ===== 
 + 
 +Even though some core code still uses PHP4 compatible ''var'' declarations you're encouraged to use proper PHP5 visibility declarations. 
 + 
 +Please do not use ''private'' but use ''protected'' instead. You never know when a subclass may want to overwrite functionality. Eg. some plugin might want to provide new functionality based on your plugin.
  
 ===== Checking for Coding Style Violations ===== ===== Checking for Coding Style Violations =====
  
-The development [[git]] checkout contains a coding standard setup for the use with [[http://pear.php.net/package/PHP_CodeSniffer|PHP Code Sniffer]] in ''_cs/DokuWiki''.+The development [[Git]] checkout contains a coding standard setup for the use with [[http://pear.php.net/package/PHP_CodeSniffer|PHP_CodeSniffer]] in ''_cs/DokuWiki''. 
 + 
 +=== Setup === 
 + 
 +To install PHP_CodeSniffer run the following in your shell. 
 + 
 +    pear install PHP_CodeSniffer 
 + 
 +Link the DokuWiki coding standard to the CodeSniffer directory.  You may need to adjust the paths depending upon your operating system. 
 + 
 +    ln -s /path/to/dokuwiki/_cs/DokuWiki /usr/share/pear/PHP/CodeSniffer/Standards/DokuWiki 
 + 
 +Set DokuWiki to be the default standard. 
 + 
 +    phpcs --config-set default_standard DokuWiki 
 + 
 +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 ''PHP_CodeSniffer_Sniff'' was not found. 
 + 
 +=== Usage === 
 + 
 +You can use PHP_CodeSniffer to check a single file or an entire directory including subdirectories using the following commands. 
 + 
 +    # Check a single file 
 +    phpcs myfile.php 
 +     
 +    # Check all files in a directory 
 +    phpcs /path/to/directory
  
devel/coding_style.txt · Last modified: 2023-11-13 21:13 by andi

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki