Closed
Description
I noticed when fixing up the CS, that the VariableAnalysisSniff::processVariableAsStaticDeclaration()
uses outdated assumptions on what can be part of an initial value:
// Are we a static declaration?
// Static declarations are a bit more complicated than globals, since they
// can contain assignments. The assignment is compile-time however so can
// only be constant values, which makes life manageable.
This is outdated due to:
- PHP 5.6: initializers can now contain constant expressions, i.e. sums, concatenation etc, as long as the values used are constant.
Ref: https://wiki.php.net/rfc/const_scalar_exprs - PHP 5.6: initializers can now contain constant arrays.
Ref: https://www.php.net/manual/en/migration56.new-features.php#migration56.new-features.const-scalar-exprs (second code sample) - PHP 8.1: as of PHP 8.1,
new
class instantiations are allowed in initializers, as long as any parameters passed evaluate to constant values.
Ref: https://wiki.php.net/rfc/new_in_initializers
Aside from that, magic constants are also allowed (and have been for a long time): https://www.php.net/manual/en/language.constants.magic.php
For the record, "initializers" in this context are:
- Class constant declarations.
- Property declarations
- Default values for function parameters.
- Default values for variables declared via the
static
keyword.
I believe it would be good to review the code base to verify these situation are handled correctly in all relevant places.