Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overrides the file with some log output? #5

Open
muuvmuuv opened this issue Jun 22, 2018 · 15 comments
Open

Overrides the file with some log output? #5

muuvmuuv opened this issue Jun 22, 2018 · 15 comments
Assignees
Labels
bug Something isn't working high priority

Comments

@muuvmuuv
Copy link

normal .php files are working fine, but when I select a plain text as php template and run the formatter this weird thing happen:

Before:

<?php

return PhpCsFixer\Config::create()
  ->setRiskyAllowed(true)
  ->setIndent('  ')
  ->setLineEnding("\n")
  ->setRules([
    '@Symfony' => true,
    'indentation_type' => true,
    'array_indentation' => true,
    // 'array_syntax' => ['syntax' => 'short'],
    'dir_constant' => true,
    'heredoc_to_nowdoc' => true,
    'linebreak_after_opening_tag' => true,
    'modernize_types_casting' => true,
    'semicolon_after_instruction' => true,
    'align_multiline_comment' => true,
    'doctrine_annotation_array_assignment' => true,
    'general_phpdoc_annotation_remove' => true,
    'list_syntax' => true,
    'single_line_comment_style' => true,
    'combine_consecutive_unsets' => true,
    'method_separation' => true,
    'no_multiline_whitespace_before_semicolons' => true,
    'single_quote' => true,
    'concat_space' => ['spacing' => 'one'],
    'phpdoc_align' => true,
    'phpdoc_annotation_without_dot' => true,
    'phpdoc_indent' => true,
    'phpdoc_inline_tag' => true,

    'binary_operator_spaces' => [
      'align_double_arrow' => false,
      'align_equals' => false,
    ],
    'braces' => [
      'allow_single_line_closure' => true,
    ],
    'declare_equal_normalize' => true,
    'function_typehint_space' => true,
    'hash_to_slash_comment' => true,
    'include' => true,
    'lowercase_cast' => true,
    'no_extra_consecutive_blank_lines' => [
      'curly_brace_block',
      'extra',
      'parenthesis_brace_block',
      'square_brace_block',
      'throw',
      'use',
    ],
    'single_blank_line_before_namespace' => true,
    'ternary_operator_spaces' => true,
    'trim_array_spaces' => true,
    'unary_operator_spaces' => true,
    'whitespace_after_comma_in_array' => true,

    'no_multiline_whitespace_before_semicolons' => true,
    'no_unreachable_default_argument_value' => true,
    'no_useless_else' => true,
    'no_useless_return' => true,
    'no_php4_constructor' => true,
    'no_short_echo_tag' => true,

    'ordered_class_elements' => true,
    'ordered_imports' => true,

    'phpdoc_add_missing_param_annotation' => true,
    'phpdoc_order' => true,
    'phpdoc_types_order' => true,

    'doctrine_annotation_braces' => true,
    'doctrine_annotation_indentation' => true,
    'doctrine_annotation_spaces' => true,
  ]);

After:

No fixable errors were found

Time: 34ms; Memory: 4Mb

So it outputs some log into the file and removes everything

@Glavin001
Copy link
Member

UGH. You're kidding me. 🔥 PHPCBF writes to stdout the file contents when there are changes. Except if there are no changes, writes out this error message. That's terrible.

@muuvmuuv I'd really appreciate a Pull Request adding unit tests -- reproducing the error you're experiencing -- and hopefully a bug fix, too.

Example test: https://github.com/Unibeautify/beautifier-php-codesniffer/tree/master/test/preferBeautifierConfig/subFolder

The applicable code for CLI args to phpcbf can be found at https://github.com/Unibeautify/beautifier-php-codesniffer/blob/master/src/index.ts#L93

Thank you!

@Glavin001 Glavin001 added bug Something isn't working high priority labels Jun 23, 2018
@muuvmuuv
Copy link
Author

Oh, that is bad. 😄. I‘ll create a unit test if I have time, but I think I‘ll not finding time fixing it. This would take too long to dig into.

Should we refer an issue on phpcf to this issue?

@muuvmuuv
Copy link
Author

@Glavin001 I can not get a test working because I don‘t know how to change the files language programmatically (I don‘t event think it is possible), but I found some flags that could help maybe.

 -n    Do not fix warnings (shortcut for --warning-severity=0)
 -q    Quiet mode; disables progress and verbose output

maybe they will fix it? I‘ll try them in my brake later

@Glavin001
Copy link
Member

I don‘t know how to change the files language programmatically

Could you elaborate? I do not understand what you're having trouble trying to accomplish.

maybe they will fix it? I‘ll try them in my brake later

Let's hope. I have no clue, since I do not personally use PHPCBF 😆.

@Glavin001
Copy link
Member

Should we refer an issue on phpcf to this issue?

I don't think so. Feel free, however, I suspect they did this for their own reasons. And we also want to support old and new versions of PHPCBF, so in the end we need at least a workaround on our (Unibeautify) end 👎 .

@muuvmuuv
Copy link
Author

Ok I tried it and it changes nothing. I‘ll be fine for now as it is just happening when I have a txt language file that I manually change into a php file. Normal php language files are working fine 😁

@Glavin001
Copy link
Member

Ok I tried it and it changes nothing.

What did you try?

I would like to add a unit tests so we can see this issue and fix it.

@muuvmuuv
Copy link
Author

I tried to add the flags I commented above.

So to reproduce create a .phpcs file with the content in my first comment and run unibeautify formatter after changing the language from plain text to php.

@muuvmuuv
Copy link
Author

muuvmuuv commented Nov 7, 2018

Turns out I used the wrong Beautifier:

PHP_CodeSniffer uses .phpcs.xml
PHP-CS-Fixer uses .php_cs

I was so confused haha. The bug still persists but it has nothing to do with my config I posted above. I'll try to add tests today and maybe a fix.

@Glavin001
Copy link
Member

@Vader699 found this issue, too.

Duplicate: Unibeautify/unibeautify#224

@Vader699
Copy link

Vader699 commented Apr 3, 2019

Looks like the same issue. I switched over to PHP-CS-Fixer and it does not overwrite the file like PHP_CodeSniffer does.

@muuvmuuv
Copy link
Author

muuvmuuv commented Apr 3, 2019

Haven't looked into this (no time...), but I think we would just need to check the output against a regex to determine if it is an error output and return false instead of replacing the content.

@Glavin001
Copy link
Member

Applicable code: https://github.com/Unibeautify/beautifier-php-codesniffer/blob/master/src/index.ts#L105

Pull Requests very welcome!

@Vader699
Copy link

Vader699 commented Apr 7, 2019

Stupid question(probably), what's the best way to run this to test changes?

@muuvmuuv
Copy link
Author

muuvmuuv commented Apr 7, 2019

@Vader699 have a look into my PR, I think i have created tests and a file where it happens.

Sent with GitHawk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority
Projects
None yet
Development

No branches or pull requests

3 participants