improve testability

This commit is contained in:
池下克彦 2020-02-23 12:30:18 +09:00
parent e11202168f
commit fe83f070aa
2 changed files with 80 additions and 61 deletions

View File

@ -1,13 +1,23 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\DocumentGenerator; use PhpOffice\PhpSpreadsheet\DocumentGenerator;
require_once __DIR__ . '/../src/Bootstrap.php'; require_once __DIR__ . '/../src/Bootstrap.php';
try { try {
DocumentGenerator::generateFunctionListByCategory(); $phpSpreadsheetFunctionsProperty = (new ReflectionClass(Calculation::class))->getProperty('phpSpreadsheetFunctions');
DocumentGenerator::generateFunctionListByName(); $phpSpreadsheetFunctionsProperty->setAccessible(true);
$phpSpreadsheetFunctions = $phpSpreadsheetFunctionsProperty->getValue();
ksort($phpSpreadsheetFunctions);
file_put_contents(__DIR__ . '/../docs/references/function-list-by-category.md',
DocumentGenerator::generateFunctionListByCategory($phpSpreadsheetFunctions)
);
file_put_contents(__DIR__ . '/../docs/references/function-list-by-name.md',
DocumentGenerator::generateFunctionListByName($phpSpreadsheetFunctions)
);
} catch (ReflectionException $e) { } catch (ReflectionException $e) {
fwrite(STDERR, (string)$e); fwrite(STDERR, (string)$e);
exit(1); exit(1);

View File

@ -1,64 +1,69 @@
<?php <?php
namespace PhpOffice\PhpSpreadsheet; namespace PhpOffice\PhpSpreadsheet;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Category; use PhpOffice\PhpSpreadsheet\Calculation\Category;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
use UnexpectedValueException; use UnexpectedValueException;
class DocumentGenerator { class DocumentGenerator
{
/** /**
* @param array[] $phpSpreadsheetFunctions
*
* @throws ReflectionException * @throws ReflectionException
*
* @return string
*/ */
public static function generateFunctionListByCategory(): void { public static function generateFunctionListByCategory(array $phpSpreadsheetFunctions): string
ob_start(); {
try { $result = "# Function list by category\n";
echo "# Function list by category\n";
$phpSpreadsheetFunctions = self::getPhpSpreadsheetFunctions();
foreach (self::getCategories() as $categoryConstant => $category) { foreach (self::getCategories() as $categoryConstant => $category) {
echo "\n"; $result .= "\n";
echo "## {$categoryConstant}\n"; $result .= "## {$categoryConstant}\n";
echo "\n"; $result .= "\n";
echo "Excel Function | PhpSpreadsheet Function\n"; $lengths = [20, 42];
echo "--------------------|-------------------------------------------\n"; $result .= self::tableRow($lengths, ['Excel Function', 'PhpSpreadsheet Function']) . "\n";
foreach ($phpSpreadsheetFunctions as $function => $functionInfo) { $result .= self::tableRow($lengths, null) . "\n";
foreach ($phpSpreadsheetFunctions as $excelFunction => $functionInfo) {
if ($category === $functionInfo['category']) { if ($category === $functionInfo['category']) {
echo str_pad($function, 20) $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
. '| ' . self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']) . "\n"; $result .= self::tableRow($lengths, [$excelFunction, $phpFunction]) . "\n";
} }
} }
} }
} finally {
file_put_contents(__DIR__ . '/../../docs/references/function-list-by-category.md', ob_get_clean()); return $result;
} }
}
/** /**
* @return mixed
* @throws ReflectionException * @throws ReflectionException
*/ *
private static function getPhpSpreadsheetFunctions() {
$phpSpreadsheetFunctionsProperty = (new ReflectionClass(Calculation::class))->getProperty('phpSpreadsheetFunctions');
$phpSpreadsheetFunctionsProperty->setAccessible(true);
$phpSpreadsheetFunctions = $phpSpreadsheetFunctionsProperty->getValue();
ksort($phpSpreadsheetFunctions);
return $phpSpreadsheetFunctions;
}
/**
* @return array * @return array
* @throws ReflectionException
*/ */
private static function getCategories(): array { private static function getCategories(): array
{
return (new ReflectionClass(Category::class))->getConstants(); return (new ReflectionClass(Category::class))->getConstants();
} }
private static function getPhpSpreadsheetFunctionText($functionCall): string { private static function tableRow(array $lengths, array $values = null): string
{
$result = '';
foreach (array_map(null, $lengths, $values ?? []) as $i => [$length, $value]) {
$pad = $value === null ? '-' : ' ';
if ($i > 0) {
$result .= '|' . $pad;
}
$result .= str_pad($value ?? '', $length, $pad);
}
return rtrim($result, ' ');
}
private static function getPhpSpreadsheetFunctionText($functionCall): string
{
if (is_string($functionCall)) { if (is_string($functionCall)) {
return $functionCall; return $functionCall;
} }
@ -68,35 +73,39 @@ class DocumentGenerator {
if (is_array($functionCall)) { if (is_array($functionCall)) {
return "\\{$functionCall[0]}::{$functionCall[1]}"; return "\\{$functionCall[0]}::{$functionCall[1]}";
} }
throw new UnexpectedValueException('$functionCall is of type ' . gettype($functionCall) . '. string or array expected');
throw new UnexpectedValueException(
'$functionCall is of type ' . gettype($functionCall) . '. string or array expected'
);
} }
/** /**
* @param array[] $phpSpreadsheetFunctions
*
* @throws ReflectionException * @throws ReflectionException
*
* @return string
*/ */
public static function generateFunctionListByName(): void { public static function generateFunctionListByName(array $phpSpreadsheetFunctions): string
{
$categoryConstants = array_flip(self::getCategories()); $categoryConstants = array_flip(self::getCategories());
ob_start(); $result = "# Function list by name\n";
try {
echo "# Function list by name\n";
$phpSpreadsheetFunctions = self::getPhpSpreadsheetFunctions();
$lastAlphabet = null; $lastAlphabet = null;
foreach ($phpSpreadsheetFunctions as $function => $functionInfo) { foreach ($phpSpreadsheetFunctions as $excelFunction => $functionInfo) {
if ($lastAlphabet !== $function[0]) { $lengths = [20, 31, 42];
$lastAlphabet = $function[0]; if ($lastAlphabet !== $excelFunction[0]) {
echo "\n"; $lastAlphabet = $excelFunction[0];
echo "## {$lastAlphabet}\n"; $result .= "\n";
echo "\n"; $result .= "## {$lastAlphabet}\n";
echo "Excel Function | Category | PhpSpreadsheet Function\n"; $result .= "\n";
echo "--------------------|--------------------------------|-------------------------------------------\n"; $result .= self::tableRow($lengths, ['Excel Function', 'Category', 'PhpSpreadsheet Function']) . "\n";
$result .= self::tableRow($lengths, null) . "\n";
} }
echo str_pad($function, 20) $category = $categoryConstants[$functionInfo['category']];
. '| ' . str_pad($categoryConstants[$functionInfo['category']], 31) $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
. '| ' . self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']) $result .= self::tableRow($lengths, [$excelFunction, $category, $phpFunction]) . "\n";
. "\n";
}
} finally {
file_put_contents(__DIR__ . '/../../docs/references/function-list-by-name.md', ob_get_clean());
} }
return $result;
} }
} }