Upgrade to Pro — share decks privately, control downloads, hide ads and more …

計測できるレガシーさを捉え、コード改善に対処する / phperkaigi2023

計測できるレガシーさを捉え、コード改善に対処する / phperkaigi2023

PHPerKaigi 2023の登壇資料です。
https://phperkaigi.jp/2023/

blue_goheimochi

March 25, 2023
Tweet

More Decks by blue_goheimochi

Other Decks in Technology

Transcript

  1. PHPerKaigi 2023
    2023.03.25
    Yuta Ohashi

    View full-size slide

  2. 2






    View full-size slide

  3. 3










    View full-size slide

  4. / @blue_goheimochi
    PHP(Laravel), JavaScript(Vue.js/Nuxt.js),
    , , ,
    , , PUBG Mobile,

    View full-size slide

  5. 5
    • Web




    View full-size slide

  6. 7
    Web





    • etc...etc...etc...

    View full-size slide

  7. 9
    Web
    (legacy)

    View full-size slide

  8. 11
    Web
    3



    View full-size slide

  9. 12
    Web
    3




    View full-size slide

  10. 13
    Web
    3





    View full-size slide

  11. 14
    Web
    3








    View full-size slide

  12. 15
    Web
    3




    View full-size slide

  13. 17
    3



    View full-size slide

  14. 18
    3




    View full-size slide

  15. 19
    3






    View full-size slide

  16. 20
    3







    View full-size slide

  17. 21
    3





    View full-size slide

  18. 23





    View full-size slide

  19. 24

    ...
    • 24

    • 1

    View full-size slide

  20. 25






    View full-size slide

  21. 27





    View full-size slide

  22. 28





    • etc...

    View full-size slide

  23. 29
    • : phpcpd
    • Copy/Paste Detector (CPD) for PHP code.
    • 2023 1

    • Don t Repeat Yourself

    View full-size slide

  24. 30
    phpcpd 4.1.0 by Sebastian Bergmann.
    Found 184 clones with 9893 duplicated lines in 142 files:
    - /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:166-524 (358 lines)
    /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:170-528
    ~~~~~~~~~~
    - /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:321-561 (240 lines)
    /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:334-574
    ~~~~~~~~~~~
    - /app/xxxxxx/controllers/xxxxxx.php:909-1084 (175 lines)
    /app/xxxxxx/controllers/xxxxxx.php:1247-1422
    ~~~~~~~~~~
    8.41% duplicated lines out of 112932 total lines of code.
    Average size of duplication is 54 lines, largest clone has 358 of lines
    184 142 9,893

    View full-size slide

  25. 31
    phpcpd 4.1.0 by Sebastian Bergmann.
    Found 184 clones with 9893 duplicated lines in 142 files:
    - /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:166-524 (358 lines)
    /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:170-528
    ~~~~~~~~~~
    - /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:321-561 (240 lines)
    /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:334-574
    ~~~~~~~~~~~
    - /app/xxxxxx/controllers/xxxxxx.php:909-1084 (175 lines)
    /app/xxxxxx/controllers/xxxxxx.php:1247-1422
    ~~~~~~~~~~
    8.41% duplicated lines out of 112932 total lines of code.
    Average size of duplication is 54 lines, largest clone has 358 of lines
    View

    View full-size slide

  26. 32
    phpcpd 4.1.0 by Sebastian Bergmann.
    Found 184 clones with 9893 duplicated lines in 142 files:
    - /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:166-524 (358 lines)
    /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:170-528
    ~~~~~~~~~~
    - /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:321-561 (240 lines)
    /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:334-574
    ~~~~~~~~~~~
    - /app/xxxxxx/controllers/xxxxxx.php:909-1084 (175 lines)
    /app/xxxxxx/controllers/xxxxxx.php:1247-1422
    ~~~~~~~~~~
    8.41% duplicated lines out of 112932 total lines of code.
    Average size of duplication is 54 lines, largest clone has 358 of lines
    Fat

    View full-size slide

  27. 33
    phpcpd 4.1.0 by Sebastian Bergmann.
    Found 184 clones with 9893 duplicated lines in 142 files:
    - /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:166-524 (358 lines)
    /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:170-528
    ~~~~~~~~~~
    - /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:321-561 (240 lines)
    /app/xxxxxx/xxxxxx/xxxxxx/xxxxxx.php:334-574
    ~~~~~~~~~~~
    - /app/xxxxxx/controllers/xxxxxx.php:909-1084 (175 lines)
    /app/xxxxxx/controllers/xxxxxx.php:1247-1422
    ~~~~~~~~~~
    8.41% duplicated lines out of 112932 total lines of code.
    Average size of duplication is 54 lines, largest clone has 358 of lines
    112,932 8.41%
    54 358

    View full-size slide

  28. 34
    • : 112,932
    • : 8.41%
    • : 9,893
    • : 184
    • 規 : 142

    View full-size slide

  29. 36
    • : terryyin/lizard
    • Lizard is an extensible Cyclomatic Complexity Analyzer for many
    programming languages(PHP Python )


    • if/else, for, switch

    View full-size slide

  30. 37
    10 25%
    30 40%
    50 70%
    75 98%

    View full-size slide

  31. 38
    • : terryyin/lizard
    • Lizard is an extensible Cyclomatic Complexity Analyzer for many
    programming languages(PHP Python )


    • if/else, for, switch

    =========================================================================================
    !!!! Warnings (cyclomatic_complexity > 15 or length > 1000 or parameter_count > 100) !!!!
    ================================================
    NLOC CCN token PARAM length location
    ------------------------------------------------
    247 63 2436 0 353 aaaaAction@41-393@./xxxxx/controllers/zzzzController.php
    139 41 1272 0 202 bbbbAction@625-826@./xxxxx/controllers/zzzzController.php
    124 39 1018 0 161 ccccAction@77-237@./xxxxx/controllers/zzzzController.php
    81 37 351 1 84 xxxx@840-923@./xxxx/xxxx/Controller/xxxx/Abstract.php
    152 37 1086 0 197 xxxxAction@21-217@./xxxx/xxxx/xxxx/xxxxController.php
    143 36 1110 0 197 xxxxAction@740-936@./xxxx/xxxx/xxxxController.php
    150 35 1516 0 223 xxxxAction@33-255@./xxxx/controllers/xxxxController.php
    63 34 741 1 72 xxxx@16-87@./xxxx/xxxx/xxxx/xxxx/xxxx/xxxx.php
    81 34 917 0 106 aaaaAction@229-334@./yyyy/controllers/yyyyController.php
    110 31 1071 0 143 xxxxAction@20-162@./xxxx/controllers/xxxxController.php
    75 30 753 0 108 bbbb@79-186@./xxxx/controllers/yyyyController.php
    97 29 926 0 127 ccccAction@609-735@./xxxx/controllers/yyyyController.php
    ==========================================================================================
    Total nloc Avg.NLOC AvgCCN Avg.token Fun Cnt Warning cnt Fun Rt nloc Rt
    ------------------------------------------------------------------------------------------
    33248 16.3 2.3 104.2 1743 11 0.01 0.05
    63
    70%

    View full-size slide

  32. 39
    • : terryyin/lizard
    • Lizard is an extensible Cyclomatic Complexity Analyzer for many
    programming languages(PHP Python )


    • if/else, for, switch

    =========================================================================================
    !!!! Warnings (cyclomatic_complexity > 15 or length > 1000 or parameter_count > 100) !!!!
    ================================================
    NLOC CCN token PARAM length location
    ------------------------------------------------
    247 63 2436 0 353 aaaaAction@41-393@./xxxxx/controllers/zzzzController.php
    139 41 1272 0 202 bbbbAction@625-826@./xxxxx/controllers/zzzzController.php
    124 39 1018 0 161 ccccAction@77-237@./xxxxx/controllers/zzzzController.php
    81 37 351 1 84 xxxx@840-923@./xxxx/xxxx/Controller/xxxx/Abstract.php
    152 37 1086 0 197 xxxxAction@21-217@./xxxx/xxxx/xxxx/xxxxController.php
    143 36 1110 0 197 xxxxAction@740-936@./xxxx/xxxx/xxxxController.php
    150 35 1516 0 223 xxxxAction@33-255@./xxxx/controllers/xxxxController.php
    63 34 741 1 72 xxxx@16-87@./xxxx/xxxx/xxxx/xxxx/xxxx/xxxx.php
    81 34 917 0 106 aaaaAction@229-334@./yyyy/controllers/yyyyController.php
    110 31 1071 0 143 xxxxAction@20-162@./xxxx/controllers/xxxxController.php
    75 30 753 0 108 bbbb@79-186@./xxxx/controllers/yyyyController.php
    97 29 926 0 127 ccccAction@609-735@./xxxx/controllers/yyyyController.php
    ==========================================================================================
    Total nloc Avg.NLOC AvgCCN Avg.token Fun Cnt Warning cnt Fun Rt nloc Rt
    ------------------------------------------------------------------------------------------
    33248 16.3 2.3 104.2 1743 11 0.01 0.05
    3

    View full-size slide

  33. 40
    • ( ): 69,946
    • CCN: 2.8
    • CCN
    • :4,518

    View full-size slide

  34. 41
    • : phpmd unusedcode





    View full-size slide

  35. 42
    • : phpmd unusedcode





    class Sample
    {
    private static $start = 1;
    private $end = 2;
    private $piyo = 3;
    public function addOne()
    {
    $item = 1;
    return $this->piyo++;
    }
    private function execute($text)
    {
    }
    }

    View full-size slide

  36. 43
    • : phpmd unusedcode





    class Sample
    {
    private static $start = 1;
    private $end = 2;
    private $piyo = 3;
    public function addOne()
    {
    $item = 1;
    return $this->piyo++;
    }
    private function execute($text)
    {
    }
    }

    View full-size slide

  37. 44
    • : phpmd unusedcode





    FILE: /app/sample.php
    ----------------------
    5 | VIOLATION | Avoid unused private fields such as '$start'.
    6 | VIOLATION | Avoid unused private fields such as '$end'.
    11 | VIOLATION | Avoid unused local variables such as '$item'.
    15 | VIOLATION | Avoid unused private methods such as 'execute'.
    15 | VIOLATION | Avoid unused parameters such as '$text'.

    View full-size slide

  38. 45


    • Rector, PHPStan

    View full-size slide

  39. 46
    1) sample.php:1
    ---------- begin diff ----------
    @@ @@
    class Sample
    {
    - private static int $start = 1;
    - private $end = 2;
    private $piyo = 3;
    public function addOne()
    {
    - $item = 1;
    return $this->piyo++;
    - }
    -
    - private function execute($text)
    - {
    }
    }
    ----------- end diff -----------
    Rectorの場合

    View full-size slide

  40. 47
    class Sample
    {
    private $piyo = 3;
    public function addOne()
    {
    return $this->piyo++;
    }
    }
    class Sample
    {
    private static $start = 1;
    private $end = 2;
    private $piyo = 3;
    public function addOne()
    {
    $item = 1;
    return $this->piyo++;
    }
    private function execute($text)
    {
    }
    }
    Rectorの場合

    View full-size slide

  41. 48
    • : PHP CSFixer, PHP CodeSniffer
    • PSR-12 PSR-2

    View full-size slide

  42. 49
    • : PHPStan, Psalm
    • (= )

    View full-size slide

  43. 50





    • etc...

    View full-size slide

  44. 51



    • Four Keys

    View full-size slide

  45. 52
    • : Git
    • Git

    View full-size slide

  46. 53
    • : Google Analytics



    View full-size slide

  47. 54
    • : new relic, Datadog, Mackerel, Sentry
    • APM



    View full-size slide

  48. 55
    Four Keys
    • For keys



    View full-size slide

  49. 56





    View full-size slide

  50. 57
    How


    • ex)

    View full-size slide

  51. 59
    • Web







    View full-size slide