it would be great if php-cs-fixer could check the composer.json of the project it is fixing and use this information to e.g. warn the user or maybe even adjust the fixing strategy.
my initial problem: I recently CS-Fixed' a project which requires php <= 5.4 compat with a php7 binary invoking the php-cs-fixer. so while fixing get_called_class() was replaced with static::class. this leads to a syntax error.
I assume this can easily also happen when fixing a php <= 7.0 projects with a php 7.2 based fixing run.
could the php-cs-fixer warn me in this case?
at best it would not do something which leads to a php syntax error.
Isn't it up to the user to define the fixers to use, and thus to know which fixers to not enable?
I agree that perhaps being able to configure a php version might be a good idea, instead it now uses the version it's running on (which might be newer in dev, that's not uncommon). That would require a rather big refactor I think, and I'd still opt for the user needing to configure that in the config, and not getting it from composer. There might not even be a composer config.
On top of that though, this project currently supports 5.6 and up, so anything pre 5.6 is technically unsupported so even with proposed changes, that would change nothing for your current situation.
I also encountered this problem, that it is impossible to separately specify the version of php for which fixers are used.
The version of the interpreter from which the utility is running is used.
This is a configuration inconvenience. Always keep an eye on whether the unsupported rule is enabled.
It would be very convenient to separately set the version of php in the config, and do not worry that unsupported fixers will be used.
What you mean by unsupported fixer being use ? If you set fixer to be used that requires php 7, but you run on php 5, the rule will not be used. Nothing to do manually for that
I mean that the code is executed on the server, and edited on the desktop. Server and desktop versions of php are different.
Running fixers on the desktop uses the desktop version of php (https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/37cc9c62f71a4cd49d9427aea729954f0ca35fe2/src/Fixer/Whitespace/HeredocIndentationFixer.php#L70) and there is no way to specify a different one.
In the case of composer, it is possible to explicitly specify the version of php and the composer will install dependencies in accordance with the specified version.
P.S. Please do not suggest to use the identical php version, when working with several projects to keep several versions of php is extremely inconvenient.
Similar issue for me. Recent versions of PHP-CS-Fixer have added support for using multi-byte version of string functions, specifically, mb_str_split() instead of str_split(). But: that new function is coming with 7.4. I am using 7.3 of PHP, so the mb_str_split() is not available yet.
hi @peter-hoa , please see #4529 .
it's quite complex to make code syntax for php v7.2 when running on 7.4 (or write 5.6 syntax when executing fixer on 7.x), yet we can at least add a warning in CLI that PHP CS Fixer should be called by same environment that project being fixed is running on
I've myself created shared fixer configuration project, which extracts version from composer.lock or composer.json and decides how to enable fixers.
class PlatformRules implements RuleInterface {
/** @var PlatformVersion */
private $platform;
public function __construct($dir) {
$this->platform = new PlatformVersion($dir);
}
/**
* @param RuleBuilder $builder
*/
public function apply(RuleBuilder $builder) {
$php54 = $this->platform->satisfies('>=5.4');
$php70 = $this->platform->satisfies('>=7.0');
$php71 = $this->platform->satisfies('>=7.1');
$builder['array_syntax'] = ['syntax' => $php54 ? 'short' : 'long'];
// "self::" accessor in closures requires php >= 5.4, not safe to always enable
$builder['self_accessor'] = $php54;
$builder['combine_nested_dirname'] = $php70;
$builder['void_return'] = $php71;
}
}
note to self: I should opensource that project.
that sounds really useful. I am curious.
open-sourced now: https://github.com/glensc/php-cs-fixer-config
needs more work to for general purpose use, currently applies my favorite fixers, and nothing is documented :)
Most helpful comment
Isn't it up to the user to define the fixers to use, and thus to know which fixers to not enable?
I agree that perhaps being able to configure a php version might be a good idea, instead it now uses the version it's running on (which might be newer in dev, that's not uncommon). That would require a rather big refactor I think, and I'd still opt for the user needing to configure that in the config, and not getting it from composer. There might not even be a composer config.
On top of that though, this project currently supports 5.6 and up, so anything pre 5.6 is technically unsupported so even with proposed changes, that would change nothing for your current situation.