Skip to content

Commit

Permalink
Fix indentation in PhpArrayGenerator.php
Browse files Browse the repository at this point in the history
  • Loading branch information
sovetski committed Apr 12, 2024
1 parent 9c89886 commit 3899f6b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions features/makephp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ Feature: Generate PHP files from PO files
And the foo-plugin/foo-plugin-de_DE.l10n.php file should contain:
"""
<?php
return [
return [
'domain' => 'foo-plugin',
'plural-forms' => 'nplurals=2; plural=(n != 1);',
'language' => 'de_DE',
Expand All @@ -317,5 +317,5 @@ Feature: Generate PHP files from PO files
'Hello' => 'Hallo',
'You have %d new message' => 'Du hast %d neue Nachricht' . "\0" . 'Du hast %d neue Nachrichten',
],
];
];
"""
11 changes: 5 additions & 6 deletions src/PhpArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function toString( Translations $translations, array $options = []
$exported_array = static::var_export( $array );
}

return '<?php' . PHP_EOL . ' return ' . $exported_array . ';';
return '<?php' . PHP_EOL . 'return ' . $exported_array . ';';
}

/**
Expand Down Expand Up @@ -175,22 +175,21 @@ private static function var_export( $value ) {

/**
* Outputs or returns a parsable string representation of a variable.
* @since 4.0.0
*
* @param mixed $value The variable you want to export.
* @param int $level The current indentation level.
* @param int $indentation The number of spaces for indentation. Default is 4.
* @param int $indentation The number of spaces for indentation.
* @return string The variable representation.
*/
private static function pretty_export( $values, $indentation = 2 ) {
private static function pretty_export( $values, $indentation = 4, $is_top_level = true ) {
$result = '[' . PHP_EOL;
$indent = str_repeat( ' ', $indentation );

foreach ( $values as $key => $value ) {
$result .= $indent . str_repeat( ' ', $indentation ) . "'$key' => ";

if ( is_array( $value ) ) {
$result .= self::pretty_export( $value, $indentation + $indentation );
$result .= self::pretty_export( $value, $indentation + $indentation, false );
} elseif ( strpos( $value, "\0" ) !== false ) {
$parts = explode( "\0", $value );
$result .= "'" . implode( "' . \"\\0\" . '", array_map( 'addslashes', $parts ) ) . "'";
Expand All @@ -201,7 +200,7 @@ private static function pretty_export( $values, $indentation = 2 ) {
$result .= ',' . PHP_EOL;
}

$result .= $indent . ']';
$result .= $is_top_level ? ']' : $indent . ']';
return $result;
}
}

0 comments on commit 3899f6b

Please sign in to comment.