From fe83f070aa404bc954c515fc8b440c7a618a3147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=A0=E4=B8=8B=E5=85=8B=E5=BD=A6?= Date: Sun, 23 Feb 2020 12:30:18 +0900 Subject: [PATCH] improve testability --- bin/generate-document | 14 ++- src/PhpSpreadsheet/DocumentGenerator.php | 127 ++++++++++++----------- 2 files changed, 80 insertions(+), 61 deletions(-) diff --git a/bin/generate-document b/bin/generate-document index a68a1361..10ac8118 100755 --- a/bin/generate-document +++ b/bin/generate-document @@ -1,13 +1,23 @@ #!/usr/bin/env php 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); diff --git a/src/PhpSpreadsheet/DocumentGenerator.php b/src/PhpSpreadsheet/DocumentGenerator.php index 010d1325..de6f313f 100644 --- a/src/PhpSpreadsheet/DocumentGenerator.php +++ b/src/PhpSpreadsheet/DocumentGenerator.php @@ -1,64 +1,69 @@ $category) { - echo "\n"; - echo "## {$categoryConstant}\n"; - echo "\n"; - echo "Excel Function | PhpSpreadsheet Function\n"; - echo "--------------------|-------------------------------------------\n"; - foreach ($phpSpreadsheetFunctions as $function => $functionInfo) { - if ($category === $functionInfo['category']) { - echo str_pad($function, 20) - . '| ' . self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']) . "\n"; - } + public static function generateFunctionListByCategory(array $phpSpreadsheetFunctions): string + { + $result = "# Function list by category\n"; + foreach (self::getCategories() as $categoryConstant => $category) { + $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']) { + $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(); - $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"; - } - echo str_pad($function, 20) - . '| ' . str_pad($categoryConstants[$functionInfo['category']], 31) - . '| ' . self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']) - . "\n"; + $result = "# Function list by name\n"; + $lastAlphabet = null; + 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"; } - } 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; } -} \ No newline at end of file +}