Skip to content

Commit

Permalink
Merge pull request #8 from mlocati/prettier-array-generator
Browse files Browse the repository at this point in the history
Let ArrayGenerator generate prettier PHP code
  • Loading branch information
oscarotero committed Nov 6, 2023
2 parents 9a9916e + 9d98bab commit 3a9fdda
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/Generator/ArrayGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@

final class ArrayGenerator extends Generator
{
/**
* @private
*/
const PRETTY_INDENT = ' ';

/**
* @var bool
*/
private $includeEmpty;

/**
* @var bool
*/
private $strictTypes;

/**
* @var bool
*/
private $pretty;

/**
* Constructs a new ArrayGenerator
* @param array|null $options
Expand All @@ -20,13 +38,21 @@ final class ArrayGenerator extends Generator
public function __construct(?array $options = null)
{
$this->includeEmpty = (bool) ($options['includeEmpty'] ?? false);
$this->strictTypes = (bool) ($options['strictTypes'] ?? false);
$this->pretty = (bool) ($options['pretty'] ?? false);
}

public function generateString(Translations $translations): string
{
$array = $this->generateArray($translations);
$result = '<?php';
if ($this->pretty) {
$result .= $this->strictTypes ? "\n\ndeclare(strict_types=1);\n\n" : "\n\n";
} else {
$result .= $this->strictTypes ? ' declare(strict_types=1); ' : ' ';
}

return sprintf('<?php return %s;', var_export($array, true));
return $result . 'return ' . ($this->pretty ? self::prettyExport($array) : (var_export($array, true) . ';'));
}

public function generateArray(Translations $translations): array
Expand Down Expand Up @@ -66,4 +92,48 @@ private static function hasPluralTranslations(Translation $translation): bool
{
return implode('', $translation->getPluralTranslations()) !== '';
}

private static function prettyExport(array &$array): string
{
return self::prettyExportArray($array, 0) . ";\n";
}

private static function prettyExportArray(array &$array, int $depth): string
{
if ($array === []) {
return '[]';
}
$result = '[';
$isList = self::isList($array);
foreach ($array as $key => $value) {
$result .= "\n" . str_repeat(self::PRETTY_INDENT, $depth + 1);
if (!$isList) {
$result .= var_export($key, true) . ' => ';
}
if (is_array($value)) {
$result .= self::prettyExportArray($value, $depth + 1);
} else {
$result .= self::prettyExportScalar($value);
}
$result .= ',';
}
return $result . "\n" . str_repeat(self::PRETTY_INDENT, $depth) . ']';
}

private static function prettyExportScalar($value): string
{
return $value === null ? 'null' : var_export($value, true);
}

private static function isList(array &$value): bool
{
if ($value === []) {
return true;
}
if (function_exists('array_is_list')) {
return \array_is_list($value);
}

return array_keys($value) === range(0, count($value) - 1);
}
}
32 changes: 32 additions & 0 deletions tests/ArrayGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function testArrayGenerator()
];

$this->assertSame($expected, $array);

$this->checkFormatting($expected, $translations);
}

public function testArrayGeneratorWithEmptyTranslations()
Expand Down Expand Up @@ -70,5 +72,35 @@ public function testArrayGeneratorWithEmptyTranslations()
];

$this->assertSame($expected, $array);

$this->checkFormatting($expected, $translations, ['includeEmpty' => true]);
}

private function checkFormatting(array $expected, Translations $translations, array $otherOptions = [])
{
foreach ([
[],
['strictTypes' => true],
['pretty' => true],
['strictTypes' => true, 'pretty' => true],
] as $options) {
$phpCode = (new ArrayGenerator($options + $otherOptions))->generateString($translations);
if (empty($options['strictTypes'])) {
$this->assertStringNotContainsString('declare(strict_types=1);', $phpCode);
} else {
$this->assertStringContainsString('declare(strict_types=1);', $phpCode);
}
if (empty($options['pretty'])) {
$this->assertStringEndsWith(');', $phpCode);
$prefix = '<?php ';
} else {
$this->assertStringEndsWith("];\n", $phpCode);
$prefix = "<?php\n";
}
$this->assertStringStartsWith($prefix, $phpCode);
$array = eval(substr($phpCode, strlen($prefix)));
$this->assertIsArray($array);
$this->assertSame($expected, $array);
}
}
}

0 comments on commit 3a9fdda

Please sign in to comment.