Passed
Push — master ( 50d78c...560e67 )
by Adrien
11:11 queued 05:32
created

DocumentGenerator::generateFunctionListByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 21
ccs 17
cts 17
cp 1
crap 3
rs 9.7333
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Category;
6
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7
use ReflectionClass;
8
use ReflectionException;
9
use UnexpectedValueException;
10
11
class DocumentGenerator
12
{
13
    /**
14
     * @param array[] $phpSpreadsheetFunctions
15
     *
16
     * @throws ReflectionException
17
     *
18
     * @return string
19
     */
20 1
    public static function generateFunctionListByCategory(array $phpSpreadsheetFunctions): string
21
    {
22 1
        $result = "# Function list by category\n";
23 1
        foreach (self::getCategories() as $categoryConstant => $category) {
24 1
            $result .= "\n";
25 1
            $result .= "## {$categoryConstant}\n";
26 1
            $result .= "\n";
27 1
            $lengths = [20, 42];
28 1
            $result .= self::tableRow($lengths, ['Excel Function', 'PhpSpreadsheet Function']) . "\n";
29 1
            $result .= self::tableRow($lengths, null) . "\n";
30 1
            foreach ($phpSpreadsheetFunctions as $excelFunction => $functionInfo) {
31 1
                if ($category === $functionInfo['category']) {
32 1
                    $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
33 1
                    $result .= self::tableRow($lengths, [$excelFunction, $phpFunction]) . "\n";
34
                }
35
            }
36
        }
37
38 1
        return $result;
39
    }
40
41
    /**
42
     * @throws ReflectionException
43
     *
44
     * @return array
45
     */
46 2
    private static function getCategories(): array
47
    {
48 2
        return (new ReflectionClass(Category::class))->getConstants();
49
    }
50
51 2
    private static function tableRow(array $lengths, array $values = null): string
52
    {
53 2
        $result = '';
54 2
        foreach (array_map(null, $lengths, $values ?? []) as $i => [$length, $value]) {
55 2
            $pad = $value === null ? '-' : ' ';
56 2
            if ($i > 0) {
57 2
                $result .= '|' . $pad;
58
            }
59 2
            $result .= str_pad($value ?? '', $length, $pad);
60
        }
61
62 2
        return rtrim($result, ' ');
63
    }
64
65 2
    private static function getPhpSpreadsheetFunctionText($functionCall): string
66
    {
67 2
        if (is_string($functionCall)) {
68 2
            return $functionCall;
69
        }
70 2
        if ($functionCall === [Functions::class, 'DUMMY']) {
71 2
            return '**Not yet Implemented**';
72
        }
73 2
        if (is_array($functionCall)) {
74 2
            return "\\{$functionCall[0]}::{$functionCall[1]}";
75
        }
76
77
        throw new UnexpectedValueException(
78
            '$functionCall is of type ' . gettype($functionCall) . '. string or array expected'
79
        );
80
    }
81
82
    /**
83
     * @param array[] $phpSpreadsheetFunctions
84
     *
85
     * @throws ReflectionException
86
     *
87
     * @return string
88
     */
89 1
    public static function generateFunctionListByName(array $phpSpreadsheetFunctions): string
90
    {
91 1
        $categoryConstants = array_flip(self::getCategories());
92 1
        $result = "# Function list by name\n";
93 1
        $lastAlphabet = null;
94 1
        foreach ($phpSpreadsheetFunctions as $excelFunction => $functionInfo) {
95 1
            $lengths = [20, 31, 42];
96 1
            if ($lastAlphabet !== $excelFunction[0]) {
97 1
                $lastAlphabet = $excelFunction[0];
98 1
                $result .= "\n";
99 1
                $result .= "## {$lastAlphabet}\n";
100 1
                $result .= "\n";
101 1
                $result .= self::tableRow($lengths, ['Excel Function', 'Category', 'PhpSpreadsheet Function']) . "\n";
102 1
                $result .= self::tableRow($lengths, null) . "\n";
103
            }
104 1
            $category = $categoryConstants[$functionInfo['category']];
105 1
            $phpFunction = self::getPhpSpreadsheetFunctionText($functionInfo['functionCall']);
106 1
            $result .= self::tableRow($lengths, [$excelFunction, $category, $phpFunction]) . "\n";
107
        }
108
109 1
        return $result;
110
    }
111
}
112