OC Coding Style: Unterschied zwischen den Versionen

Aus Opencaching-Wiki
Zur Navigation springen Zur Suche springen
Zeile 19: Zeile 19:
  
 
== SQL ==
 
== SQL ==
All SQL language identifiers are written in CAPS, and all user-defined identifiers in `backticks`. Inner joins are coded as JOIN and not as WHERE condition. All variable values inserted into SQLs '''must''' be escaped, either via &1 &2 &3 placeholders or via function <code>sql_escape()</code>.
+
See also: [[Entwicklung/Codedoku#Datenbank]]
  
SQL statements embedded in PHP code are generally formatted like this:
+
All SQL language elements are written in CAPS, and all user-defined identifiers in `backticks`. Inner joins are coded as JOIN and not as WHERE. All variable values inserted into SQLs '''must''' be escaped, either via placeholders like <code>'&1'</code> or via function <code>sql_escape()</code>.
 +
 
 +
SQL code inserted into PHP code is enclosed in double quotes and generally formatted like this:
  
 
     $result = sql(
 
     $result = sql(
Zeile 27: Zeile 29:
 
         FROM `caches`
 
         FROM `caches`
 
         JOIN `cache_logs` ON `cache_logs`.`id` = `caches`.`id`
 
         JOIN `cache_logs` ON `cache_logs`.`id` = `caches`.`id`
         WHERE `caches`.`staatus` IN ('&1', '&2')"
+
         WHERE `caches`.`status` IN ('&1', '&2')"
 
         $status1,
 
         $status1,
 
         $status2
 
         $status2
 +
    );
 +
 +
Note that the starting " is on a tab position, while the following lines start one column to the right.
 +
 +
If lines get too long, the parameters are moved to the next line(s) and indented to the next Tab position (which is ''three'' chars right of the preceding line start):
 +
 +
    $result = sql(
 +
        "SELECT
 +
            `caches`.`cache_id`,
 +
            `caches`.`type` AS `cache_type`,
 +
            `cache_logs`.`date` AS `log_date`,
 +
            `cache_logs`.`type` AS `log_type`,
 +
        FROM `caches`
 +
        JOIN `cache_logs`
 +
            ON `cache_logs`.`id` = `caches`.`id`
 +
            AND `cache_logs`.`type` = '&1'
 +
        WHERE
 +
            `caches`.`status` = '&1'
 +
            AND `cache_logs`.`user_id` = '&2'
 +
        $cacheStatus
 +
        $userId
 
     );
 
     );

Version vom 19. Mai 2016, 16:08 Uhr

The following rules apply to all developers who contribute source code to the Opencaching.de project.

General rules

All source code files must

  • not contain TAB characters,
  • use 4-char indenting columns,
  • be UTF-8 encoded without byte order marks,
  • use only LF as line ends, with the current exception of email templates, which have RFC-822 CR/LF line ends.

There are some tools in the local/devel directory for verifying and fixing these things.

PHP

All self-written or directly changed code must fully comply to the PSR-1 / PSR-2 standard, with these exceptions:

  • When referencing identifiers (e.g. class and function names) that are defined in non-PSR-2-compliant source code files, they need not be PSR-2 compliant.
  • When writing small standalone scripts like those in the 'local' and 'util' directories, they need not to comply to the PSR-1 "Side Effects" rule.

Only <?php and <?= open tags must be used. <? (short open tags) must not be used.

SQL

See also: Entwicklung/Codedoku#Datenbank

All SQL language elements are written in CAPS, and all user-defined identifiers in `backticks`. Inner joins are coded as JOIN and not as WHERE. All variable values inserted into SQLs must be escaped, either via placeholders like '&1' or via function sql_escape().

SQL code inserted into PHP code is enclosed in double quotes and generally formatted like this:

   $result = sql(
       "SELECT `cache_id`, `wp_oc`
        FROM `caches`
        JOIN `cache_logs` ON `cache_logs`.`id` = `caches`.`id`
        WHERE `caches`.`status` IN ('&1', '&2')"
       $status1,
       $status2
   );

Note that the starting " is on a tab position, while the following lines start one column to the right.

If lines get too long, the parameters are moved to the next line(s) and indented to the next Tab position (which is three chars right of the preceding line start):

   $result = sql(
       "SELECT
           `caches`.`cache_id`,
           `caches`.`type` AS `cache_type`,
           `cache_logs`.`date` AS `log_date`,
           `cache_logs`.`type` AS `log_type`,
        FROM `caches`
        JOIN `cache_logs`
           ON `cache_logs`.`id` = `caches`.`id`
           AND `cache_logs`.`type` = '&1'
        WHERE
           `caches`.`status` = '&1'
           AND `cache_logs`.`user_id` = '&2'
       $cacheStatus
       $userId
   );