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
<?php
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\DocumentGenerator;
require_once __DIR__ . '/../src/Bootstrap.php';
try {
DocumentGenerator::generateFunctionListByCategory();
DocumentGenerator::generateFunctionListByName();
$phpSpreadsheetFunctionsProperty = (new ReflectionClass(Calculation::class))->getProperty('phpSpreadsheetFunctions');
$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) {
fwrite(STDERR, (string)$e);
exit(1);

View File

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