* Clear the current result data.
*/
public function reset() {
- $this->data = array(
+ $this->data = [
self::META_TYPE => 'assoc', // Usually what's desired
- );
+ ];
$this->size = 0;
}
* @param array $transforms See above
* @return mixed Result data, or null if not found
*/
- public function getResultData( $path = array(), $transforms = array() ) {
+ public function getResultData( $path = [], $transforms = [] ) {
$path = (array)$path;
if ( !$path ) {
return self::applyTransformations( $this->data, $transforms );
$exists = isset( $arr[$name] );
if ( !$exists || ( $flags & ApiResult::OVERRIDE ) ) {
if ( !$exists && ( $flags & ApiResult::ADD_ON_TOP ) ) {
- $arr = array( $name => $value ) + $arr;
+ $arr = [ $name => $value ] + $arr;
} else {
$arr[$name] = $value;
}
if ( !$conflicts ) {
$arr[$name] += $value;
} else {
- $keys = join( ', ', array_keys( $conflicts ) );
+ $keys = implode( ', ', array_keys( $conflicts ) );
throw new RuntimeException(
"Conflicting keys ($keys) when attempting to merge element $name"
);
if ( is_object( $value ) ) {
// Note we use is_callable() here instead of instanceof because
// ApiSerializable is an informal protocol (see docs there for details).
- if ( is_callable( array( $value, 'serializeForApiResult' ) ) ) {
+ if ( is_callable( [ $value, 'serializeForApiResult' ] ) ) {
$oldValue = $value;
$value = $value->serializeForApiResult();
if ( is_object( $value ) ) {
throw new UnexpectedValueException(
- get_class( $oldValue ) . "::serializeForApiResult() returned an object of class " .
+ get_class( $oldValue ) . '::serializeForApiResult() returned an object of class ' .
get_class( $value )
);
}
return self::validateValue( $value );
} catch ( Exception $ex ) {
throw new UnexpectedValueException(
- get_class( $oldValue ) . "::serializeForApiResult() returned an invalid value: " .
+ get_class( $oldValue ) . '::serializeForApiResult() returned an invalid value: ' .
$ex->getMessage(),
0,
$ex
);
}
- } elseif ( is_callable( array( $value, '__toString' ) ) ) {
+ } elseif ( is_callable( [ $value, '__toString' ] ) ) {
$value = (string)$value;
} else {
- $value = (array)$value + array( self::META_TYPE => 'assoc' );
+ $value = (array)$value + [ self::META_TYPE => 'assoc' ];
}
}
if ( is_array( $value ) ) {
+ // Work around PHP bug 45959 by copying to a temporary
+ // (in this case, foreach gets $k === "1" but $tmp[$k] assigns as if $k === 1)
+ $tmp = [];
foreach ( $value as $k => $v ) {
- $value[$k] = self::validateValue( $v );
+ $tmp[$k] = self::validateValue( $v );
}
+ $value = $tmp;
} elseif ( is_float( $value ) && !is_finite( $value ) ) {
- throw new InvalidArgumentException( "Cannot add non-finite floats to ApiResult" );
+ throw new InvalidArgumentException( 'Cannot add non-finite floats to ApiResult' );
} elseif ( is_string( $value ) ) {
$value = $wgContLang->normalize( $value );
} elseif ( $value !== null && !is_scalar( $value ) ) {
) {
throw new RuntimeException(
"Attempting to set content element as $name when " . $arr[self::META_CONTENT] .
- " is already set as the content element"
+ ' is already set as the content element'
);
}
$arr[self::META_CONTENT] = $name;
* @param string $kvpKeyName See ApiResult::META_KVP_KEY_NAME
*/
public static function setArrayType( array &$arr, $type, $kvpKeyName = null ) {
- if ( !in_array( $type, array(
+ if ( !in_array( $type, [
'default', 'array', 'assoc', 'kvp', 'BCarray', 'BCassoc', 'BCkvp'
- ), true ) ) {
+ ], true ) ) {
throw new InvalidArgumentException( 'Bad type' );
}
$arr[self::META_TYPE] = $type;
throw new InvalidArgumentException( __METHOD__ . ':Value for "Types" must be an array' );
}
- $metadata = array();
+ $metadata = [];
$data = self::stripMetadataNonRecursive( $dataIn, $metadata );
if ( isset( $transforms['Custom'] ) ) {
if ( !is_callable( $transforms['Custom'] ) ) {
throw new InvalidArgumentException( __METHOD__ . ': Value for "Custom" must be callable' );
}
- call_user_func_array( $transforms['Custom'], array( &$data, &$metadata ) );
+ call_user_func_array( $transforms['Custom'], [ &$data, &$metadata ] );
}
if ( ( isset( $transforms['BC'] ) || $transformTypes !== null ) &&
if ( !in_array( 'nobool', $transforms['BC'], true ) ) {
$boolKeys = isset( $metadata[self::META_BC_BOOLS] )
? array_flip( $metadata[self::META_BC_BOOLS] )
- : array();
+ : [];
}
if ( !in_array( 'no*', $transforms['BC'], true ) &&
) {
foreach ( $metadata[self::META_BC_SUBELEMENTS] as $k ) {
if ( isset( $data[$k] ) ) {
- $data[$k] = array(
+ $data[$k] = [
'*' => $data[$k],
self::META_CONTENT => '*',
self::META_TYPE => 'assoc',
- );
+ ];
}
}
}
switch ( $strip ) {
case 'all':
case 'base':
- $keepMetadata = array();
+ $keepMetadata = [];
break;
case 'none':
$keepMetadata = &$metadata;
break;
case 'bc':
- $keepMetadata = array_intersect_key( $metadata, array(
+ $keepMetadata = array_intersect_key( $metadata, [
self::META_INDEXED_TAG_NAME => 1,
self::META_SUBELEMENTS => 1,
- ) );
+ ] );
break;
default:
throw new InvalidArgumentException( __METHOD__ . ': Unknown value for "Strip"' );
$assocAsObject = !empty( $transformTypes['AssocAsObject'] );
$merge = !empty( $metadata[self::META_KVP_MERGE] );
- $ret = array();
+ $ret = [];
foreach ( $data as $k => $v ) {
if ( $merge && ( is_array( $v ) || is_object( $v ) ) ) {
$vArr = (array)$v;
$mergeType = 'n/a';
}
if ( $mergeType === 'assoc' ) {
- $item = $vArr + array(
+ $item = $vArr + [
$key => $k,
- );
+ ];
if ( $strip === 'none' ) {
- self::setPreserveKeysList( $item, array( $key ) );
+ self::setPreserveKeysList( $item, [ $key ] );
}
} else {
- $item = array(
+ $item = [
$key => $k,
$valKey => $v,
- );
+ ];
if ( $strip === 'none' ) {
- $item += array(
- self::META_PRESERVE_KEYS => array( $key ),
+ $item += [
+ self::META_PRESERVE_KEYS => [ $key ],
self::META_CONTENT => $valKey,
self::META_TYPE => 'assoc',
- );
+ ];
}
}
$ret[] = $assocAsObject ? (object)$item : $item;
}
$preserveKeys = isset( $data[self::META_PRESERVE_KEYS] )
? (array)$data[self::META_PRESERVE_KEYS]
- : array();
+ : [];
foreach ( $data as $k => $v ) {
if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys, true ) ) {
unset( $data[$k] );
*/
public static function stripMetadataNonRecursive( $data, &$metadata = null ) {
if ( !is_array( $metadata ) ) {
- $metadata = array();
+ $metadata = [];
}
if ( is_array( $data ) || is_object( $data ) ) {
$isObj = is_object( $data );
}
$preserveKeys = isset( $data[self::META_PRESERVE_KEYS] )
? (array)$data[self::META_PRESERVE_KEYS]
- : array();
+ : [];
foreach ( $data as $k => $v ) {
if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys, true ) ) {
$metadata[$k] = $v;
if ( !isset( $ret[$k] ) ) {
switch ( $create ) {
case 'append':
- $ret[$k] = array();
+ $ret[$k] = [];
break;
case 'prepend':
- $ret = array( $k => array() ) + $ret;
+ $ret = [ $k => [] ] + $ret;
break;
case 'dummy':
- $tmp = array();
+ $tmp = [];
return $tmp;
default:
- $fail = join( '.', array_slice( $path, 0, $i + 1 ) );
+ $fail = implode( '.', array_slice( $path, 0, $i + 1 ) );
throw new InvalidArgumentException( "Path $fail does not exist" );
}
}
if ( !is_array( $ret[$k] ) ) {
- $fail = join( '.', array_slice( $path, 0, $i + 1 ) );
+ $fail = implode( '.', array_slice( $path, 0, $i + 1 ) );
throw new InvalidArgumentException( "Path $fail is not an array" );
}
$ret = &$ret[$k];
// Process subarrays and determine if this is a JS [] or {}
$hash = $forceHash;
$maxKey = -1;
- $bools = array();
+ $bools = [];
foreach ( $vars as $k => $v ) {
if ( is_array( $v ) || is_object( $v ) ) {
$vars[$k] = ApiResult::addMetadataToResultVars( (array)$v, is_object( $v ) );
if ( $hash ) {
// Get the list of keys we actually care about. Unfortunately, we can't support
// certain keys that conflict with ApiResult metadata.
- $keys = array_diff( array_keys( $vars ), array(
+ $keys = array_diff( array_keys( $vars ), [
ApiResult::META_TYPE, ApiResult::META_PRESERVE_KEYS, ApiResult::META_KVP_KEY_NAME,
ApiResult::META_INDEXED_TAG_NAME, ApiResult::META_BC_BOOLS
- ) );
+ ] );
- return array(
+ return [
ApiResult::META_TYPE => 'kvp',
ApiResult::META_KVP_KEY_NAME => 'key',
ApiResult::META_PRESERVE_KEYS => $keys,
ApiResult::META_BC_BOOLS => $bools,
ApiResult::META_INDEXED_TAG_NAME => 'var',
- ) + $vars;
+ ] + $vars;
} else {
- return array(
+ return [
ApiResult::META_TYPE => 'array',
ApiResult::META_BC_BOOLS => $bools,
ApiResult::META_INDEXED_TAG_NAME => 'value',
- ) + $vars;
+ ] + $vars;
}
}
*/
public function getData() {
wfDeprecated( __METHOD__, '1.25' );
- return $this->getResultData( null, array(
- 'BC' => array(),
- 'Types' => array(),
+ return $this->getResultData( null, [
+ 'BC' => [],
+ 'Types' => [],
'Strip' => 'all',
- ) );
+ ] );
}
/**
self::setContentValue( $arr, 'content', $value );
} else {
if ( !isset( $arr[$subElemName] ) ) {
- $arr[$subElemName] = array();
+ $arr[$subElemName] = [];
}
self::setContentValue( $arr[$subElemName], 'content', $value );
}
* @return array
*/
public function beginContinuation(
- $continue, array $allModules = array(), array $generatedModules = array()
+ $continue, array $allModules = [], array $generatedModules = []
) {
wfDeprecated( __METHOD__, '1.25' );
if ( $this->mainForContinuation->getContinuationManager() ) {
$newCtx = new DerivativeContext( $oldCtx );
$newCtx->setRequest( new DerivativeRequest(
$oldCtx->getRequest(),
- array( 'continue' => $continue ) + $oldCtx->getRequest()->getValues(),
+ [ 'continue' => $continue ] + $oldCtx->getRequest()->getValues(),
$oldCtx->getRequest()->wasPosted()
) );
$this->mainForContinuation->setContext( $newCtx );
$reset = new ScopedCallback(
- array( $this->mainForContinuation, 'setContext' ),
- array( $oldCtx )
+ [ $this->mainForContinuation, 'setContext' ],
+ [ $oldCtx ]
);
}
$manager = new ApiContinuationManager(
$this->mainForContinuation->setContinuationManager( $manager );
- return array(
+ return [
$manager->isGeneratorDone(),
$manager->getRunModules(),
- );
+ ];
}
/**