0

I'd like to count the the lines of code of a PHP-project in a given git repository. Currently I'm using:

git ls-files -- *.php* | xargs wc -l

to count the lines of code in all php files.

It would be great to have a step before counting all lines, that skips all empty lines and maybe also lines containing comments (in PHP this in # and // for single line comments and /* */ for multiline comments). I guess sed might help, but how to handle multiline comments?

Edward
  • 4,453
  • 8
  • 44
  • 82
  • 3
    If counting the lines of code is important, use [phploc](https://github.com/sebastianbergmann/phploc) – Mark Baker Nov 04 '15 at 14:22
  • 1
    Just out of curiosity what are you using the line count for? – cmorrissey Nov 04 '15 at 14:28
  • 2
    @cmorrissey - let's hope it isn't for measuring productivity – Mark Baker Nov 04 '15 at 14:28
  • @MarkBaker my thoughts exactly – cmorrissey Nov 04 '15 at 14:30
  • @cmorrissey: I'm refactoring an old project. I try to remove unnecessary stuff and increase documentation. Therefore I'd like to exclude empty lines and have a measurement with and without documentation. – Edward Nov 04 '15 at 14:38
  • You might want to consider a few other tools for measuring progress, rather than a simplistic line count - http://phpqatools.org/ - lists a range of useful tools when refactoring and documenting code – Mark Baker Nov 04 '15 at 14:46
  • @MarkBaker: Could you write an answer from you questions, so that I can accept I,... – Edward Nov 06 '15 at 09:25
  • Another one I've had to use which I thought was good -> http://cloc.sourceforge.net/ – McNab Dec 29 '15 at 17:34

1 Answers1

0

You may use powershell for this as pointed out here. Open Powershell and go to the directory with your PHP-files. Issue the following command:

(dir -include *.php -recurse | select-string .).Count

If you want files of other file types to be included, simply add them like

(dir -include *.php, *.js -recurse | select-string .).Count

If there aren't any subdirectories, the command would be

(dir *.php | select-string .).Count
Community
  • 1
  • 1
wbartussek
  • 1,850
  • 1
  • 10
  • 8
  • 2
    I'm not a Powershell user, so apologies if this is wrong, but doesn't the fact that the question references `wc` and `sed` imply that @Edward is not a Windows user? – DaveyDaveDave Dec 29 '15 at 17:02
  • Developing on Windows so I found it useful. Thanks – BeNice Jan 05 '17 at 11:22