Skip to content
Snippets Groups Projects
Select Git revision
  • upstream/8.0.27
  • debian/main/7.4 default
  • debian/main/8.2
  • debian/main/8.1
  • pristine-tar
  • upstream/8.1
  • debian/main/8.0
  • upstream/8.0
  • debian/main/7.3
  • debian/main/7.2
  • debian/main/7.1
  • debian/main/7.0
  • debian/main/5.6
  • upstream/8.2
  • debian/bullseye/7.4
  • upstream/7.4
  • upstream/7.3
  • debian/buster/7.3
  • upstream/7.2
  • debian/stretch/7.0
  • upstream/7.1
  • upstream/8.1.16+repack
  • upstream/8.0.28
  • upstream/8.2.3
  • debian/8.2.2-3
  • debian/8.2.2-1
  • upstream/8.1.15
  • upstream/8.2.2
  • debian/8.2.1-1
  • upstream/8.1.14
  • upstream/8.2.1
  • debian/8.2.0-4
  • debian/8.2.0-1
  • upstream/8.2.0
  • debian/1%8.0.26-1
  • upstream/8.0.26
  • upstream/8.2.0_rc7
  • upstream/8.1.13
  • debian/7.4.33-1+deb11u1
  • debian/1%7.4.33-1
40 results

tidy.php

Blame
  • Forked from Debian PHP Team / php
    1650 commits behind, 18 commits ahead of the upstream repository.
    tidy.php 3.93 KiB
    <?php
    
    set_error_handler(function($_, $msg) {
        throw new Exception($msg);
    });
    
    if ($argc > 1) {
        $dir = $argv[1];
    } else {
        $dir = __DIR__ . '/../..';
    }
    if (!is_dir($dir)) {
        echo "Directory $dir does not exist.\n";
        exit(1);
    }
    
    $it = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($dir),
        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',
        '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|SKIPIF|CLEAN)--)(.+?)(?=--[A-Z_]+--)/s',
            function(array $matches) use($transformer) {
                return $matches[1] . $transformer($matches[2]);
            },
            $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;
        }
    }