PHPの各種のソースコード計測ツールを試す php-metrics-tools

PHPカンファレンス北海道2024をオンラインで観ていたところ、各種計測ツールを試すためのDockerイメージが紹介されていました。

READMEを参考に試してみました。

準備

リポジトリをローカルの適当なディレクトリにクローンします。Makefileを使ってDockerイメージの作成や各種計測ツールを実行できるようになっているようでした。

$ git clone https://github.com/blue-goheimochi/php-metrics-tools.git
$ cd php-metrics-tools/
$ make build

PHPStan

正常にビルドができたようですので、試しにPHPStanを実行してみました。

$ TARGET_DIR=$(realpath ../php-class-diagram/src) make phpstan
サンプルの設定ファイルの levelがmaxでしたので、たくさんエラーが表示されましたが、これはPHPStanのチェック結果なので、正常に実行できました。

Lizard

元々試してみたかったLizardを実行してみます。自作のクラス図作成ツール(php-class-diagram)のsrcを対象に分析してもらいます。

$ TARGET_DIR=$(realpath ../php-class-diagram/src) make lizard
TARGET_DIR: /home/smeghead/work/php/php-class-diagram/src
================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
      10      2     63      1      10 __construct@20-29@./Php/Doc/PhpDocComment.php
       3      1     17      1       3 use@58-60@./Php/Doc/PhpDocComment.php
       3      1     14      1       3 array_map@93-95@./Php/Doc/PhpDocComment.php
       4      1     12      1       4 __construct@17-20@./Php/Finders/FindUsePhpTypes.php
       3      1     17      2       3 find@28-30@./Php/Finders/FindUsePhpTypes.php
       3      1     11      1       3 (anonymous)@34-36@./Php/Finders/FindUsePhpTypes.php
       3      1     24      1       5 foreach@33-37@./Php/Finders/FindUsePhpTypes.php
       3      2     21      1       3 (anonymous)@24-26@./Php/Finders/FindConstructerProperties.php
      18      5    115      1      21 __construct@20-40@./Php/Finders/FindConstructerProperties.php
       5      1     26      1       5 __construct@17-21@./Php/PhpEnumCase.php
       3      1     12      1       3 (anonymous)@52-54@./Php/PhpTypeExpression.php
       6      1     27      2       6 parseType@60-65@./Php/PhpTypeExpression.php
       3      2     34      1       3 foreach@212-214@./Php/PhpTypeExpression.php
       5      1     21      2       5 __construct@12-16@./Php/PhpMethodParameter.php
       3      1      4      0       3 __construct@19-21@./Php/PhpProperty.php
      12      2     62      3      12 __construct@33-44@./Php/PhpClass.php
       5      2     24      1       5 foreach@51-55@./Php/PhpClass.php
       6      2     27      1       6 foreach@83-88@./Php/PhpClass.php
       3      1     20      1       3 foreach@99-101@./Php/PhpClass.php
       3      1     19      2       3 PhpType@139-141@./Php/PhpClass.php
       5      2     30      1       5 foreach@158-162@./Php/PhpClass.php
       5      2     29      1       5 foreach@211-215@./Php/PhpClass.php
       4      1     12      1       4 __construct@21-24@./Php/PhpReader.php
      11      4     70      1      11 foreach@77-87@./Php/PhpReader.php
       3      1     25      0       3 getUses@25-27@./Php/PhpMethod.php
      16      3    126      1      16 __construct@24-39@./Php/PhpAccessModifier.php
       8      2     60      5       8 __construct@20-27@./Php/PhpType.php
       4      1     12      1       4 __construct@24-27@./Config/Options.php
       2      1     10      2       2 register@18-19@./DiagramElement/ExternalPackage/PackageHierarchy.php
       4      1     12      1       4 __construct@38-41@./DiagramElement/ExternalPackage/PackageHierarchy.php
       6      2     28      1       6 foreach@60-65@./DiagramElement/ExternalPackage/PackageHierarchy.php
       3      1     22      1       3 foreach@78-80@./DiagramElement/ExternalPackage/PackageHierarchy.php
       6      1     30      3       6 __construct@22-27@./DiagramElement/Entry.php
       3      1     15      1       3 array_map@70-72@./DiagramElement/Entry.php
       5      1     21      2       5 __construct@18-22@./DiagramElement/Arrow.php
       5      1     21      2       5 __construct@13-17@./DiagramElement/PackageArrow.php
       5      1     21      2       5 __construct@20-24@./DiagramElement/PackageRelations.php
       3      1     18      1       3 (anonymous)@46-48@./DiagramElement/PackageRelations.php
       3      1     14      1       3 (anonymous)@51-53@./DiagramElement/PackageRelations.php
       5      1     49      1      10 foreach@45-54@./DiagramElement/PackageRelations.php
       1      1      2      0       1 (anonymous)@61-61@./DiagramElement/PackageRelations.php
       6      2     30      1       6 foreach@66-71@./DiagramElement/PackageRelations.php
       7      2     35      1       7 foreach@88-94@./DiagramElement/PackageRelations.php
       5      1     12      0       5 bothSideArrow@100-104@./DiagramElement/PackageRelations.php
       5      2     21      1       5 foreach@113-117@./DiagramElement/PackageRelations.php
       6      1     30      3       6 __construct@26-31@./DiagramElement/Package.php
       5      2     22      1       5 foreach@67-71@./DiagramElement/Package.php
       3      1     25      1       3 foreach@92-94@./DiagramElement/Package.php
       3      1     25      1       3 foreach@95-97@./DiagramElement/Package.php
       3      1     25      1       3 foreach@127-129@./DiagramElement/Package.php
       3      1     22      1       3 foreach@140-142@./DiagramElement/Package.php
       3      1     22      1       3 foreach@143-145@./DiagramElement/Package.php
       3      1     22      1       3 foreach@146-148@./DiagramElement/Package.php
       3      1     22      1       3 foreach@159-161@./DiagramElement/Package.php
       3      1     26      1       3 foreach@172-174@./DiagramElement/Package.php
       3      1     18      1       3 foreach@191-193@./DiagramElement/Package.php
       6      2     31      1       6 foreach@218-223@./DiagramElement/Package.php
       3      1     25      1       3 foreach@242-244@./DiagramElement/Package.php
       3      1     25      1       3 foreach@245-247@./DiagramElement/Package.php
       1      1     14      2       1 addEntry@22-22@./DiagramElement/Relation.php
       3      1     16      2       3 array_merge@37-39@./DiagramElement/Relation.php
       8      2     51      1       8 use@53-60@./DiagramElement/Relation.php
       3      1     16      2       3 array_merge@97-99@./DiagramElement/Relation.php
       6      2     30      0       6 getInfo@33-38@./Main.php
