LinuxCommandLibrary

phploc

Analyze PHP code size and structure

TLDR

Analyze a directory and print the result

$ phploc [path/to/directory]
copy

Include only specific files from a comma-separated list (globs are allowed)
$ phploc [path/to/directory] --names '[path/to/file1,path/to/file2,...]'
copy

Exclude specific files from a comma-separated list (globs are allowed)
$ phploc [path/to/directory] --names-exclude '[path/to/file1,path/to/file2,...]'
copy

Exclude a specific directory from analysis
$ phploc [path/to/directory] --exclude [path/to/exclude_directory]
copy

Log the results to a specific CSV file
$ phploc [path/to/directory] --log-csv [path/to/file]
copy

Log the results to a specific XML file
$ phploc [path/to/directory] --log-xml [path/to/file]
copy

Count PHPUnit test case classes and test methods
$ phploc [path/to/directory] --count-tests
copy

SYNOPSIS

phploc [options] <path> [<path> ...]

PARAMETERS

--help -h
    Display help message.

--quiet -q
    Do not output any message.

--version -V
    Display application version.

--ansi
    Force ANSI output.

--no-ansi
    Disable ANSI output.

--no-interaction -n
    Do not ask interactive questions.

--profile
    Display timing and memory usage.

--verbose -v|-vv|-vvv
    Increase verbosity level.

--configuration= -c
    Read configuration from file.

--count-tests
    Include test code in analysis.

--names=
    Use custom names from file.

--no-configuration
    Ignore default config file.

--progress[=<type>]
    Show progress bar.

--no-progress
    Disable progress output.

--log-json=<file>
    Log JSON report to file.

--log-xml=<file>
    Log XML report to file.

--log-csv=<file>
    Log CSV report to file.

--log-html=<file>
    Log HTML report to file.

DESCRIPTION

phploc is a command-line tool designed to analyze PHP codebases, providing metrics on project size, structure, and complexity. It recursively scans specified directories or files, counting lines of code (physical, logical, executable), comments, blanks, namespaces, interfaces, traits, classes, abstract classes, methods, functions, and more.

It calculates key metrics like average lines per method, cyclomatic complexity (minimum, average, maximum), and lack of cohesion in methods (LCOM). Output is a formatted console report, with options for JSON, XML, CSV, or HTML logs. Ideal for code audits, CI/CD pipelines, and maintaining code quality in PHP projects. Supports modern PHP features and can optionally include test code in analysis.

CAVEATS

Analyzes only PHP files (.php, .php5, .phtml, .inc); requires PHP ≥7.3. Ignores non-PHP files and vendor dirs by default in configs. Not suitable for runtime analysis.

INSTALLATION

Install via Composer: composer global require phploc/phploc. Add ~/.composer/vendor/bin to PATH.
Usage: phploc src tests.

OUTPUT EXAMPLE

Console shows tables with directories, volumes, complexity, hierarchies, etc. Logs enable parsing in tools like Jenkins.

HISTORY

Developed by Sebastian Bergmann, first released in 2011 as part of PHP QA tools. Evolved to support PHP 8+, integrated into CI workflows. Maintained on GitHub; version 10+ focuses on modern metrics.

SEE ALSO

php(1), composer(1), phpunit(1)

Copied to clipboard