r97005 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97004‎ | r97005 | r97006 >
Date:21:45, 13 September 2011
Author:krinkle
Status:ok (Comments)
Tags:
Comment:
R2: Adding some comments that helped me understand Gadget::validatePropertiesArray
* Explicitly document the first and third array item as a type check, since the error messages produced mentions 'wrong type'
* Add comments explaining the two loops.
-
fu r96954
Modified paths:
  • /branches/RL2/extensions/Gadgets/backend/Gadget.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/backend/Gadget.php
@@ -45,7 +45,7 @@
4646
4747 /**
4848 * Validation metadata.
49 - * 'foo.bar.baz' => array( 'callback', 'type name' [, 'membercallback', 'member type name'] )
 49+ * 'foo.bar.baz' => array( 'type check callback', 'type name' [, 'member type check callback', 'member type name'] )
5050 */
5151 protected static $propertyValidation = array(
5252 'settings' => array( 'is_array', 'array' ),
@@ -75,33 +75,36 @@
7676 if ( !is_array( $properties ) ) {
7777 return Status::newFatal( 'gadgets-validate-notanobject', gettype( $properties ) ); // Use JSON terminology
7878 }
79 -
 79+
8080 foreach ( self::$propertyValidation as $property => $validation ) {
8181 $path = explode( '.', $property );
82 - $var = $properties;
 82+ $val = $properties;
 83+
 84+ // Walk down and verify that the path from the root to this property exists
8385 foreach ( $path as $p ) {
84 - if ( !isset( $var[$p] ) ) {
 86+ if ( !isset( $val[$p] ) ) {
8587 return Status::newFatal( 'gadgets-validate-notset', $property );
8688 }
87 - $var = $var[$p];
 89+ $val = $val[$p];
8890 }
89 -
 91+
 92+ // Do the actual validation of this property
9093 $func = $validation[0];
91 - if ( !$func( $var ) ) {
92 - return Status::newFatal( 'gadgets-validate-wrongtype', $property, $validation[1], gettype( $var ) );
 94+ if ( !$func( $val ) ) {
 95+ return Status::newFatal( 'gadgets-validate-wrongtype', $property, $validation[1], gettype( $val ) );
9396 }
94 -
 97+
9598 if ( isset( $validation[2] ) ) {
9699 // Descend into the array and check the type of each element
97100 $func = $validation[2];
98 - foreach ( $var as $i => $v ) {
 101+ foreach ( $val as $i => $v ) {
99102 if ( !$func( $v ) ){
100103 return Status::newFatal( 'gadgets-validate-wrongtype', "{$property}[{$i}]", $validation[3], gettype( $v ) );
101104 }
102105 }
103106 }
104107 }
105 -
 108+
106109 return Status::newGood();
107110 }
108111

Follow-up revisions

RevisionCommit summaryAuthorDate
r97052RL2: Followup to r96954 and r97005: add i18n messages for the validation stuf...catrope13:10, 14 September 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r96954RL2: Put in some more sophisticate validation code for gadget property arrays...catrope11:24, 13 September 2011

Comments

#Comment by Catrope (talk | contribs)   21:57, 13 September 2011

Hmm, I guess I need to add the i18n messages too.

Status & tagging log