PHP (IntelliJ/PhpStorm inspection): "Method should be final"

127 Views Asked by At

Since a month or something like this the IntelliJ/PhpStorm inspection "Method should be final" is shown on almost any function as a warning. I know I can disable certain inspections I just wonder why they have introduced it?

Definition of a final method in PHP:

A method is considered a final method if it is prefixed with the final keyword. The final methods are the methods that cannot be overridden. So, these methods can’t be overridden in child/subclasses.

So why should I actively prevent this on any method? It simply means more work if you have to remove the keyword when creating subclasses. If it is critical that a method is not overridden then of course you add the final keyword, but is there ANY good reason to do this for all methods?

1

There are 1 best solutions below

1
Roman Bakirov On

Final in method declaration helps to understand that this method not overridden anywhere.

In latest PHP release core team give us many abilities for make code more strict & clear.

Yes, you can see if method overridden in IntelliJ / PHPStorm, but if you see code in web browser or in terminal you can't id.

Also it helpful for make code review. If reviewers see that final was deleted - they pay attention, but if final was not set they see only new method.

So I think that is best practice to make your app classes final by default, or make other methods final if you have only one overridden method. Sorry for my bad english.