Passed
Push — master ( ba186c...8baf10 )
by Lars
03:46
created

TemplateFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 1
c 2
b 1
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/** @noinspection TransitiveDependenciesUsageInspection */
4
5
use voku\build\Template\TemplateFormatter;
6
use voku\helper\UTF8;
7
8
require __DIR__ . '/../vendor/autoload.php';
9
require __DIR__ . '/vendor/autoload.php';
10
11
$phpFiles = \voku\SimplePhpParser\Parsers\PhpCodeParser::getPhpFiles(__DIR__ . '/../src/voku/helper/UTF8.php');
0 ignored issues
show
Bug introduced by
The type voku\SimplePhpParser\Parsers\PhpCodeParser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
$phpClasses = $phpFiles->getClasses();
13
$phpUtf8Class = $phpClasses[UTF8::class];
14
15
// -------------------------------------
16
17
$templateDocument = file_get_contents(__DIR__ . '/docs/base.md');
18
19
$templateMethodParam = <<<RAW
20
- `%param%`
21
RAW;
22
23
/** @noinspection HtmlUnknownAnchorTarget */
24
$templateMethod = <<<RAW
25
## %name%
26
<a href="#class-methods">↑</a>
27
%description%
28
29
**Parameters:**
30
%params%
31
32
**Return:**
33
- `%return%`
34
35
--------
36
37
RAW;
38
39
$templateIndexLink = <<<RAW
40
<a href="%href%">%title%</a>
41
RAW;
42
43
// -------------------------------------
44
45
$functionsDocumentation = [];
46
$functionsIndex = [];
47
48
foreach ($phpUtf8Class->methods as $method) {
49
    assert($method instanceof \voku\SimplePhpParser\Model\PHPMethod);
0 ignored issues
show
Bug introduced by
The type voku\SimplePhpParser\Model\PHPMethod was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
51
    if ($method->access !== 'public') {
52
        continue;
53
    }
54
55
    if ($method->is_deprecated) {
56
        continue;
57
    }
58
59
    if (\strpos($method->name, '_') === 0) {
60
        continue;
61
    }
62
63
    $methodIndexTemplate = new TemplateFormatter($templateIndexLink);
64
65
    $methodTemplate = new TemplateFormatter($templateMethod);
66
67
    // -- params
68
    $params = [];
69
    $paramsTypes = [];
70
    foreach ($method->parameters as $param) {
71
        $paramsTemplate = new TemplateFormatter($templateMethodParam);
72
        $paramsTemplate->set('param', ($param->typeFromPhpDocPslam ?: $param->typeFromPhpDoc) . UTF8::str_replace_beginning($param->typeMaybeWithComment, $param->typeFromPhpDoc, ''));
73
        $params[] = $paramsTemplate->format();
74
        $paramsTypes[] = $param->typeFromPhpDoc . ' ' . '$' . $param->name;
75
    }
76
77
    if (count($params) !== 0) {
78
        $methodTemplate->set('params', implode("\n", $params));
79
    } else {
80
        $methodTemplate->set('params', '__nothing__');
81
    }
82
83
    // -- return
84
85
    $methodWithType = $method->name . '(' . implode(', ', $paramsTypes) . '): ' . $method->returnTypeFromPhpDoc;
86
87
    $description = trim($method->summary . "\n\n" . $method->description);
88
89
    $methodTemplate->set('name', $methodWithType);
90
    $methodTemplate->set('description', $description);
91
    $methodTemplate->set('return', $method->returnTypeMaybeWithComment);
92
93
    $methodIndexTemplate->set('title', $method->name);
94
    $methodIndexTemplate->set('href', '#' . UTF8::css_identifier($methodWithType));
95
96
    $functionsDocumentation[$method->name] = $methodTemplate->format();
97
    $functionsIndex[$method->name] = $methodIndexTemplate->format();
98
}
99
100
ksort($functionsDocumentation);
101
$functionsDocumentation = array_values($functionsDocumentation);
102
103
ksort($functionsIndex);
104
105
// -------------------------------------
106
107
$documentTemplate = new TemplateFormatter($templateDocument);
108
$documentTemplate->set('__functions_list__', implode("\n", $functionsDocumentation));
109
110
$indexLastChar = null;
111
$indexStrResult = '';
112
$counterTmp = 0;
113
foreach ($functionsIndex as $_index => $_template) {
114
    $counterTmp++;
115
116
    if ($counterTmp === 1) {
117
        $indexStrResult .= '<tr>';
118
    }
119
120
    $indexStrResult .= '<td>' . sprintf("%s\n", $_template) . '</td>';
121
122
    if ($counterTmp === 4) {
123
        $counterTmp = 0;
124
        $indexStrResult .= '</tr>';
125
    }
126
}
127
if ($counterTmp > 0) {
128
    $indexStrResult .= '</tr>';
129
}
130
$indexStrResult = '
131
<table>
132
    ' . $indexStrResult . '
133
</table>
134
';
135
136
$documentTemplate->set('__functions_index__', $indexStrResult);
137
138
file_put_contents(__DIR__ . '/../README.md', $documentTemplate->format());
139