PHP code example of huangdijia / php-coding-standard
1. Go to this page and download the library: Download huangdijia/php-coding-standard library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
huangdijia / php-coding-standard example snippets
use Huangdijia\PhpCsFixer\Config;
use PhpCsFixer\Runner\Parallel\ParallelConfig;
rComment(
projectName: 'your-vendor/your-package',
projectLink: 'https://github.com/your-vendor/your-package',
projectDocument: 'https://github.com/your-vendor/your-package/blob/main/README.md',
contacts: [
'Your Name' => '[email protected] ',
],
)
->setFinder(
PhpCsFixer\Finder::create()
->exclude('public')
->exclude('runtime')
->exclude('vendor')
->in(__DIR__)
->append([
__FILE__,
])
)
->setUsingCache(false);
return (new Config())
->setHeaderComment(/* ... */)
->addRules([
'strict_comparison' => true,
'native_function_invocation' => ['
// 使用名称 => 邮箱的数组
->setHeaderComment(
projectName: 'my-project',
contacts: [
'John Doe' => '[email protected] ',
'Jane Smith' => '[email protected] ',
]
)
// 使用邮箱地址的简单数组
->setHeaderComment(
projectName: 'my-project',
contacts: ['[email protected] ', '[email protected] ']
)
return (new Config())
->setHeaderComment(/* ... */)
->forLaravel() // 应用 Laravel 特定的优化
->enableCache() // 启用缓存以提高性能
->setFinder(/* ... */);
return (new Config())
->setHeaderComment(/* ... */)
->disableRiskyRules() // 使用更安全的规则
->enableCache()
->addRules([
'strict_comparison' => true,
'strict_param' => true,
])
->setFinder(/* ... */);
->setFinder(
PhpCsFixer\Finder::create()
->exclude('bootstrap')
->exclude('storage')
->exclude('vendor')
->exclude('node_modules')
->in(__DIR__)
->name('*.php')
->notPath('database/migrations')
)
bash
cp vendor/huangdijia/php-coding-standard/.php-cs-fixer.php.stub .php-cs-fixer.php
bash
vendor/bin/php-cs-fixer fix
bash
vendor/bin/php-cs-fixer fix --dry-run --diff
bash
# 查看 Laravel 示例
cat vendor/huangdijia/php-coding-standard/examples/laravel.php
# 查看库项目示例
cat vendor/huangdijia/php-coding-standard/examples/library.php
yaml
name: Code Style
on: [push, pull_request]
jobs:
php-cs-fixer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
- run: composer install
- run: vendor/bin/php-cs-fixer fix --dry-run --diff