r64482 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64481‎ | r64482 | r64483 >
Date:11:37, 1 April 2010
Author:svip
Status:ok (Comments)
Tags:
Comment:
Tidying up code, added comments to each function, etc.
Modified paths:
  • /trunk/extensions/NaturalLanguageList/NaturalLanguageList.php (modified) (history)

Diff [purge]

Index: trunk/extensions/NaturalLanguageList/NaturalLanguageList.php
@@ -55,7 +55,7 @@
5656 $wgParserTestFiles[] = dirname( __FILE__ ) . "/nllParserTests.txt";
5757
5858 class NaturalLanguageList {
59 -
 59+
6060 public static function onParserFirstCallInit( $parser ) {
6161 $parser->setFunctionHook(
6262 'list',
@@ -69,7 +69,15 @@
7070 );
7171 return true;
7272 }
73 -
 73+
 74+ /**
 75+ * Render {{#list:}}
 76+ *
 77+ * @param $parser Parser
 78+ * @param $frame PPFrame_DOM
 79+ * @param $args Array
 80+ * @return wikicode parsed
 81+ */
7482 public static function render( $parser, $frame, $args ) {
7583 if ( count( $args ) == 0 ) {
7684 return '';
@@ -80,33 +88,39 @@
8189
8290 return $obj->outputList();
8391 }
84 -
 92+
 93+ /**
 94+ * Render {{#rawlist:}}
 95+ *
 96+ * @param $parser Parser
 97+ * @param $frame PPFrame_DOM
 98+ * @param $args Array
 99+ * @return wikicode parsed
 100+ */
85101 public static function renderRaw ( $parser, $frame, $args ) {
86102 if ( count( $args ) == 0 ) {
87103 return '';
88104 }
89 -
90 - $obj = new self( $parser, $frame, $args );
91 -
 105+ $obj = new self( $parser, $frame, $args );
 106+ # get separator between data
92107 $separator = $obj->mArgs[0];
93 -
94 - $obj->readOptions( true, $separator );
95 -
 108+ $obj->readOptions( true, $separator );
96109 $obj->readArgs( $separator );
97110
98111 return $obj->outputList();
99112 }
 113+
100114 private $mParser;
101115 private $mFrame;
102116 public $mArgs;
103117 private $mSeparator = null;
104118 private $mOptions = array(
105 - 'fieldsperitem' => -1,
106 - 'duplicates' => true,
107 - 'blanks' => false,
108 - 'itemoutput' => null,
109 - 'outputseparator' => null,
110 - 'lastseparator' => null,
 119+ 'fieldsperitem' => -1, # size of pairs
 120+ 'duplicates' => true, # allow same elements to appear
 121+ 'blanks' => false, # allow blank elements to appear
 122+ 'itemoutput' => null, # the format for each element
 123+ 'outputseparator' => null, # the separator between output elements
 124+ 'lastseparator' => null, # the separator between the last two elements
111125 );
112126 private $mReaditems = array();
113127 public $mParams = array();
@@ -129,31 +143,41 @@
130144 */
131145 private function outputList() {
132146
133 - // Convert each item from an array into a string according to the format.
 147+ # Convert each item from an array into a string according to the format.
134148 $items = array_map( array( $this, 'formatOutputItem' ), $this->mParams );
135149
136 - // If there's only one item, there are no separators
 150+ # If there's only one item, there are no separators
137151 if ( count( $items ) === 1 )
138152 return $items[0];
139153
140 - // Otherwise remove the last from the list so that we can implode() the remainder
 154+ # Otherwise remove the last from the list so that we can implode() the remainder
141155 $last = array_pop( $items );
142156
143157 return implode( $this->mOptions['outputseparator'], $items ) . $this->mOptions['lastseparator'] . $last;
144158 }
145159
146 - // Format the input pairs that make up each output item using the given format
 160+ /**
 161+ * Format the input pairs that make up each output item using the given format
 162+ *
 163+ * @param $pair array or string
 164+ * @return string formatted output
 165+ */
147166 private function formatOutputItem( $pair ) {
148167 return wfMsgReplaceArgs( $this->mOptions['itemoutput'], $pair );
149168 }
150169
151170 /**
152171 * Create $this->mParams from $this->mReaditems using $this->mOptions.
 172+ *
 173+ * @param $separator [default:null] Input separator (e.g. ',')
153174 */
154175 private function readArgs( $separator=null ) {
155176 $items = array(); # array of args to include
156177
157 - $args = $this->mOptions['duplicates'] ? $this->mReaditems : array_unique( $this->mReaditems );
 178+ # strip read items of duplicate elements if not permitted
 179+ $args = $this->mOptions['duplicates']
 180+ ? $this->mReaditems
 181+ : array_unique( $this->mReaditems );
158182
159183 foreach ( $args as $arg ) {
160184 if ( !$this->mOptions['blanks'] && $arg === '' )
@@ -161,13 +185,13 @@
162186 self::parseArrayItem( $items, $arg, $separator );
163187 }
164188
165 - // Remove the ignored elements from the array
 189+ # Remove the ignored elements from the array
166190 $items = array_diff( $items, $this->mIgnores );
167191
168 - // Split the array into smaller arrays, one for each output item.
 192+ # Split the array into smaller arrays, one for each output item.
169193 $this->mParams = array_chunk( $items, $this->mOptions['fieldsperitem'] );
170194
171 - // Disgard any leftovers, hrm...
 195+ # Disgard any leftovers, hrm...
172196 if ( count( end( $this->mParams ) ) != $this->mOptions['fieldsperitem'] ) {
173197 array_pop( $this->mParams );
174198 }
@@ -176,6 +200,9 @@
177201
178202 /**
179203 * Create $this->mOptions and $this->mReaditems from $this->mArgs using $this->mFrame.
 204+ *
 205+ * @param $ignorefirst boolean Ignore first element in case of {{#rawlist:}}
 206+ * @param $separator [default:null] Input separator
180207 */
181208 private function readOptions ( $ignorefirst, $separator=null ) {
182209 $args = $this->mArgs;
@@ -206,7 +233,8 @@
207234 $this->maxDollar = 1;
208235 if ( $this->mOptions['itemoutput'] !== null ) {
209236 # set $this->maxDollar to the maxmimum found
210 - preg_replace_callback( '/\$([1-9][0-9]*)/', array( $this, 'callbackMaxDollar' ),
 237+ preg_replace_callback( '/\$([1-9][0-9]*)/',
 238+ array( $this, 'callbackMaxDollar' ),
211239 $this->mOptions['itemoutput'] );
212240 }
213241 $this->mOptions['fieldsperitem'] = $this->maxDollar;
@@ -216,21 +244,28 @@
217245 if ( $this->mOptions['outputseparator'] === null ) {
218246
219247 $this->mOptions['outputseparator'] = wfMsgNoTrans( 'nll-separator' );
220 -
 248+
221249 if ( $this->mOptions['lastseparator'] === null ) {
222250 $this->mOptions['lastseparator'] = wfMsgNoTrans( 'nll-lastseparator' );
223251 }
224 -
 252+ # set the last separator to the regular separator if the separator is
 253+ # set and the last separator isn't set specifically
225254 } else if ( $this->mOptions['lastseparator'] === null ) {
226255 $this->mOptions['lastseparator'] = $this->mOptions['outputseparator'];
227256 }
228 -
 257+
 258+ # use the default format if format not set
229259 if ( $this->mOptions['itemoutput'] === null ) {
230260 $this->mOptions['itemoutput'] = wfMsgNoTrans( 'nll-itemoutput' );
231261 }
232262 }
233263
234 - // Used to find the highest $n in a string
 264+ /**
 265+ * Find the highest $n in a string
 266+ *
 267+ * @param $m Array (object, number)
 268+ * @return object
 269+ */
235270 private function callbackMaxDollar( $m ) {
236271 $this->maxDollar = max( $this->maxDollar, $m[1] );
237272 return $m[0];
@@ -241,6 +276,10 @@
242277 * and decides whether it is an option or not.
243278 * If it is, then it handles the option (and applies it).
244279 * If it isn't, then it just returns the string it found.
 280+ *
 281+ * @param $arg Argument
 282+ * @param $separator Input separator
 283+ * @return Return string if element, else return false
245284 */
246285 private function handleInputItem( $arg, $separator=null ) {
247286 if ( $arg instanceof PPNode_DOM ) {
@@ -292,6 +331,12 @@
293332 return false;
294333 }
295334
 335+ /**
 336+ * Using magic to store all known names for each option
 337+ *
 338+ * @param $input string
 339+ * @return The option found; otherwise false
 340+ */
296341 private static function parseOptionName( $value ) {
297342
298343 static $magicWords = null;
@@ -307,20 +352,39 @@
308353 if ( $name = $magicWords->matchStartToEnd( trim($value) ) ) {
309354 return str_replace( 'nll_', '', $name );
310355 }
311 -
 356+
 357+ # blimey, so not an option!?
312358 return false;
313359 }
314360
 361+ /**
 362+ * Insert a new element into an array.
 363+ *
 364+ * @param $array The array in question
 365+ * @param $value The element to be inserted
 366+ * @param $separator [default:null] Input separator
 367+ */
315368 private static function parseArrayItem( &$array, $value, $separator=null ) {
 369+ # if no separator, just assume the value can be appended,
 370+ # simple as that
316371 if ( $separator === null ) {
317372 $array[] = $value;
318373 } else {
 374+ # else, let's break the value up and append
 375+ # each 'subvalue' to the array.
319376 $tmp = explode ( $separator, $value );
320377 foreach ( $tmp as $v )
321378 $array[] = $v;
322379 }
323380 }
324 -
 381+
 382+ /**
 383+ * Parse numeral
 384+ *
 385+ * @param $value Integer
 386+ * @param $default [default:1] Integer
 387+ * @return The integer if integer and above 0, otherwise $default
 388+ */
325389 private static function parseNumeral( $value, $default = 1 ) {
326390 if ( is_numeric( $value ) && $value > 0 ) {
327391 return floor( $value ); # only integers
@@ -328,12 +392,25 @@
329393 return $default;
330394 }
331395
 396+ /**
 397+ * Parse string
 398+ *
 399+ * @param $value String
 400+ * @param $default [default:null] String
 401+ * @return The string, if none found, return $default
 402+ */
332403 private static function parseString( $value, $default = null ) {
333404 if ( $value !== '' )
334405 return $value;
335406 return $default;
336407 }
337408
 409+ /**
 410+ * Parse boolean
 411+ *
 412+ * @param $value String
 413+ * @return true if truth value found; otherwise false
 414+ */
338415 private static function parseBoolean( $value ) {
339416 return in_array( $value, array( 1, true, '1', 'true' ), true );
340417 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r64507Follow up to r64482svip22:30, 1 April 2010

Comments

#Comment by Conrad.Irwin (talk | contribs)   17:19, 1 April 2010

The lines that contain only a tab character are a bit wierd, and you're missing some of the type annotations in the docs, "@param $arg Argument". (Also better to avoid tabs for spacing between code and a comment on the same line)

#Comment by Svippong (talk | contribs)   17:27, 1 April 2010

Well, what is the full format then?

"@param $arg Type [default] Explanation"?

As for the lines only containing a tab character, I suppose that is a weirdness of mine, but I do not like going through my code and having my cursor jump in between code that is supposed to be all indented (I suppose it stems from my workings with Python).

I'm afraid I do not understand your comment in brackets, btw.

#Comment by Svippong (talk | contribs)   17:28, 1 April 2010

Also; no congrats on commit access? >:|

#Comment by Conrad.Irwin (talk | contribs)   17:33, 1 April 2010

Right on doc formatting. Can understand the indentation, just use vim and configure it right :). (The comment in brackets was referring to "'blanks' => false, # allow blank elements to appear" - that comment jumps out of line with tab width of 8).

Congrats!

#Comment by Svippong (talk | contribs)   18:56, 1 April 2010

Well, uhm, I generally prefer a length of 4 spaces for tabs. So I guess that's why. 8 spaces seems so long and when you start indenting a lot (deep code), the dreaded horizontal scrollbar appears.

#Comment by JonathanWilliford (talk | contribs)   22:22, 1 April 2010

My understanding is that you should use only tabs for indentation so that even the (crazy) people that prefer 8 spaces for tabs can see the comments that follow code - and so you don't start a dreaded Tab War (and you don't want to get in a war with those crazy folk). I also made that mistake before when first contributing.

#Comment by Svippong (talk | contribs)   22:33, 1 April 2010

There, followed up in r64507, hope the changes pleases, cirwin.

Status & tagging log