Extracting just the interface from a PHP application, for code completion
this is my first question.. be gentle :)
I am part of the development team for a PHP application (that is I am building the application). We shall call this our 'core code'. We have a separate team who use the core code on a day to day basis, they develop other sites using the core code.
Now here's the thing, currently we give our developers access to the core code and all is fine, but in the future we are planning to document the code using PHPdoc, and then we won't need to give them the actual code.
The problem then becomes that if our developers don't have the actual code then their IDE's will no longer give them the benefit of code completion.
Now, I use Netbeans, and I notice that it stores files such as Standard.php, these simply contain 开发者_Go百科a huge list of functions and their PHP doc above, my question is this:
does anybody know of a way I can generate such a set of files? They don't need to contain any code, they should strip out the actual PHP, and just leave behind all the classes and PHPdoc.
Any help is greatly appreciated. Thanks.
Well, I can understand why you want to do this. But it will make debugging MUCH more difficult (since they can't trace the calls through the core code.
But if you really want to do this, you can create a generator that uses token_get_all
to fetch all the tokens from each file, and write a new one with only the appropriate tokens left (the interface, with empty code blocks)...
Either that, or use reflection to generate the classes. Basically load a new reflector for each class, and use its methods to generate the code. For example, to generate the methods:
foreach ($reflector->getMethods() as $method) {
$signature = $method->getDocComment() . "\n";
$modifiers = $method->getModifiers();
if ($modifiers & ReflectionMethod::IS_PUBLIC) {
$signature .= 'public ';
} elseif ($modifiers & ReflectionMethod::IS_PROTECTED) {
$signature .= 'protected ';
} elseif ($modifiers & ReflectionMethod::IS_PRIVATE) {
$signature .= 'private ';
}
if ($modifiers & ReflectionMethod::IS_STATIC) {
$signature .= 'static ';
}
if ($modifiers & ReflectionMethod::IS_ABSTRACT) {
$signature .= 'abstract ';
}
if ($modifiers & ReflectionMethod::IS_FINAL) {
$signature .= 'final ';
}
$signature .= 'function ' . $method->name.'(';
$params = array();
foreach ($method->getParameters() as $param) {
$paramSignature = $param->name;
if ($param->isDefaultAvailable()) {
$paramSignature .= ' = ' . var_export($param->getDefaultValue(), true);
}
$params[] = $paramSignature;
}
$signature .= implode(', ', $params) . ') {}';
}
There's some more to be done (like type hinting, etc), but that should get you started...
just been having a look at this and thought I would post a couple of findings.
Firstly, ircmaxell's solution is definitely the best way I can see, using the Reflection classes, not something I have come across until now, they look like a good way of achieving my goal.
Secondly, it doesn't appear that anybody has really done this before, clearly we are not looking at a common use-case here.
I am not sure if I am going to go ahead yet, but if I do, then I thought I would share a simple methodology that I would use.
Each of the Reflection classes provides a __toString() method, when called a representation of the PHP object in question (class, method or whatever) is returned, but it isn't PHP syntax. So to be in keeping with this, I think that the best way to actually use the reflectior in this case is to subclass it:
class ReflectionClassRebuilder extends ReflectionClass{
/**
* Comments (Including an auto generated comment
*
* Constants
*
* Static Properties
*
* Static Methods
*
* Properties
*
* Methods
*
*/
public function __toString() {
foreach($this->getMethods() as $method){
//Build each of the methods, with comments as valid PHP syntax
}
}
}
$reflection = new ReflectionClassRebuilder("Example_Class");
//Write $reflection
(string) $reflection; //Returns the class for writing to a file.
Just in case that helps anybody.
Thanks.
精彩评论