Commit | Line | Data |
---|---|---|
e3747bbc | 1 | # Code Style |
54c9308d | 2 | |
9003992d | 3 | !!! info "The following code style conventions are used by us for our own packages. While you do not have to follow every rule, you are encouraged to do so." |
10887dc3 | 4 | |
21609ed2 | 5 | For information about how to document your code, please refer to the [documentation page](code-style-documentation.md). |
07242e17 MS |
6 | |
7 | ||
54c9308d MS |
8 | ## General Code Style |
9 | ||
10 | ### Naming conventions | |
11 | ||
12 | The relevant naming conventions are: | |
13 | ||
14 | - **Upper camel case**: | |
15 | The first letters of all compound words are written in upper case. | |
16 | - **Lower camel case**: | |
17 | The first letters of compound words are written in upper case, except for the first letter which is written in lower case. | |
18 | - **Screaming snake case**: | |
0b828ab7 | 19 | All letters are written in upper case and compound words are separated by underscores. |
54c9308d MS |
20 | |
21 | ||
223b602c MS |
22 | | Type | Convention | Example | |
23 | |------|------------|---------| | |
24 | | Variable | lower camel case | `$variableName` | | |
25 | | Class | upper camel case | `class UserGroupEditor` | | |
26 | | Properties | lower camel case | `public $propertyName` | | |
27 | | Method | lower camel case | `public function getObjectByName()` | | |
28 | | Constant | screaming snake case | `MODULE_USER_THING` | | |
54c9308d MS |
29 | |
30 | ### Arrays | |
31 | ||
32 | For arrays, use the short array syntax introduced with PHP 5.4. | |
33 | The following example illustrates the different cases that can occur when working with arrays and how to format them: | |
34 | ||
35 | ```php | |
36 | <?php | |
37 | ||
38 | $empty = []; | |
39 | ||
40 | $oneElement = [1]; | |
41 | $multipleElements = [1, 2, 3]; | |
42 | ||
43 | $oneElementWithKey = ['firstElement' => 1]; | |
44 | $multipleElementsWithKey = [ | |
45 | 'firstElement' => 1, | |
46 | 'secondElement' => 2, | |
47 | 'thirdElement' => 3 | |
48 | ]; | |
49 | ``` | |
50 | ||
10887dc3 MS |
51 | ### Ternary Operator |
52 | ||
0b828ab7 | 53 | The ternary operator can be used for short conditioned assignments: |
10887dc3 MS |
54 | |
55 | ```php | |
56 | <?php | |
57 | ||
58 | $name = isset($tagArgs['name']) ? $tagArgs['name'] : 'default'; | |
59 | ``` | |
60 | ||
61 | The condition and the values should be short so that the code does not result in a very long line which thus decreases the readability compared to an `if-else` statement. | |
62 | ||
63 | Parentheses may only be used around the condition and not around the whole statement: | |
64 | ||
65 | ```php | |
66 | <?php | |
67 | ||
68 | // do not do it like this | |
69 | $name = (isset($tagArgs['name']) ? $tagArgs['name'] : 'default'); | |
70 | ``` | |
71 | ||
72 | Parentheses around the conditions may not be used to wrap simple function calls: | |
73 | ||
74 | ```php | |
75 | <?php | |
76 | ||
77 | // do not do it like this | |
78 | $name = (isset($tagArgs['name'])) ? $tagArgs['name'] : 'default'; | |
79 | ``` | |
80 | ||
81 | but have to be used for comparisons or other binary operators: | |
82 | ||
83 | ```php | |
84 | <?php | |
85 | ||
86 | $value = ($otherValue > $upperLimit) ? $additionalValue : $otherValue; | |
87 | ``` | |
88 | ||
89 | If you need to use more than one binary operator, use an `if-else` statement. | |
90 | ||
91 | The same rules apply to assigning array values: | |
92 | ||
93 | ```php | |
94 | <?php | |
95 | ||
96 | $values = [ | |
97 | 'first' => $firstValue, | |
98 | 'second' => $secondToggle ? $secondValueA : $secondValueB, | |
99 | 'third' => ($thirdToogle > 13) ? $thirdToogleA : $thirdToogleB | |
100 | ]; | |
101 | ``` | |
102 | ||
103 | or return values: | |
104 | ||
105 | ```php | |
106 | <?php | |
107 | ||
108 | return isset($tagArgs['name']) ? $tagArgs['name'] : 'default'; | |
109 | ``` | |
110 | ||
54c9308d MS |
111 | ### Whitespaces |
112 | ||
113 | You have to put a whitespace *in front* of the following things: | |
114 | ||
0b828ab7 | 115 | - equal sign in assignments: `$x = 1;` |
54c9308d MS |
116 | - comparison operators: `$x == 1` |
117 | - opening bracket of a block `public function test() {` | |
118 | ||
119 | You have to put a whitespace *behind* the following things: | |
120 | ||
0b828ab7 | 121 | - equal sign in assignments: `$x = 1;` |
54c9308d MS |
122 | - comparison operators: `$x == 1` |
123 | - comma in a function/method parameter list if the comma is not followed by a line break: `public function test($a, $b) {` | |
124 | - `if`, `for`, `foreach`, `while`: `if ($x == 1)` | |
78b868ea MS |
125 | |
126 | ||
127 | ## Classes | |
128 | ||
10887dc3 MS |
129 | ### Referencing Class Names |
130 | ||
131 | If you have to reference a class name inside a php file, you have to use the `class` keyword. | |
132 | ||
133 | ```php | |
134 | <?php | |
135 | ||
136 | // not like this | |
137 | $className = 'wcf\data\example\Example'; | |
138 | ||
139 | // like this | |
140 | use wcf\data\example\Example; | |
bc897394 | 141 | $className = Example::class; |
10887dc3 MS |
142 | ``` |
143 | ||
78b868ea MS |
144 | ### Static Getters (of `DatabaseObject` Classes) |
145 | ||
146 | Some database objects provide static getters, either if they are decorators or for a unique combination of database table columns, like `wcf\data\box\Box::getBoxByIdentifier()`: | |
147 | ||
9a3f5fa3 MS |
148 | {jinja{ codebox( |
149 | "php", | |
150 | "php/code-style/Box.class.php", | |
151 | "files/lib/data/box/Box.class.php" | |
152 | ) }} | |
78b868ea MS |
153 | |
154 | Such methods should always either return the desired object or `null` if the object does not exist. | |
155 | `wcf\system\database\statement\PreparedStatement::fetchObject()` already takes care of this distinction so that its return value can simply be returned by such methods. | |
156 | ||
157 | The name of such getters should generally follow the convention `get{object type}By{column or other description}`. | |
10887dc3 MS |
158 | |
159 | ### Long method calls | |
160 | ||
161 | In some instances, methods with many argument have to be called which can result in lines of code like this one: | |
162 | ||
163 | ```php | |
164 | <?php | |
165 | ||
166 | \wcf\system\search\SearchIndexManager::getInstance()->set('com.woltlab.wcf.article', $articleContent->articleContentID, $articleContent->content, $articleContent->title, $articles[$articleContent->articleID]->time, $articles[$articleContent->articleID]->userID, $articles[$articleContent->articleID]->username, $articleContent->languageID, $articleContent->teaser); | |
167 | ``` | |
168 | ||
169 | which is hardly readable. | |
170 | Therefore, the line must be split into multiple lines with each argument in a separate line: | |
171 | ||
172 | ```php | |
173 | <?php | |
174 | ||
175 | \wcf\system\search\SearchIndexManager::getInstance()->set( | |
176 | 'com.woltlab.wcf.article', | |
177 | $articleContent->articleContentID, | |
178 | $articleContent->content, | |
179 | $articleContent->title, | |
180 | $articles[$articleContent->articleID]->time, | |
181 | $articles[$articleContent->articleID]->userID, | |
182 | $articles[$articleContent->articleID]->username, | |
183 | $articleContent->languageID, | |
184 | $articleContent->teaser | |
185 | ); | |
186 | ``` | |
187 | ||
188 | In general, this rule applies to the following methods: | |
189 | ||
190 | - `wcf\system\edit\EditHistoryManager::add()` | |
191 | - `wcf\system\message\quote\MessageQuoteManager::addQuote()` | |
192 | - `wcf\system\message\quote\MessageQuoteManager::getQuoteID()` | |
193 | - `wcf\system\search\SearchIndexManager::set()` | |
194 | - `wcf\system\user\object\watch\UserObjectWatchHandler::updateObject()` | |
195 | - `wcf\system\user\notification\UserNotificationHandler::fireEvent()` |