Maybe there's a better tool to tell you what function is defined in what class in PHP, but I couldn't find one in the time it would take me to write it, so I wrote it. It's not even a screenful. Give it the class definitions, in class hierarchy order, on the command line. It will pull out the class name and function names, and tell you which function is actually being implemented by which class. It doesn't pay any attention to parent::self() calls, so you should watch out for them. -russ
#!/usr/bin/python
import sys, re
functions = {} classes = {} cl = None for fn in sys.argv[1:]: for line in open(fn): match = re.match(r'(abstract\s+)?class\s+(.*?)\s+(extends\s+(.*?)\s+)?{', line) if match: cl = match.group(2) supercl = match.group(4) classes[cl] = supercl if supercl: classes[supercl] # superclass should already be exist
match = re.match(r'\s*(public\s+|static\s+)?function\s+(.*?)(', line) if match: # we have a function definition. funct = match.group(2) functions[funct] = cl
keys = functions.keys() keys.sort() for key in keys: print key,functions[key]
On Mon, May 2, 2011 at 4:21 PM, Russell N. Nelson - rnnelson < rnnelson@clarkson.edu> wrote:
Maybe there's a better tool to tell you what function is defined in what class in PHP, but I couldn't find one in the time it would take me to write it, so I wrote it.
Depending on what you're trying to do, this may already be built into your editor or IDE. NetBeans for instance, with the standard PHP plugin installed is able to jump to class, function, or symbol definitions via a hotkey (or ctrl+click or right-click+menu on a mention in source).
-- brion
Am using vim, which knows about ctags. Exuberant ctags (which is the name of a piece of software) knows about languages other than C. It seems not to know about class structure, and which function is being overridden where. I want to be able to read through the code that my Swift interface will be executing to look for filesystem calls, and I want to be able to ignore code which won't be executed. grep hasn't been my best friend in this process, although we *are* drinking buddies. ________________________________________ From: wikitech-l-bounces@lists.wikimedia.org [wikitech-l-bounces@lists.wikimedia.org] on behalf of Brion Vibber [brion@pobox.com] Sent: Monday, May 02, 2011 7:24 PM To: Wikimedia developers Subject: Re: [Wikitech-l] auditcode.py - discern class structure
On Mon, May 2, 2011 at 4:21 PM, Russell N. Nelson - rnnelson < rnnelson@clarkson.edu> wrote:
Maybe there's a better tool to tell you what function is defined in what class in PHP, but I couldn't find one in the time it would take me to write it, so I wrote it.
Depending on what you're trying to do, this may already be built into your editor or IDE. NetBeans for instance, with the standard PHP plugin installed is able to jump to class, function, or symbol definitions via a hotkey (or ctrl+click or right-click+menu on a mention in source).
-- brion _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
I totally <3 that you wrote it in python.
On Mon, May 2, 2011 at 4:21 PM, Russell N. Nelson - rnnelson rnnelson@clarkson.edu wrote:
Maybe there's a better tool to tell you what function is defined in what class in PHP, but I couldn't find one in the time it would take me to write it, so I wrote it. It's not even a screenful. Give it the class definitions, in class hierarchy order, on the command line. It will pull out the class name and function names, and tell you which function is actually being implemented by which class. It doesn't pay any attention to parent::self() calls, so you should watch out for them. -russ
#!/usr/bin/python
import sys, re
functions = {} classes = {} cl = None for fn in sys.argv[1:]: for line in open(fn): match = re.match(r'(abstract\s+)?class\s+(.*?)\s+(extends\s+(.*?)\s+)?{', line) if match: cl = match.group(2) supercl = match.group(4) classes[cl] = supercl if supercl: classes[supercl] # superclass should already be exist
match = re.match(r'\s*(public\s+|static\s+)?function\s+(.*?)(', line) if match: # we have a function definition. funct = match.group(2) functions[funct] = cl
keys = functions.keys() keys.sort() for key in keys: print key,functions[key]
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
On 5/2/2011 7:35 PM, Ryan Lane wrote:
I totally<3 that you wrote it in python.
On Mon, May 2, 2011 at 4:21 PM, Russell N. Nelson - rnnelson rnnelson@clarkson.edu wrote:
Maybe there's a better tool to tell you what function is defined in what class in PHP, but I couldn't find one in the time it would take me to write it, so I wrote it. It's not even a screenful. Give it the class definitions, in class hierarchy order, on the command line. It will pull out the class name and function names, and tell you which function is actually being implemented by which class. It doesn't pay any attention to parent::self() calls, so you should watch out for them. -russ
Note that there is a PHP tokenizer built into PHP which makes it straightforward to develop tools like this in PHP:
http://php.net/manual/en/book.tokenizer.php
A practical example can be found here
http://gen5.info/q/2009/01/09/an-awesome-autoloader-for-php
Now, it would be nice to have a parser available that can see the tree structure.
On Tue, May 3, 2011 at 10:25 AM, Paul Houle paul@ontology2.com wrote:
Note that there is a PHP tokenizer built into PHP which makes it straightforward to develop tools like this in PHP:
http://php.net/manual/en/book.tokenizer.php
A practical example can be found here
We have a practical example too:
http://svn.wikimedia.org/viewvc/mediawiki/trunk/tools/code-utils/stylize.php...
Paul Houle wrote:
Note that there is a PHP tokenizer built into PHP which makes it
straightforward to develop tools like this in PHP:
http://php.net/manual/en/book.tokenizer.php
A practical example can be found here
http://gen5.info/q/2009/01/09/an-awesome-autoloader-for-php
Now, it would be nice to have a parser available that can see the
tree structure.
The tokenizer is slow. It's not worth taking it out for this.
Russell N. Nelson - rnnelson wrote:
Maybe there's a better tool to tell you what function is defined in what class in PHP, but I couldn't find one in the time it would take me to write it, so I wrote it. It's not even a screenful. Give it the class definitions, in class hierarchy order, on the command line.
That restriction isn't too friendly.
It will pull out the class name and function names, and tell you which function is actually being implemented by which class. It doesn't pay any attention to parent::self() calls, so you should watch out for them. -russ
#!/usr/bin/python
import sys, re
functions = {} classes = {} cl = None for fn in sys.argv[1:]: for line in open(fn): match = re.match(r'(abstract\s+)?class\s+(.*?)\s+(extends\s+(.*?)\s+)?{', line) if match: cl = match.group(2) supercl = match.group(4) classes[cl] = supercl if supercl: classes[supercl] # superclass should already be exist
Also note, some files include both the class and the superclass, so "give in class hierarchy order" may not be possible (I don't know if there's any file that includes the child before the parent, though).
match = re.match(r'\s*(public\s+|static\s+)?function\s+(.*?)\(', line)
I understand you may not want protected or private functions, but this regex will also miss the 536 functions that are public static, and the 7 that are static public.
if match: # we have a function definition. funct = match.group(2) functions[funct] = cl
keys = functions.keys() keys.sort() for key in keys: print key,functions[key]
If you are adventurous, you may want to add an option to create a file similar to parent.classes in check-vars.php Functions are already being tracked, so it shouldn't be too hard.
wikitech-l@lists.wikimedia.org