0

Are there any scripts that can go through a collection of PHP files and detect multiple functions with the same name?

5
Contributors
4
Replies
20
Views
2 Months
Discussion Span
Last Post by pty
0

If you post a couple of files I'll see if I can write a script. I don't know php syntax but I might be able to toss something together with a few examples.

0

I don't know much PHP, but if you simply want a list of function names with a count of how frequently they occur, you can do the heavy lifting in grep. Note, I used GNU Grep which I had to install on my mac with brew install grep. The one that comes bundled on the mac is BSD Grep which has fewer options and doesn't support Perl-style regular expressions.

Here it is displaying the top twenty function names in Laravel.

grep -hroP '(?<=function )(\w+)' | sort | uniq -c | sort -r | head -n20
 311  __construct
  93  handle
  49  get
  40  register
  39  __call
  35  getFacadeAccessor
  27  delete
  25  send
  25  getOptions
  25  flush
  24  make
  24  forget
  22  put
  22  getStub
  21  resolve
  20  create
  19  getDefaultNamespace
  18  increment
  18  has
  18  exists

Alternatively, using The Silver Searcher (aka ag) instead of grep.

ag --only-matching --no-heading --no-filename '(?<=function )(\w+)'| sed '/^$/d' | sort | uniq -c | sort --reverse | head -n20

Obviously, run this from the root of your project. Or, after the regex supply a list of filenames to whichever ag or grep and it'll just search them.

Edited by pty: Add ag

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.