Thursday, April 12, 2012

count lines in a PHP project

I'm just curious. Do you know any tool which can count all the code lines from a PHP project?

Many thx!

Source: Tips4all


  1. On a POSIX operating system (e.g. Linux or OS X) you can write the following into your Bash shell:

    wc -l `find . -iname "*.php"`

    This will count the lines in all php-files in the current directory and also subdirectories. (Note that those single 'quotes' are backticks, not actual single quotes)

  2. SLOCCount is an awesome tool that produces a line-count report for a large number of languages. It also goes further by producing other, related statistics such as expected developer cost.

    Here's an example:

    $ sloccount .
    Creating filelist for experimental
    Creating filelist for prototype
    Categorizing files.
    Finding a working MD5 command....
    Found a working MD5 command.
    Computing results.

    SLOC Directory SLOC-by-Language (Sorted)
    10965 experimental cpp=5116,ansic=4976,python=873
    832 prototype cpp=518,tcl=314

    Totals grouped by language (dominant language first):
    cpp: 5634 (47.76%)
    ansic: 4976 (42.18%)
    python: 873 (7.40%)
    tcl: 314 (2.66%)

    Total Physical Source Lines of Code (SLOC) = 11,797
    Development Effort Estimate, Person-Years (Person-Months) = 2.67 (32.03)
    (Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
    Schedule Estimate, Years (Months) = 0.78 (9.33)
    (Basic COCOMO model, Months = 2.5 * (person-months**0.38))
    Estimated Average Number of Developers (Effort/Schedule) = 3.43
    Total Estimated Cost to Develop = $ 360,580
    (average salary = $56,286/year, overhead = 2.40).
    SLOCCount, Copyright (C) 2001-2004 David A. Wheeler
    SLOCCount is Open Source Software/Free Software, licensed under the GNU GPL.
    SLOCCount comes with ABSOLUTELY NO WARRANTY, and you are welcome to
    redistribute it under certain conditions as specified by the GNU GPL license;
    see the documentation for details.
    Please credit this data as "generated using David A. Wheeler's 'SLOCCount'."

  3. Unfortunately, SLOCCount is a bit long in the tooth and a pain in the neck for PHP projects, particularly ones that have a nested vendor directory you don't want counted. Also, it emits a warning for every PHP file that doesn't have a closing tag (which should be all of them if you aren't mixing HTML and PHP).

    CLOC is a more modern alternative that does everything SLOCCount does, but also supports an --exclude-dir option and it doesn't suffer from the aforementioned close tag problem. It also emits a SQLite database that you can extract some pretty advanced metrics from.