Giter VIP home page Giter VIP logo

php-doc-check's Introduction

php-doc-check

Latest Stable Version Build Status License

PHP Doc Check is an automated command line tool to determine which functions and methods could use some more documentation.

By default this script:

  • Emits a warning if there is no docblock for non trivial functions (score > 4)
  • Emits an error if there is no docblock for complex functions (score > 6)

You can gradually improve documenation on projects by starting with relatively high limits and slowly moving these limits down.

Metrics

Installation

Composer

For now you have to install the beta version.

composer require --dev niels-de-blaauw/php-doc-check:^0.2.0@dev

Phive

phive install php-doc-check

Usage

$vendor/bin/php-doc-check -?
Usage: vendor/bin/php-doc-check [options] <directory> [<directory>...]

Options:
  -x, --exclude <arg>                       Directories to exclude, without
                                            slash
  -f, --format <arg>                        Output format [text, json]
                                            [default: text]
  -o, --reportFile <arg>                    Send report output to a file
  -m, --metric <arg>                        Metric to use for determining
                                            complexity
                                            [metrics.complexity.cognitive,
                                            metrics.complexity.cyclomatic,
                                            metrics.deprecated.category,
                                            metrics.deprecated.subpackage,
                                            metrics.complexity.length]
                                            [default:
                                            metrics.complexity.cognitive]
  -w, --complexity-warning-threshold <arg>  Cyclomatic complexity score which
                                            is the lower bound for a warning
                                            [default: 4]
  -e, --complexity-error-threshold <arg>    Cyclomatic complexity score which
                                            is the lower bound for an error
                                            [default: 6]
  -$, --file-extension <arg>                Valid file extensions to scan
                                            [default: php]
  -g, --grouping-method <arg>               Allows different grouping of the
                                            results list [file, none, metric,
                                            severity, fileline] [default: file]
  -s, --sorting-method <arg>                Sorting for the results. Natural
                                            sorts by name for groups and line
                                            for findings. Value uses the
                                            cumulative group score, and finding
                                            score as sorting value. [natural,
                                            value] [default: natural]
  -i, --ignore-violations-on-exit           Will exit with a zero code, even if
                                            any violations are found
  -a, --ignore-anonymous-functions          Skip checks on anonymous functions
  -?, --help                                Show this help and quit
  -q, --quiet                               Don't show any output

Example first use: vendor/bin/php-doc-check --exclude vendor ./

Examples

This is fine without docblocks (trivial method)

public function get_title() : string{
    return strtoupper($this->title);
}

This could use some explanation

/**
 * Limits the length of the title to a normal sentence, because older titles
 * tend to be longer then we can currently show.
 */
public function get_title() : string{
    if(strlen($this->title) > 20 ){
        if(strpos($this->title,'.') !== false && strpos($this->title,'.') < 20){
            [$title] = explode('.', $this->title, 2);
        }else{
            $title = substr($this->title, 0, 17) . '...';
        }
    }else{
        $title = $this->title;
    }
    return strtoupper($title);
}

FAQ

Q: Why dont you want if there is no comment at all, regardless of complexity?

A: You can set this software to warn for all functions that are undocumented by setting --complexity-error-threshold 1. However, if you want to force documentation, you probably want to look into a tool like php CodeSniffer in combination with documentation standards.

Q: Why isn't there a warning/error about complex functions and refactoring, regardless if they have a DocBlock?

A: You should refactor very complex functions. However, adding DocBlocks for complex function is often easier and safer. This tool only checks the availability of this type of documentation. Other tools, like php Mess Detector, can help you limit complexity.

Issues

Issues are in the GitHub tracker: https://github.com/NielsdeBlaauw/php-doc-check/issues

php-doc-check's People

Contributors

nielsdeblaauw avatar nikserg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

php-doc-check's Issues

Custom sorting of groups

Requires implementation of #28 first. Groups can be sorted by either cumulative score of everything in the group or groupname (filename, error severity, etc.).

Code has changed (significantly), but the DocBlock has not

If we can make some kind of snapshot of the docblocks for a function/method at a certain time, we can see which documentation has not been updated when the function is changed.

This would include the following steps:

  1. Edit function with a docblock.
  2. Run php-doc-check
  3. If the functions meet a complexity metric: php-doc-check will compare the current docblock and function (length or complexity) to what is saved in the snapshot.
    3.a. If the DocBlock is the same as the one in the snapshot: error that the DocBlock has not been updated.
    3.b. If the DocBlock has changed, update the snapshot with the new docblock and filelength.
  4. Manually edit the DocBlocks that have errored where applicable
  5. Goto step 3 until all errors have been removed, or all the docblocks for all remaining errors are still descriptive of the function and origin of the code.
  6. If remaining errors don't require changes: manually regenerate snapshot.
  7. Commit changes and snapshot.

Allow for custom groupings

This allows users to group the findings to create their own overviews. Potential groups:

filename
severity
originating metric/rule
filename+starting line of method
none, everything is put into a single group

Function length metric

Add a metric for very long lineair functions and method that might not trigger complexity warnings.

Phar in releases

Hello,

I want to use this tool but I don't want to pollute my composer.json file. I've taken into habit to use the phar version of static analyzers (phan, phpstan, psalm, phpdoc…) so I'm sure I'll never run into dependencies clash.

Please consider releasing a (signed) phar archive :)

Cheers,
~Nico

Catch parsing errors for invalid files

PHP-Parser throws an error exception when a php file being parsed is not valid.

Example:

Fatal error: Uncaught PhpParser\Error: Namespace declaration statement has to be the very first statement in the script on line 8 in [...]

Multiple metrics at once

Allow for checking multiple metrics at once. This allows the user to choose their own set of checks to run.

Parser error

There is some failure half through analysis and it throws the following exception:

.PHP Fatal error: Uncaught PhpParser\Error: Unexpected null byte on line 1 in ...vendor/nikic/php-parser/lib/PhpParser/Lexer.php:100

Custom sorting inside groups

Allows the user to either set the score/weight of a finding or the line number as sort order. Defaults to line number for backward compatibility.

Add --ansi option to force table output when not running in a TTY

I looked through the code but wasn't able to figure out where the difference comes from, but when running this command from a git pre-commit hook, docker exec -it fails with the error the input device is not a TTY, and without the -it option, the output is not formatted into tables.

Tools like PHPStan and PHP-CS-fixer have a --ansi/--no-ansi option which would be helpful here.

I managed to work around this by running the command with < /dev/tty.

Specifically, the following:

make doc-check < /dev/tty
# where this make target runs bin/php-doc-check
# which runs:
docker exec -it container /path/vendor/bin/php-doc-check folders

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.