You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PHP has tried to deprecate the use of is_a() a couple of times already, so it is probably a good idea to have a sniff which can detect and (whenever possible) auto-fix any calls to is_a() with instanceof comparisons.
Additional reasons why this sniff would be a nice idea:
Performance: using a PHP native operator will generally be faster than a function call, though it is a micro-optimization.
IDE support: IDEs will be able to automatically fix/replace all uses of a class name when a class is renamed when the class name is used like a plain name and will, in most cases, not do so when a class name is in a text string.
// Bad.if ( is_a( $var, 'MyClass' ) ) {}
if ( is_a( $var, MyClass::class ) ) {}
if ( is_a( $var, self::class ) ) {}
// Okay.if ( $varinstanceof \MyClass ) {}
if ( $varinstanceof MyClass ) {} // If file is non-namespaced and/or there is an import `use` statement for the class.if ( $varinstanceof self ) {}
// Should probably be ignored by this sniff, or only flagged, but not auto-fixed ?if ( is_a( $var, get_class( $obj ) ) ) {}
if ( is_a( $var, get_parent_class( $obj ) ) ) {}
Additional context (optional)
The sniff will need to take namespace/use statement resolution into account and be wary of creative code in the second parameter of the call to is_a() as it should be careful not to create bugs when auto-fixing.
Also note that since PHP 8.0 $obj::class is supported, so is_a( $var, $obj::class ) should be replaceable by ( $var instanceof $obj ).
Also be wary of the PHP 8.0 change that instanceof can then be used in arbitrary expressions, which means this wasn't allowed before and the auto-fixer should take this into account and be wary of this when auto-fixing.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem?
PHP has tried to deprecate the use of
is_a()
a couple of times already, so it is probably a good idea to have a sniff which can detect and (whenever possible) auto-fix any calls tois_a()
withinstanceof
comparisons.Additional reasons why this sniff would be a nice idea:
Refs:
Describe the solution you'd like
A new sniff in the
Modernize
standard.Some example code:
Additional context (optional)
The sniff will need to take namespace/use statement resolution into account and be wary of creative code in the second parameter of the call to
is_a()
as it should be careful not to create bugs when auto-fixing.Also note that since PHP 8.0
$obj::class
is supported, sois_a( $var, $obj::class )
should be replaceable by( $var instanceof $obj )
.Also be wary of the PHP 8.0 change that
instanceof
can then be used in arbitrary expressions, which means this wasn't allowed before and the auto-fixer should take this into account and be wary of this when auto-fixing.The text was updated successfully, but these errors were encountered: