git的code_style.php

本文介绍了一个用于规范化上线代码的PHP脚本。该脚本通过分析git提交的更改,使用PHP_CodeSniffer验证PHP文件是否符合PSR2标准,并跳过特定的文件路径。对于控制器文件,还会自动添加命名空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是为了规范上线代码自己写的一个php脚本,在这里做一下备份,以备不时之需。

<?php

    //获取提交的参数
    $fp = fopen('php://stdin', 'r');
    $input = fgets($fp);
    $params = explode(" ", $input);
    $old_commitid = isset($params[0]) ? $params[0] : '';
    $commitid = isset($params[1]) ? $params[1] : '';
    //这里是新创建分支的情况
    if ($old_commitid == '0000000000000000000000000000000000000000') {
        $cmd = "git diff --cached --name-only $commitid";
        $file_names = shell_exec($cmd);
    } else {
        $cmd = "git diff --name-only $commitid $old_commitid";
        $file_names = shell_exec($cmd);
    }
    $files = explode("\n", $file_names);
    $phpcs_path = '/opt/codesniffer/vendor/bin';
    $tmp_content_path = "/tmp/tmp_content.php";
    $err_str = '';
    if ($files) {
        foreach ($files as $_file) {
        $_file = ltrim($_file, 'remote:');
            $_file = trim($_file);
            if (!empty($_file)) {
                if (filter_special($_file)) {
                    continue;
                }
                $tmp_content = shell_exec("git show $commitid:$_file");
                if (match_special($_file)) {
                    $tmp_content = str_replace("<?php", "<?php\nnamespace testnamespace;", $tmp_content);
                }
                file_put_contents($tmp_content_path, $tmp_content);    
        $cmd = '/opt/codesniffer/vendor/bin/phpcs /tmp/tmp_content.php --standard=PSR2';
                $verify_ret = shell_exec($cmd);
                $verify_ret = shell_exec("$phpcs_path/phpcs $tmp_content_path --standard=PSR2");
                $verify_ret = str_replace("$tmp_content_path", "$_file", $verify_ret);
                $err_str .= $verify_ret;
            }
        }
    }
    if (file_exists($tmp_content_path)) {
        unlink($tmp_content_path);
    }
    echo $err_str;

/**
 * 过滤特殊文件
 * @param  [type] $filepath [description]
 * @return [type]           [description]
 */
function filter_special($filepath) {
    if (empty($filepath)) {
        return true;
    }
    $special_str = [
        'Bootstrap.php', 
        'web/index.php',
        'api/index.php',
        'admin/index.php',
        'client/index.php',
        'seller/index.php',
        'app/views',
        'app/modules',
        'app/plugins',
        'app/source',
        'README.md',
        'web/conf/app.ini',
        'library/helper',
        'CMQ'
        ];    
    foreach ($special_str as $_special) {
        if (strpos($filepath, $_special) !== false) {
                return true;
        }    
    }

    return false; 
}

/**
 * 框架controller不能加namespace
 * [match_special description]
 * @param  [type] $filepath [description]
 * @return [type]           [description]
 */
function match_special($filepath) {
    if (empty($filepath)) {
        return false;
    }
    $special_str = [
        'controllers',
        'Controller.php', 
        'Index.php'
    ];    
    foreach ($special_str as $_special) {
        if (strpos($filepath, $_special) !== false) {
                return true;
        }    
    }

    return false; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值