Creating PHP Interfaces

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

+6
source share
2 answers

For programmatic use, InterfaceDistiller , which allows you to derive interfaces from existing classes as follows:

 $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] <classname> <interfacename> --bootstrap Path to File containing your bootstrap and autoloader --methodsWithModifiers <number> A ReflectionMethod Visibility BitMask. Defaults to Public. --extendInterfaceFrom <name,...> 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 <pattern> Only include methods matching PCRE pattern. --saveAs Filename to save new Interface to. STDOUT if omitted. 

I do not know any IDE that offers such functionality for php.

+15
source

Currently, PHPStorm 8 can do this, and possibly previous versions.

Steps:

  • Place the cursor on the class name
  • Choose: Refactor → Extract → Interface
  • Fill in the parameters and execute.
+3
source

Source: https://habr.com/ru/post/902242/


All Articles