genAPIDoc()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 2
b 0
f 0
nc 8
nop 6
dl 0
loc 27
rs 9.7333
1
<?php
2
3
/*
4
 * This file is part of the Valdi package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
$doxphpPath = getenv('DOXPHPPATH');
13
14
$classHierarchyToctree = [
15
    'Valdi\\\\Validator\\\\AbstractArray' => '.. toctree::
16
17
   Collection
18
   Nested',
19
    'Valdi\\\\Validator\\\\AbstractComparator' => '.. toctree::
20
21
   Between
22
   LengthBetween
23
   Max
24
   MaxLength
25
   Min
26
   MinLength
27
   Regexp
28
   Value',
29
    'Valdi\\\\Validator\\\\AbstractDateTimeComparator' => '.. toctree::
30
31
   AfterDateTime
32
   BeforeDateTime
33
   DateTimeBetween
34
   InTheFuture
35
   InThePast
36
   OlderThan
37
   YoungerThan',
38
    'Valdi\\\\Validator\\\\AbstractFilter' => '.. toctree::
39
40
   Email
41
   Floating
42
   Integer
43
   IP
44
   IPv4
45
   IPv6
46
   Url',
47
    'Valdi\\\\Validator\\\\AbstractParametrizedValidator' => '.. toctree::
48
49
   AbstractComparator
50
   AbstractDateTimeComparator',
51
    'Valdi\\\\Validator\\\\Regexp' => '.. toctree::
52
53
   Alphabetical
54
   AlphaNumerical
55
   Slug'
56
];
57
58
function scanFiles($dir) : array
59
{
60
    echo "Scanning $dir\n";
61
    $result = [];
62
    $files = scandir($dir);
63
    $omit = ['.', '..', '.DS_Store'];
64
    foreach ($files as $file) {
65
        $completeFile = $dir.DIRECTORY_SEPARATOR.$file;
66
        if (in_array($file, $omit)) {
67
            continue;
68
        }
69
        if (is_file($completeFile)) {
70
            $result[] = $completeFile;
71
        }
72
        if (is_dir($completeFile)) {
73
            $result = array_merge($result, scanFiles($completeFile));
74
        }
75
    }
76
    return $result;
77
}
78
79
function genAPIDoc($doxphpPath, $baseDir, $targetDir, $baseNamespace, $classHierarchyToctree, $file)
80
{
81
    $cmd = $doxphpPath.'/doxphp < "'.$file.'" | '.$doxphpPath.'/doxphp2sphinx';
82
    $doc = shell_exec($cmd);
83
84
    $fileWithoutExtension = substr($file, strlen($baseDir) + 1, strlen($file) - strlen($baseDir) - 5);
85
    $fullClassname = $baseNamespace.'\\\\'.str_replace('/', '\\\\', $fileWithoutExtension);
86
87
    $headlineSeparator = '';
88
    $fullClassnameLength = strlen($fullClassname);
89
    for ($i = 0; $i < $fullClassnameLength; ++$i) {
90
        $headlineSeparator .= '-';
91
    }
92
93
    $headline = "$headlineSeparator\n$fullClassname\n$headlineSeparator\n";
94
95
    if (array_key_exists($fullClassname, $classHierarchyToctree)) {
96
        $headline .= "\n".$classHierarchyToctree[$fullClassname]."\n";
97
    }
98
99
    $targetFile = $targetDir.'/'.$fileWithoutExtension.'.rst';
100
    $targetClassDir = dirname($targetFile);
101
    if (!is_dir($targetClassDir)) {
102
        mkdir($targetClassDir, 0755, true);
103
    }
104
105
    file_put_contents($targetFile, $headline."\n".$doc."\n");
106
107
}
108
109
$baseDir = 'src/Valdi';
110
$files = scanFiles($baseDir);
111
112
$targetDir = 'docs/api';
113
114
shell_exec('rm -r '.$targetDir);
115
foreach ($files as $file) {
116
    genAPIDoc($doxphpPath, $baseDir, $targetDir, 'Valdi', $classHierarchyToctree, $file);
117
}
118