Skip to main content

Generate PHP Interfaces

Is there a tool the generate php interface from existing classes? It would be nice to have a tool like Netbeans automatic getter/setter creation but for interfaces.

Solved

For programmatic usage there is InterfaceDistiller that allows you to derive interfaces from existing classes like this:

$distiller = new InterfaceDistiller;
$distiller
    ->methodsWithModifiers(\ReflectionMethod::IS_PUBLIC)
    ->extendInterfaceFrom('Iterator, SeekableIterator')
    ->excludeImplementedMethods()
    ->excludeInheritedMethods()
    ->excludeMagicMethods()
    ->excludeOldStyleConstructors()
    ->filterMethodsByPattern('(^get)')
    ->saveAs(new SplFileObject('MyInterface.php'))
    ->distill('SomeFoo', 'MyInterface');

It also has a CLI interface:

Usage: phpdistill [options]  

  --bootstrap                           Path to File containing your bootstrap and autoloader

  --methodsWithModifiers        A ReflectionMethod Visibility BitMask. Defaults to Public.
  --extendInterfaceFrom       Comma-separated list of Interfaces to extend.
  --excludeImplementedMethods           Will exclude all implemented methods.
  --excludeInheritedMethods             Will exclude all inherited methods.
  --excludeMagicMethods                 Will exclude all magic methods.
  --excludeOldStyleConstructors         Will exclude Legacy Constructors.
  --filterMethodsByPattern     Only include methods matching PCRE pattern.
  --saveAs                              Filename to save new Interface to. STDOUT if omitted.

I'm not aware of any IDE that offers such functionality for php.


Currently PHPStorm 8 can do this, maybe prior versions as well.

Steps:

  1. Place your cursor on the class name
  2. Choose: Refactor -> Extract -> Interface
  3. Fill out the options and done.

Comments

Popular posts from this blog

How can I detect Gnome Terminal?

I am writing a console application which makes use of the F1 key (for help). Unfortunately, while Konsole (of KDE) doesn't use this key, Gnome Terminal does, so the F1 key becomes inaccessible to my application. I don't think there's a way to detect whether the F1 key is already mapped in the GUI side of things (Gnome Terminal), but if there is, the answer to that will obviate this question. :) Ergo, my next best bet is to try to detect whether I am running inside Gnome Terminal. Is there some way to do that? I'm primarily interested in gleaning this from within Ruby, but if it can be done via shell or environment variables, or virtual filesystem (/proc, /dev, etc.) then that will suffice. I'm hoping for a reliable way to do this, but I can settle for "best guess" approaches like grepping the environment variables for clues that can let me reasonably assume that Gnome Terminal is the wrapping terminal. Extra info: other keys are also "sto...