Php-cs-fixer: [Idea] Use SPL interface Countable whenever it's possible

Created on 17 Feb 2020  Â·  5Comments  Â·  Source: FriendsOfPHP/PHP-CS-Fixer

Hello all,

I have an idea and I'm opening this issue to gather some feedback about it.

Let's say you have this interface:

<?php

namespace App;

interface Cart
{
  public function foo();
  public function bar();
  public function count();
}

Do you think it would be a good idea to have a fixer that transforms it into this:

<?php

namespace App;

interface Cart extends \Countable
{
  public function foo();
  public function bar();
}

Basically, the idea is to check if the interface implements methods from already existing SPL interfaces (here \Countable) , and use them whenever it's possible.

Let me know what you think, I'll see if I can dedicate some time for this thing if it worth it.

kinfeature request

All 5 comments

I like it, yet I think it pretty rare, not 100 % sure for the core project, but lets see indeed :)

IMO it's a very rare scenario which has exceptions PHP CS Fixer will not be able to handle reliably:

  • having a count() method does not mean implementing Coutable is intended;
  • the class/interface does not implement Coutable directly but it may extend a class/implement another interface that does, PHP CS Fixer cannot detect that.

Also the missing interface can probably be detected by a static analysis tool such as PHPStan.

For those reasons I would not implement this feature here.

👎 for this. This is not a code style thing. This is a task for a code refactoring tool.

I'm closing this for now as the case is out of scope of the project, however I invite anyone to keep discussing here and we can always reopen when we feel different about it in the future, thanks for the idea!

Thanks for your feedback guys, much appreciated!

Was this page helpful?
0 / 5 - 0 ratings