4

I'm currently using VS Code's latest version 1.41.1 (at the time of writing).

I've installed PHP CS Fixer to auto-format my code. However there's a behavious that I don't like. It always formats the code like this:

if (condition) {
    // code...
} else {
    // code
}

But this is not giving me good readability.

I'd like to achieve this:

if (condition)
{
    // code...
}
else
{
    // code
}

Is there any extentsion that supports this code formatting for PHP in VS Code? Or is there any option in PHP CS Fixer to skip formatting these curly brackets? Any other otpions?

4

2 回答 2

5

根据@Nicolas 的评论,我能够分两步完成它。

  1. 我必须在项目的根目录中创建一个名为.php_cs
  2. 将此代码块添加到文件中:

    <?php
    
    return PhpCsFixer\Config::create()
    ->setRules(array(
        'braces' => array(
            'position_after_anonymous_constructs' => 'next',
            'position_after_control_structures' => 'next',
        )
    ));
    

一切都完成了,就像一个魅力。感谢@Nicolas 的帮助!

于 2020-01-16T13:23:39.583 回答
0

正在寻找同样的东西。我尝试了 Radical_activity 给出的(接受的)答案,但这对我不起作用。在这里找到另一个片段:

https://stackoverflow.com/a/54883745/4638682

片段:

<?php

$finder = PhpCsFixer\Finder::create()
    //->exclude('somedir')
    //->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php'
    ->in(__DIR__)
;

return PhpCsFixer\Config::create()
    ->setRules([
        '@PSR2' => true,
        'strict_param' => false,
        'array_syntax' => ['syntax' => 'long'],
        'braces' => [
            'allow_single_line_closure' => true, 
            'position_after_functions_and_oop_constructs' => 'same'],
    ])
    ->setFinder($finder)
;

您还需要.php_cs在存储库中创建一个文件。这对我有用!

于 2020-10-11T20:18:27.913 回答