summaryrefslogtreecommitdiff
path: root/includes/ConfEditor.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/ConfEditor.php')
-rw-r--r--includes/ConfEditor.php67
1 files changed, 42 insertions, 25 deletions
diff --git a/includes/ConfEditor.php b/includes/ConfEditor.php
index b68fc762..67cb87db 100644
--- a/includes/ConfEditor.php
+++ b/includes/ConfEditor.php
@@ -66,7 +66,6 @@ class ConfEditor {
*/
var $stateStack;
-
/**
* The path stack is a stack of associative arrays with the following elements:
* name The name of top level of the path
@@ -128,7 +127,7 @@ class ConfEditor {
/**
* Edit the text. Returns the edited text.
- * @param $ops Array of operations.
+ * @param array $ops of operations.
*
* Operations are given as an associative array, with members:
* type: One of delete, set, append or insert (required)
@@ -159,6 +158,7 @@ class ConfEditor {
* insert
* Insert a new element at the start of the array.
*
+ * @throws MWException
* @return string
*/
public function edit( $ops ) {
@@ -278,19 +278,23 @@ class ConfEditor {
function getVars() {
$vars = array();
$this->parse();
- foreach( $this->pathInfo as $path => $data ) {
- if ( $path[0] != '$' )
+ foreach ( $this->pathInfo as $path => $data ) {
+ if ( $path[0] != '$' ) {
continue;
+ }
$trimmedPath = substr( $path, 1 );
$name = $data['name'];
- if ( $name[0] == '@' )
+ if ( $name[0] == '@' ) {
continue;
- if ( $name[0] == '$' )
+ }
+ if ( $name[0] == '$' ) {
$name = substr( $name, 1 );
+ }
$parentPath = substr( $trimmedPath, 0,
strlen( $trimmedPath ) - strlen( $name ) );
- if( substr( $parentPath, -1 ) == '/' )
+ if ( substr( $parentPath, -1 ) == '/' ) {
$parentPath = substr( $parentPath, 0, -1 );
+ }
$value = substr( $this->text, $data['valueStartByte'],
$data['valueEndByte'] - $data['valueStartByte']
@@ -306,7 +310,7 @@ class ConfEditor {
* setVar( $arr, 'foo/bar', 'baz', 3 ); will set
* $arr['foo']['bar']['baz'] = 3;
* @param $array array
- * @param $path string slash-delimited path
+ * @param string $path slash-delimited path
* @param $key mixed Key
* @param $value mixed Value
*/
@@ -315,13 +319,15 @@ class ConfEditor {
$target =& $array;
if ( $path !== '' ) {
foreach ( $pathArr as $p ) {
- if( !isset( $target[$p] ) )
+ if ( !isset( $target[$p] ) ) {
$target[$p] = array();
+ }
$target =& $target[$p];
}
}
- if ( !isset( $target[$key] ) )
+ if ( !isset( $target[$key] ) ) {
$target[$key] = $value;
+ }
}
/**
@@ -329,25 +335,30 @@ class ConfEditor {
* @return mixed Parsed value
*/
function parseScalar( $str ) {
- if ( $str !== '' && $str[0] == '\'' )
+ if ( $str !== '' && $str[0] == '\'' ) {
// Single-quoted string
// @todo FIXME: trim() call is due to mystery bug where whitespace gets
// appended to the token; without it we ended up reading in the
// extra quote on the end!
return strtr( substr( trim( $str ), 1, -1 ),
array( '\\\'' => '\'', '\\\\' => '\\' ) );
- if ( $str !== '' && $str[0] == '"' )
+ }
+ if ( $str !== '' && $str[0] == '"' ) {
// Double-quoted string
// @todo FIXME: trim() call is due to mystery bug where whitespace gets
// appended to the token; without it we ended up reading in the
// extra quote on the end!
return stripcslashes( substr( trim( $str ), 1, -1 ) );
- if ( substr( $str, 0, 4 ) == 'true' )
+ }
+ if ( substr( $str, 0, 4 ) == 'true' ) {
return true;
- if ( substr( $str, 0, 5 ) == 'false' )
+ }
+ if ( substr( $str, 0, 5 ) == 'false' ) {
return false;
- if ( substr( $str, 0, 4 ) == 'null' )
+ }
+ if ( substr( $str, 0, 4 ) == 'null' ) {
return null;
+ }
// Must be some kind of numeric value, so let PHP's weak typing
// be useful for a change
return $str;
@@ -392,6 +403,8 @@ class ConfEditor {
* Finds the source byte region which you would want to delete, if $pathName
* was to be deleted. Includes the leading spaces and tabs, the trailing line
* break, and any comments in between.
+ * @param $pathName
+ * @throws MWException
* @return array
*/
function findDeletionRegion( $pathName ) {
@@ -450,6 +463,8 @@ class ConfEditor {
* or semicolon.
*
* The end position is the past-the-end (end + 1) value as per convention.
+ * @param $pathName
+ * @throws MWException
* @return array
*/
function findValueRegion( $pathName ) {
@@ -533,7 +548,7 @@ class ConfEditor {
*/
function getIndent( $pos, $key = false, $arrowPos = false ) {
$arrowIndent = ' ';
- if ( $pos == 0 || $this->text[$pos-1] == "\n" ) {
+ if ( $pos == 0 || $this->text[$pos - 1] == "\n" ) {
$indentLength = strspn( $this->text, " \t", $pos );
$indent = substr( $this->text, $pos, $indentLength );
} else {
@@ -555,7 +570,7 @@ class ConfEditor {
public function parse() {
$this->initParse();
$this->pushState( 'file' );
- $this->pushPath( '@extra-' . ($this->serial++) );
+ $this->pushPath( '@extra-' . ( $this->serial++ ) );
$token = $this->firstToken();
while ( !$token->isEnd() ) {
@@ -613,19 +628,21 @@ class ConfEditor {
$this->expect( '=' );
$this->skipSpace();
$this->startPathValue();
- if ( $arrayAssign )
+ if ( $arrayAssign ) {
$this->pushState( 'expression', 'array assign end' );
- else
+ } else {
$this->pushState( 'expression', 'statement end' );
+ }
break;
case 'array assign end':
case 'statement end':
$this->endPathValue();
- if ( $state == 'array assign end' )
+ if ( $state == 'array assign end' ) {
$this->popPath();
+ }
$this->skipSpace();
$this->expect( ';' );
- $this->nextPath( '@extra-' . ($this->serial++) );
+ $this->nextPath( '@extra-' . ( $this->serial++ ) );
break;
case 'expression':
$token = $this->skipSpace();
@@ -645,7 +662,7 @@ class ConfEditor {
$this->skipSpace();
$this->expect( '(' );
$this->skipSpace();
- $this->pushPath( '@extra-' . ($this->serial++) );
+ $this->pushPath( '@extra-' . ( $this->serial++ ) );
if ( $this->isAhead( ')' ) ) {
// Empty array
$this->pushState( 'array end' );
@@ -677,7 +694,7 @@ class ConfEditor {
$this->endPathValue();
$this->markComma();
$this->nextToken();
- $this->nextPath( '@extra-' . ($this->serial++) );
+ $this->nextPath( '@extra-' . ( $this->serial++ ) );
// Look ahead to find ending bracket
if ( $this->isAhead( ")" ) ) {
// Found ending bracket, no continuation
@@ -1052,9 +1069,10 @@ class ConfEditorParseError extends MWException {
$lines = StringUtils::explode( "\n", $text );
foreach ( $lines as $lineNum => $line ) {
if ( $lineNum == $this->lineNum - 1 ) {
- return "$line\n" .str_repeat( ' ', $this->colNum - 1 ) . "^\n";
+ return "$line\n" . str_repeat( ' ', $this->colNum - 1 ) . "^\n";
}
}
+ return '';
}
}
@@ -1089,4 +1107,3 @@ class ConfEditorToken {
return $this->type == 'END';
}
}
-