24 file analyzed.
==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
     85       5.3     1.3       31.3         3     ./Php/Doc/PhpDocComment.php
     29       3.2     1.0       16.0         4     ./Php/Finders/FindUsePhpTypes.php
     41      10.5     3.5       68.0         2     ./Php/Finders/FindConstructerProperties.php
     24       5.0     1.0       26.0         1     ./Php/PhpEnumCase.php
    159       4.0     1.3       24.3         3     ./Php/PhpTypeExpression.php
     20       5.0     1.0       21.0         1     ./Php/PhpMethodParameter.php
     44       3.0     1.0        4.0         1     ./Php/PhpProperty.php
    171       5.6     1.7       30.1         7     ./Php/PhpClass.php
     76       7.5     2.5       41.0         2     ./Php/PhpReader.php
     46       3.0     1.0       25.0         1     ./Php/PhpMethod.php
     58      16.0     3.0      126.0         1     ./Php/PhpAccessModifier.php
     71       8.0     2.0       60.0         1     ./Php/PhpType.php
    121       4.0     1.0       12.0         1     ./Config/Options.php
     30       0.0     0.0        0.0         0     ./DiagramElement/Division/DivisionColor.php
     63       3.8     1.2       18.0         4     ./DiagramElement/ExternalPackage/PackageHierarchy.php
     11       0.0     0.0        0.0         0     ./DiagramElement/ArrowInheritance.php
    151       4.5     1.0       22.5         2     ./DiagramElement/Entry.php
     26       5.0     1.0       21.0         1     ./DiagramElement/Arrow.php
     36       5.0     1.0       21.0         1     ./DiagramElement/PackageArrow.php
     94       4.4     1.3       22.4         9     ./DiagramElement/PackageRelations.php
    192       3.6     1.1       24.3        14     ./DiagramElement/Package.php
     71       3.8     1.2       24.2         4     ./DiagramElement/Relation.php
     30       0.0     0.0        0.0         0     ./DiagramElement/ArrowDependency.php
     53       6.0     2.0       30.0         1     ./Main.php

===============================================================================================================
No thresholds exceeded (cyclomatic_complexity > 15 or length > 1000 or nloc > 1000000 or parameter_count > 100)
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
      1702       4.8     1.4       27.5       64            0      0.00    0.00
 

Lizard の実行が速くて驚きました。php-class-diagram の ソースコードの循環的複雑度(CCN)は高くないようでした。

 

まとめ

PHPのソースコードに対しての解析を行なうことができる各種ツールを実行できる blue-goheimochi/php-metrics-tools を試してみました。導入するツールを比較するためにも簡単に利用できるのでお勧めです。Lizard については実行も速かったので、このまま常用してもいいかもしれないと思いました。

Issue報告

今回Ubuntu環境で試していた際にMakefileの中でエラーが表示されていたので、Issueを立てさせてもらいました。

https://github.com/blue-goheimochi/php-metrics-tools/issues/1

 

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です


The reCAPTCHA verification period has expired. Please reload the page.

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください