PHP Classes

How to Display PHP Code Statistics with Yii2 CodeStat: Analyse and display statistics about PHP code

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 28 All time: 11,160 This week: 560Up
Version License PHP version Categories
yii2-codestat 1.0.0MIT/X Consortium ...5Statistics, Tools
Description 

Author

This package can analyze and display statistics about PHP code.

It uses several packages that analyze the code files of a project and then displays a table in the terminal console with the statistics of those files.

Currently, it displays the statistics in several groups files with numbers for:

- Classes

- Methods

- Methods per class

- Lines

- Lines of code (LOC)

- Lines of code per method

- Complexity

- Complexity per class

Innovation Award
PHP Programming Innovation award nominee
September 2022
Number 9
You can measure the quality of a developer's code in several ways. For instance, complex code may be hard to maintain.

If you write complex code, it would be good if you could improve the way you write your code to make it less complicated.

Measuring the complexity and other aspects of the quality of your code can be helpful for you to have an idea of how good is the code you write and what parts you need to improve.

This package combines several other packages that measure different aspects of your code and generate a report that you can analyze and discover opportunities to make your code better.

Manuel Lemos
Picture of Insolita
  Performance   Level  
Name: Insolita <contact>
Classes: 25 packages by
Country: Russian Federation Russian Federation
Age: 38
All time rank: 330590 in Russian Federation Russian Federation
Week rank: 215 Up12 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 18x

Documentation

Yii2 Code Statistic

Build Status Scrutinizer Code Quality SensioLabsInsight

screenshot

Based on phploc/phploc

Installation

Either run

composer require --dev insolita/yii2-codestat:~2.0

or add

"insolita/yii2-codestat": "~2.0"

in require-dev section of your composer.json file.

Basic Usage

Add in console configuration file, in section modules

php
'modules'=>[
 ....
        'codestat'=>[
            'class'=>\insolita\codestat\CodeStatModule::class,
            'scanTargets' => ['@backend/','@common/','@frontend/','@console/'],
            'exceptTargets' => ['config','vendor','web/','runtime/','views/','*tests/'],
        ]
    ],

scanTargets - array of path, or path aliases that will be scanned recursively exceptTargets - array of path patterns for excluding

For checking whole list of files that will be processed, run

./yii codestat/default/list-files

For statistic summary output run

./yii codestat

For statistic summary output with show bad resolved files

./yii codestat 1

Show full phploc report per each defined group

./yii codestat/default/advanced 
./yii codestat/default/advanced WebControllers
./yii codestat/default/advanced WebControllers,RestControllers,ConsoleControllers

Show full phploc report for all matched files

./yii codestat/default/common

Show full phploc report for custom directory

./yii codestat/default/directory @common/models

Show full phploc report for custom file

./yii codestat/default/file @common/lib/MySuperClass.php

List available metrics with codes

./yii codestat/default/list-metrics

Advanced Usage

Custom Class Grouping Rules

You can extend or overwrite property 'groupRules', with supported formats

'Group Name' => 'BaseParentClass'

where 'BaseParentClass' should by verified with (\ReflectionClass)->isSubclassOf()

or

'Group Name' => function(\ReflectionClass $reflection){
       //Should return true if class valid for this group, otherwise false;
  }

Final example

php
'modules'=>[
 ....
        'codestat'=>[
            'class'=>\insolita\codestat\CodeStatModule::class,
             'groupRules' => [
                                'Jobs' => 'yii\queue\JobInterface',
                                'Handlers' => 'trntv\bus\interfaces\Handler::class',
                                'DTO' => function (\ReflectionClass $reflection) {
                                     return mb_strpos($reflection->getFileName(), 'Dto')!==false;
                                },
                                'All Tests' => function (\ReflectionClass $reflection) {
                                    return $reflection->isSubclassOf('\Codeception\Test\Unit')
                                        || StringHelper::endsWith($reflection->getName(), 'Cest');
                                },
                            ] + CodeStatModule::defaultRules(),
                    ],
        ]
    ],

Important! The order of the rules in the list matters, the base classes (\yii\base\Component and \yii\base\Object) should be at the end of the list!

Custom code metrics

Code metrics provided by https://github.com/sebastianbergmann/phploc, has lot of variants, you can define own combination

For actions advanced/common/directory/file you should set metrics property with array of necessary metric names

php
'modules'=>[
 ....
        'codestat'=>[
            'class'=>\insolita\codestat\CodeStatModule::class,
            'metrics'=>['loc','lloc','classCcnAvg', 'classLlocAvg', 'methodCcnAvg']
            ]
 ]

For summary action you should provide property 'analyseCallback' in module like as

    'analyseCallback = function($group){
               /@var insolita\codestat\lib\collection\Group $group/
               $metrics=$customAnalyzer->analyze($group->getFiles());
               return ['totalFiles'=>count($group->getFiles()),  'metric1'=>$metrics[some], ...etc];
     }

It should return associative array with 'metric name' => 'metric value' data and will replace internal https://github .com/Insolita/yii2-codestat/blob/7d0fc3351718b2052624ea091ff8f154fe471aeb/src/lib/CodestatService.php#L154

And also table summary convention - if metric name contains slash "/", for summary row will be counted average value, otherwise sum


  Files folder image Files (44)  
File Role Description
Files folder imagesrc (1 file, 3 directories)
Files folder imagetests (4 files, 2 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Image file codestat.png Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (44)  /  src  
File Role Description
Files folder imagecontrollers (1 file)
Files folder imagehelpers (1 file)
Files folder imagelib (1 file, 3 directories)
  Plain text file CodeStatModule.php Class Class source

  Files folder image Files (44)  /  src  /  controllers  
File Role Description
  Plain text file DefaultController.php Class Class source

  Files folder image Files (44)  /  src  /  helpers  
File Role Description
  Plain text file Output.php Class Class source

  Files folder image Files (44)  /  src  /  lib  
File Role Description
Files folder imageclassdetect (2 files)
Files folder imagecollection (3 files)
Files folder imagecontracts (2 files)
  Plain text file CodestatService.php Class Class source

  Files folder image Files (44)  /  src  /  lib  /  classdetect  
File Role Description
  Plain text file RegexpDetector.php Class Class source
  Plain text file TokenizerDetector.php Class Class source

  Files folder image Files (44)  /  src  /  lib  /  collection  
File Role Description
  Plain text file BaseCollection.php Class Class source
  Plain text file Group.php Class Class source
  Plain text file GroupCollection.php Class Class source

  Files folder image Files (44)  /  src  /  lib  /  contracts  
File Role Description
  Plain text file ClassDetectorInterface.php Class Class source
  Plain text file CodestatServiceInterface.php Class Class source

  Files folder image Files (44)  /  tests  
File Role Description
Files folder imageconfig (1 file)
Files folder imagestub (1 file, 3 directories)
  Plain text file ClassDetectorTest.php Class Class source
  Plain text file CodestatServiceTest.php Class Class source
  Plain text file GroupCollectionTest.php Class Class source
  Plain text file GroupTest.php Class Class source

  Files folder image Files (44)  /  tests  /  config  
File Role Description
  Accessible without login Plain text file base.php Aux. Auxiliary script

  Files folder image Files (44)  /  tests  /  stub  
File Role Description
Files folder imageone (9 files, 1 directory)
Files folder imagetwo (9 files)
Files folder imageviews (1 directory)
  Plain text file StubTrait.php Class Class source

  Files folder image Files (44)  /  tests  /  stub  /  one  
File Role Description
Files folder imageviews (1 directory)
  Plain text file AutoloadExample.php Class Class source
  Plain text file HelloController.php Class Class source
  Plain text file LoginForm.php Class Class source
  Plain text file StubAction.php Class Class source
  Plain text file StubBehavior.php Class Class source
  Plain text file StubComponent.php Class Class source
  Plain text file StubEvent.php Class Class source
  Plain text file StubModel.php Class Class source
  Plain text file StubModule.php Class Class source

  Files folder image Files (44)  /  tests  /  stub  /  one  /  views  
File Role Description
Files folder imagedefault (1 file)

  Files folder image Files (44)  /  tests  /  stub  /  one  /  views  /  default  
File Role Description
  Accessible without login Plain text file index.php Aux. Auxiliary script

  Files folder image Files (44)  /  tests  /  stub  /  two  
File Role Description
  Plain text file Bootstrapper.php Class Class source
  Plain text file m170412_230002_migration.php Class Class source
  Plain text file SectionItem.php Class Class source
  Plain text file SiteController.php Class Class source
  Plain text file StubAbstract.php Class Class source
  Plain text file StubConcrete.php Class Class source
  Plain text file StubFinal.php Class Class source
  Plain text file StubImpl.php Class Class source
  Plain text file StubInterface.php Class Class source

  Files folder image Files (44)  /  tests  /  stub  /  views  
File Role Description
Files folder imagedefault (2 files)

  Files folder image Files (44)  /  tests  /  stub  /  views  /  default  
File Role Description
  Accessible without login Plain text file index.php Aux. Auxiliary script
  Plain text file NonPsr.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:28
This week:0
All time:11,160
This week:560Up