7.2.4
2.12.1
php php-cs-fixer fix
<?php
$finder = \PhpCsFixer\Finder::create()
->in([__DIR__]);
$config = (new \PhpCsFixer\Config('MyConfig'))
->setRiskyAllowed(false)
->setRules([
'@PSR2' => true,
//Our line endings (LF) are handled by git.
'line_ending' => false
])
->setCacheFile('.php_cs.cache')
->setFinder($finder)
->setFormat('txt')
->setHideProgress(false)
->setIndent(' ')
->setLineEnding("\n")
->setPhpExecutable(null)
->setUsingCache(true)
;
return $config;
Loaded config MyConfig from "C:\MyPath\.php_cs.dist".
Using cache file ".php_cs.cache".
<List of files>
Fixed all files in 0.870 seconds, 14.000 MB memory used
I expect that the program will not convert new lines because I have set the line_ending config setting to false.
It still reformats new line characters. I end up with a git diff of 100 files which aren't actually changed.
I have tried with the order of settings reversed, and with the command line, and checked the README.
The README has a sentence When using combinations of exact and blacklist rules, applying exact rules along with above blacklisted results. I am unable to parse this sentence, sorry, but I used the example code and ended up with php php-cs-fixer fix . --rules=@PSR2,-line_ending which still didn't work.
Say "Use PSR2 except for this rule".
Hi and thanks for your report,
In order to figure out what is going on we're going to need another piece of puzzle.
Let the following be:
<?php // test2.php
file_put_contents(__DIR__.'/test5.php', "<?php\r\necho 1;\r\n");
Lets run the fixer using @PSR2, -line_ending
$ php php-cs-fixer fix --rules=@PSR2,-line_ending tmp/test5.php --dry-run -vvv
Loaded config default.
Using cache file ".php_cs.cache".
F
Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error
1) tmp/test5.php (single_blank_line_at_eof)
Now lets remove the exception on the line_ending rule and run with @PSR2 only
$ php php-cs-fixer fix --rules=@PSR2 tmp/test5.php --dry-run -vvv
Loaded config default.
Using cache file ".php_cs.cache".
F
Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error
1) tmp/test5.php (line_ending)
Based on these results we can see (using the -vvv flag) which fixers want to make changes. It shows that using @PSR2, -line_ending did in fact disabled the rule line_ending.
The question now is; why are the line endings than still changed?
Looking back at the example that get created the file-to-fix we see the line ending in that file will be \r\n. We give this code to the single_blank_line_at_eof (implicit, as a result of using @PSR2).
This rules' task is to make sure there is one empty line at each file, but how does it know you want a \n or \r\n ending?
On default it will be Unix-style \n, so if you want another line ending you'll have to configure that.
To do so you're best of creating a configuration file for your project and set the line ending there.
For more details see:
https://github.com/FriendsOfPHP/PHP-CS-Fixer/tree/v2.12.0#config-file (or search for setLineEnding in the readme).
PS. If this doesn't help, please run the fixer with --dry-run -vvv and share the output so we know which rules do not act as you want those to act :)
Hi SpacePossum, thank you for taking the time to compose your reply. To highlight the issue we're going to need a more complex example. Please note that I am not talking about the single_blank_line_at_eof setting. I can work around that.
My test2.php now looks like:
<?php // test2.php
$action = $argv[1];
if ($action === 'write') {
file_put_contents(__DIR__.'/test5.php', "<?php\r\nnamespace Me;\r\n\r\nclass C\r\n{\r\n}\r\n\r\n//EOF");
} elseif ($action === 'check') {
$str = file_get_contents(__DIR__.'/test5.php');
for ($i=0; $i<strlen($str); $i++) {
print ord(substr($str, $i, 1)) . ' ';
}
print PHP_EOL;
} elseif ($action === 'fix1') {
execCmd('php php-cs-fixer fix --rules=@PSR2 test5.php -vvv');
} elseif ($action === 'fix2') {
execCmd('php php-cs-fixer fix --rules=@PSR2,-line_ending test5.php -vvv');
}
function execCmd($cmd)
{
$output = null;
$return = null;
exec($cmd, $output, $return);
if (!empty($output)) {
foreach ($output as $line) {
print $line . PHP_EOL;
}
}
}
And then run the following script (Sorry I'm on Windows)
@echo off
php test2.php write
echo Checking unaltered file
php test2.php check
php test2.php fix1
echo Performed fix with @PSR2
php test2.php check
php test2.php write
php test2.php fix2
echo Performed fix with @PSR2,-line_ending
php test2.php check
It outputs the ascii codes of each character in each file. I will paste relevant excerpts from my output, and highlight the newline characters:
Checking unaltered file
60 63 112 104 112 13 10 110 97 109 101 115 112 97 99 101 32 77 101 59 13 10 13 10
99 108 97 115 115 32 67 13 10 123 13 10 125 13 10 13 10 47 47 69 79 70
Performed fix with @PSR2
60 63 112 104 112 10 110 97 109 101 115 112 97 99 101 32 77 101 59 10 10
99 108 97 115 115 32 67 10 123 10 125 10 10 47 47 69 79 70 10
All of the \r\n are now \n and it has added the new line to the end. This works as expected.
Performed fix with @PSR2,-line_ending
60 63 112 104 112 13 10 110 97 109 101 115 112 97 99 101 32 77 101 59 10 10
99 108 97 115 115 32 67 10 123 10 125 13 10 13 10 47 47 69 79 70 10
However, that does not work as expected. I can understand the \n being added to the end, however, there are several instances where it has replaced my \r\n with \n even with -line_ending configured.
so, you wanted to have 'r\n, as in your original file, right? awesome !
yet, in your config file, you have this...
->setLineEnding("\n")
As @SpacePossum said already, even if line_ending is disabled and PHP CS Fixer is not converting each new line it'll find, there are still places in other rules that sometimes need new line character to be inserted, for that, to specify which one, there is that setLineEnding config
Fair enough. I'll need to put a setting in my config which detects the OS and sets the newline character accordingly. Thank you for your assistance.
You can use the constant PHP_EOL to detect that!
Thanks for getting back to use @mlambley . Let us know if you've questions about putting your configuration file together, you can chat with us over @ https://gitter.im/PHP-CS-Fixer/Lobby to get a more speedy conversation :)
@ntzm I'm laughing right now, because I should have thought of that. Thanks!
@SpacePossum Cheers mate.