summaryrefslogtreecommitdiff
path: root/scripts/dev/tidy.php
blob: cf153ead55d95a4f3b2ff969de30af6ad0b8c0ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

set_error_handler(function($_, $msg) {
    throw new Exception($msg);
});

$rootDir = __DIR__ . '/../..';
$it = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootDir),
    RecursiveIteratorIterator::LEAVES_ONLY
);

$excludes = [
    // Bundled libraries / files.
    'ext/bcmath/libbcmath/',
    'ext/date/lib/',
    'ext/fileinfo/data_file.c',
    'ext/fileinfo/libmagic/',
    'ext/gd/libgd/',
    'ext/hash/sha3/',
    'ext/hash/hash_whirlpool.c',
    'ext/hash/php_hash_whirlpool_tables.h',
    'ext/mbstring/libmbfl/',
    'ext/mbstring/unicode_data.h',
    'ext/opcache/jit/dynasm',
    'ext/opcache/jit/libudis86',
    'ext/opcache/jit/vtune',
    'ext/pcre/pcre2lib/',
    'ext/standard/html_tables/html_table_gen.php',
    'ext/xmlrpc/libxmlrpc/',
    'sapi/cli/php_http_parser.c',
    'sapi/cli/php_http_parser.h',
    'sapi/litespeed/',
    // Not a PHP file.
    'ext/zlib/tests/data.inc',
    // Flexible HEREDOC/NOWDOC tests are likely whitespace sensitive.
    // TODO: Properly classify them.
    'Zend/tests/flexible-',
];

foreach ($it as $file) {
    if (!$file->isFile()) {
        continue;
    }

    $path = $file->getPathName();
    foreach ($excludes as $exclude) {
        if (strpos($path, $exclude) !== false) {
            continue 2;
        }
    }

    $lang = getLanguageFromExtension($file->getExtension());
    if ($lang === null) {
        continue;
    }

    $origCode = $code = file_get_contents($path);

    if ($lang === 'c') {
        $code = stripTrailingWhitespace($code);
        // TODO: Avoid this for now.
        // $code = reindentToTabs($code);
    } else if ($lang === 'php') {
        $code = stripTrailingWhitespace($code);
        $code = reindentToSpaces($code);
    } else if ($lang === 'phpt') {
        $code = transformTestCode($code, function(string $code) {
            $code = stripTrailingWhitespace($code);
            $code = reindentToSpaces($code);
            return $code;
        });
    }

    if ($origCode !== $code) {
        file_put_contents($path, $code);
    }
}

function stripTrailingWhitespace(string $code): string {
    return preg_replace('/\h+$/m', '', $code);
}

function reindentToTabs(string $code): string {
    return preg_replace_callback('/^ +/m', function(array $matches) {
        $tabSize = 4;
        $spaces = strlen($matches[0]);
        $tabs = intdiv($spaces, $tabSize);
        $spaces -= $tabs * $tabSize;
        return str_repeat("\t", $tabs) . str_repeat(" ", $spaces);
    }, $code);
}

function reindentToSpaces(string $code): string {
    return preg_replace_callback('/^[ \t]+/m', function(array $matches) {
        $tabSize = 4;
        $indent = 0;
        foreach (str_split($matches[0]) as $char) {
            if ($char === ' ') {
                $indent++;
            } else {
                $partialIndent = $indent % $tabSize;
                if ($partialIndent === 0) {
                    $indent += $tabSize;
                } else {
                    $indent += $tabSize - $partialIndent;
                }
            }
        }
        return str_repeat(" ", $indent);
    }, $code);
}

function transformTestCode(string $code, callable $transformer): string {
    // Don't transform whitespace-sensitive tests.
    if (strpos($code, '--WHITESPACE_SENSITIVE--') !== false) {
        return $code;
    }

    return preg_replace_callback(
        '/(--FILE--)(.+?)(--[A-Z_]+--)/s',
        function(array $matches) use($transformer) {
            return $matches[1] . $transformer($matches[2]) . $matches[3];
        },
        $code
    );
}

function getLanguageFromExtension(string $ext): ?string {
    switch ($ext) {
    case 'c':
    case 'h':
    case 'cpp':
    case 'y':
    case 'l':
    case 're':
        return 'c';
    case 'php':
    case 'inc':
        return 'php';
    case 'phpt':
        return 'phpt';
    default:
        return null;
    }
}