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] <directory|file> ...

PARAMETERS

--count-tests
    Counts methods named 'test*' or annotated with '@test' as test methods.

--exclude <dir>
    Excludes the specified directory from the analysis. Can be used multiple times.

--names <pattern>
    A comma-separated list of file names to include in the scan (e.g., '*.php').

--names-exclude <pattern>
    A comma-separated list of file names to exclude from the scan (e.g., '*-old.php').

--suffixes <suffix>
    A comma-separated list of file suffixes to include (e.g., 'php,inc').

--log-xml <file>
    Writes the analysis results in XML format to the specified file.

--log-csv <file>
    Writes the analysis results in CSV format to the specified file.

--progress
    Displays a progress bar during the analysis.

--fuzzy
    Enables fuzzy calculation of cyclomatic complexity (experimental feature).

--help
    Displays the help message and exits.

--version
    Prints the phploc version and exits.

DESCRIPTION

phploc is a command-line tool designed to quickly measure the size and analyze the structure of a PHP project. It counts various metrics such as physical lines of code (LOC), logical lines of code, comment lines, number of files, classes, interfaces, traits, methods, functions, and namespaces. Beyond basic counts, it provides insights into complexity by measuring things like cyclomatic complexity, number of dependencies, and usage of constants and anonymous functions. phploc is an invaluable tool for developers and teams seeking to understand the scale of their codebase, track progress, identify potential technical debt, and ensure maintainability. It supports analyzing entire directories or specific files and can output results in various formats, including plain text, XML, and CSV.

CAVEATS

phploc is primarily designed for PHP projects and may not yield meaningful results for other languages. While powerful for structural metrics, it does not perform deep semantic analysis like dedicated static analyzers such as PHPStan or Psalm. Performance can be a concern on very large codebases if not configured to exclude irrelevant directories like 'vendor/' or 'node_modules/'. It requires PHP to be installed and phploc to be available, typically via Composer.

TYPICAL USE CASES

phploc is frequently integrated into CI/CD pipelines to monitor code size and complexity trends over time. It's excellent for performing quick audits of new or legacy PHP projects to understand their scale and structure. The insights gained can guide refactoring efforts by highlighting large or overly complex components that might require attention for improved maintainability.

OUTPUT FORMATS

By default, phploc provides a human-readable text output to the console. For machine readability and seamless integration with other tools or dashboards, it supports writing results in XML via the --log-xml option and CSV via the --log-csv option. These structured formats allow for easy parsing, historical data tracking, and visual representation of code metrics.

HISTORY

phploc was created by Sebastian Bergmann, renowned for his work on PHPUnit, the de-facto standard unit testing framework for PHP. It emerged from the need for a simple, fast tool to get an overview of PHP project metrics, complementing more in-depth static analysis tools. Since its initial release, it has been regularly updated to support newer PHP versions and integrate into the broader PHP ecosystem, often used alongside other development tools like PHP_CodeCoverage and PHP_Depend. Its development reflects a focus on actionable metrics for code quality and maintainability.

SEE ALSO

cloc(1), loc(1), phpcs(1), phpstan(1), lizard(1)

Copied to clipboard