Atom-beautify not loading php-cs-fixer custom config
Asked Answered
B

3

16

I have Atom installed with PHP-CS-Fixer plugin. I'm trying to use some custom rules to apply same-line braces style.

I have tried using the in-Atom config option, but couldn't make it work. I have tried setting position_after_functions_and_oop_constructs and putting it in PHP-CS-FIXER Rules in Atom, but didn't work.

Therefore, I have set a custom path to my config, which is C:\xampp\htdocs\myproject\atom.php_cs

The config is:

<?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)
;

It didn't work and Atom is NOT doing a proper beautify. Any idea to enforce the rules?

Notes:

I'm interested in having the following style:

  public function someFunction(){ 
    // code here
}
  • I'm using Windows 10 as OS, Atom is IDE and have PHP-cs-fixer installed via Composer.
Brogle answered 28/2, 2019 at 23:47 Comment(0)
M
6

Since the beautification is not working properly the programm might have run into an error. You can run Atom Beautify: Help Debug Editor from the command pallette to get Debug information.
Your config works perfectly fine for me and the problem seems to be your naming.

Simply rename your atom.php_cs to .php_cs and remove any config file path from the settings.

Mcneill answered 9/3, 2019 at 17:43 Comment(4)
The debug doesn't show anything regarding the php beautification. The issue is that the Atom-beautify is not following my rules and simply using the default ones. Regarding the php_cs path, I have set it in the settings of Atom and used the absolute path of the file with the .php_cs ending.Brogle
@Brogle The issue is that it cannot find your file. If I copy and paste your settings it works perfectly fine. Have you tried to delete the absolute path from the settings and rename the file to .php_cs(without anything else in the name)? It should show at least something in your debug log. Try to scroll all the way to the bottom and look there or search for the word "error" in the logMcneill
Renaming the file doesn't get applied because Windows doesn't accept empty named files.Brogle
Thank you!! The config is correct as you've said. The problem was with the additional naming to the config file. It's really a weird issue. Thanks again!Brogle
B
1

For any newcomers, please note that the new version needs the config file to be named .php-cs-fixer.php. It's so funny that I googled, and saw my question, lol.

Make sure that php-cs-fixer is installed globally with fixer, and restart Atom after you do all these changes to make sure that it's applied.

Also, new config would look like this

<?php

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__)
;

$config = new PhpCsFixer\Config();
$config->setRules([
        '@PSR2' => true,
        'strict_param' => false,
        'array_syntax' => ['syntax' => 'long'],
        'no_spaces_around_offset' => [
          'positions' => [ "inside", "outside" ]
        ],
        'no_spaces_inside_parenthesis' => true,
        'array_indentation' => true,
        'no_extra_blank_lines' => true,
        'object_operator_without_whitespace' => true,
        'multiline_whitespace_before_semicolons' => true,
        'switch_case_space' => true,
        'indentation_type' => true,
        'blank_line_after_namespace' => true,
        "no_break_comment"=> true,
        "no_closing_tag"=> true,
        'switch_case_semicolon_to_colon' => true,
        "no_spaces_after_function_name"=> true,
        "no_trailing_whitespace"=> true,
        "no_trailing_whitespace_in_comment"=> true,
        'no_whitespace_before_comma_in_array'=> true,
        'braces' => [
            'allow_single_line_closure' => true,
            'position_after_functions_and_oop_constructs' => 'same'],
    ])
    ->setFinder($finder);
  return $config;
Brogle answered 22/2, 2022 at 8:3 Comment(2)
What do you mean by "with fixer"?Beverage
I honestly don't remember what I meant with that. Perhaps I meant using Composer or some sort of CLI. EDIT: I believe using the global php-cs-fixer if it works or not, if so, then it's installed globally. That's what I meant.Brogle
B
1

If you have installed the php-cs-fixer like suggested in the official github repo, it's likely that you have installed the current version. At the moment it's V3 (V3.14.3 to be exact).

You can check your php-cs-fixer version by executing

php-cs-fixer -V

Unfortunately, the package atom-beautify (current version V0.33.4) seems to support php-cs-fixer up to version V2, like you can see in the package settings: enter image description here

By using a newer version of the php-cs-fixer, atom-beautify seems to have probelms in finding your local custom config file (i.e. .php-cs-fixer.php, also .php_cs or .php-cs.dist), unless you specify the full path to it in the package settings, which only makes sense, if you are using the same custom configuration all over your projects.

Long story short: you can check whether your php-cs-fixer is working on its own and is loading your local custom configuration file by creating a test php file and executing

php-cs-fixer fix test.php

After that you have three options:

  • Use php-cs-fixer in the command line
  • Downgrade your php-cs-fixer to V2 and use the atom-beautify package
  • Or use a current version of the php-cs-fixer, but use an other atom package instead. I ended up using the package php-cs-fixer by pfefferle
Beverage answered 31/1, 2023 at 12:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.