Inspection of overridden function parameters in PHPstorm

 Having just been bitten in the backside by a __constructor that had overridden its SuperClass but missed off some of the SuperClass's parameters; is there a Code Inspection that will warn if an overridden method doesn't use the same number/types of parameters as its parent::method?

0
2 comments

Hi there,

I hope I understood you correctly...

Well -- "Settings/Preferences | Editor | Inspections | PHP | PHP Strict Standard -> Declaration of overridden method should be compatible with parent class"

As for __construct -- they have exception as it's a common thing to have different set of parameters in different classes .. plus PHP itself does not issue a warning in such cases. https://youtrack.jetbrains.com/issue/WI-33675

As for mismatching parameters .. or number of parameters -- "Inspections | PHP | Type Compatibility -> Parameter Type" -- will warn if I omit one parameter that is required (be it constructor or any other method)

There is also "Inspections | PHP | Code Smell -> Parameter number mismatch declaration"

P.S. Consider also installing "Php Inspections (EA Extended)" plugin (if you do not use it already) -- it provides lots of other inspections.

 

Other than that -- please provide some simple code samples to look into.

0
Avatar
Permanently deleted user

Andriy, Hi

Yes - that's exactly the issue.  It's a CakePHP project that had:-

class Model {

// Cake Core Model class
class Model {
public function __construct( $id = false, $table = null, $ds = null )
{
}
}

// Application Class
class Service extends Model {
public function __construct( $request = null, $response = null ) { parent::__construct( $request, $response );
}
}

The developer had mistakenly included the parameters from the Core Controller constructor, and not the Core Model constructor.  Obviously in 99% of cases (when the $ds param is defaulted) it wasn't a problem that they were called something different.... but that's why it took me best part of a week to find!!

As per your link WI-33675 - PHPstorm doesn't warn or highlight the difference *in the case of __construct*.... which is the Gotcha! that caught me out.

I'll know now though!!

Thanks for the quick & full response - all the core settings you recommended were already on; I'll take a look at the plugin too.

0

Please sign in to leave a comment.