barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.17k stars 1.16k forks source link

As a Developer working on Windows I want the Line-Endings inside the generated PHP Doc to be CRLF #1561

Closed ENG3PLabs closed 3 months ago

ENG3PLabs commented 3 months ago

Summary

When checking out my project in Windows, git transforms line endings to CRLF since I have core.autocrlf set to true inside my git config.

When I rerun the command to update a model mixin, all my models get touched since the tool changes the line endings from CRLF to LF where the php docs are. This makes the files show up as changed on git status. Since the actual content did not change, I would rather not have them show up there.

I propose to either

ModelsCommand.php:1012

      if ($this->write) {
          $modelDocComment = $this->write_mixin ? $mixinDocComment : $docComment;
          // make sure to disregard differences between CRLF and LF by forcing original doc to LF
          $originalDocLinux = str_replace("\r\n", "\n", $originalDoc);
          $filename = $reflection->getFileName();
          if ($originalDocLinux != $modelDocComment) {
              $contents = $this->files->get($filename);
              if ($originalDoc) {
                  $contents = str_replace($originalDoc, $modelDocComment, $contents);
              } else {
                  $replace = "{$modelDocComment}\n";
                  $pos = strpos($contents, "final class {$classname}") ?: strpos($contents, "class {$classname}");
                  if ($pos !== false) {
                      $contents = substr_replace($contents, $replace, $pos, 0);
                  }
              }
              if ($this->files->put($filename, $contents)) {
                  $this->info('Written new phpDocBlock to ' . $filename);
              }
          } else {
              $this->info('Skipped writing phpDocBlock to ' . $filename . ' since it is already up to date');
          }
      }

However, the same issue goes for the model file itself when using the mixin option as well as (probably) also the other commands.

mfn commented 3 months ago

PHP_EOL in this package alone won't solve it, some of the \n comes from the other package https://github.com/barryvdh/ReflectionDocBlock too (sibling package, but still).

I wonder if an external tool like https://cs.symfony.com/doc/rules/whitespace/line_ending.html could solve this in a better way.

Many tools modifying the source suffer this or similar problems. To give a prominent example: rector is a fantastic tool to automatically format/rewrite your code. But even they recommend to use it together in a pipeline with a dedicated code formatting tools (they recommend easy-coding-standard AFAIK).

I see ide-helper similar: you are supposed to have a tool chain in place which takes care of this formatting issues.

For example, in a project I use ide-helper, I wouldn't be able to properly use it if I hadn't rector and php-cs-fixer with it in the pipeline, as it would also generate code blocks not matching the required formatting for the project (e.g. class imports).

I also wonder why, after so many years, this is (I believe) the first time this comes up. Do you think you can look into external tooling?

ENG3PLabs commented 3 months ago

Hey, thanks for your answer!

What a shame, that there is such a rat-tail, but I agree: fixing it on your end if something down the line is causing the problem is not what we should be doing.

By "looking into external tooling" you mean, if I would be ok to just work around it on my end with the tools that you mentioned? Definitely!

ENG3PLabs commented 3 months ago

Update: In the end, it was nothing to be worried about in the first place. When I finally tried to commit the files with the seemingly unwanted changes, git auto-crlf took care of them and only the actual changes were commited.

Case closed.