views:

70

answers:

3

Hello All,

I recently created a CMS for the client which is meant to work out of the box. But I made a mistake by not commenting my code at the time of writing. Just wanted to know if there is a tool that will go through the files of CMS and add comment blocks automatically.

Let's suppose I have this code:

class foo{
  function bar(array $array){
    print_r ($array);
  }
}

The tool should be able to comment it like (or somewhat similar):

class foo{

/**
 * bar
 *
 * @access  public
 * @param   array
 * @return  void
 */
 function bar(array $array){
  print_r ($array);
 }

}

Edit:

The tool should only add phpdoc stubs, i will write explanatory comments myself.

+5  A: 

While I am sure there is tools that can reflect on the signature (and to a lesser extent tokenize the method body), I am pretty sure there is no tool that would be able to insert the short or long description of the method. Think about it, that's where you - the developer - describes what the function is supposed to do.

So even if there is a tool that can insert the stubs, you will have to go through each comment anyway. And in this case, you can just as well use an IDE like Zend Studio, Eclipse with PDT or Netbeans to insert the comment blocks. They will insert the stubs when opening a new comment block automatically, so all you have to do is fill in the description.

Gordon
@Gordon: I am only looking for a tool that could add phpdoc stubs, i will add the actual explanatory comments myself.
Sarfraz
@Sarfraz I dont know any tool that can do it. Try Doxygen or see if your IDE can do it for you. If not, you can still use Reflection and/or the Tokenizer to write a little something.
Gordon
+3  A: 

Comments are only useful if they add something that's not in the code. Of course there are tools that could add phpdoc like your example, but without manual effort, they won't add any value at all. Writing good comments is almost as hard as writing good code, maybe even harder; in fact, good comments (along with concise naming and proper consistent indentation) often distinguish good maintainable code from useless garbage.

You just didn't do your homework when you should have, so now you have to do it all at once. This means you'll have to manually step through your code and add comments yourself; a good IDE will be a great help for this (e.g. Netbeans can automatically generate phpdoc stubs for you), but the actual comment is up to you.

tdammers
Now let's see how long it takes until someone comes up and says good [code should be self-documenting by the way it is written and thus does not need additional comments](http://stackoverflow.com/questions/209015/self-documenting-code).
Gordon
@tdammers: Thanks for your answer and i totally agree but now that i have done it all without comments, i can't use netbeans, looking for a tool that will add phpdoc stubs at least, i will write explanatory comments myself.
Sarfraz
+1  A: 

I had a little tool which transformed my ugly comment style into phpDoc blocks. In retrospect it's coded ugly, would run it carefully on other code (depends on C indendation style), and I don't even remember why it's called crw anyway. However it adds some @param boilerplate, and guesses parameter types from a few common variable names. So no substitute for manual post-editing.

Btw, I've always wondered why phpDoc doesn't have something like @output. The above example function is a void, so wouldn't it make sense to document that it prints something out?

mario