Slide 1

Slide 1 text

が ͳʹ͔ͷษڧձͱ͔͕͋Δ༁Ͱ͸ͳ͍ Hideki Kinjyo GitHub: o0h / Twitter: @o0h_

Slide 2

Slide 2 text

ࣗݾ঺հ / GitHub: @o0h / Twitter: @o0h_ FW CakePHP

Slide 3

Slide 3 text

Πϯτϩ 
 PHP PHP 


Slide 4

Slide 4 text

( ) https://fortee.jp/phpcon-2021/proposal/6cd22f6c-9caa-432f-930c-9e62f512d59d

Slide 5

Slide 5 text

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

https://www.php.net/manual/ja/language.types.intro.php

Slide 8

Slide 8 text

https://www.php.net/manual/ja/language.types.intro.php PHP ( ) PHP

Slide 9

Slide 9 text

https://www.php.net/manual/ja/language.types.intro.php 10000 3

Slide 10

Slide 10 text

https://www.php.net/manual/ja/language.types.intro.php =

Slide 11

Slide 11 text

https://www.php.net/manual/ja/language.types.intro.php ( ) Web

Slide 12

Slide 12 text

shin1x1͞Μͷൃදʮ੍໿ͷྗʯ https://speakerdeck.com/shin1x1/restricting-states

Slide 13

Slide 13 text

shin1x1͞Μͷൃදʮ੍໿ͷྗʯ https://speakerdeck.com/shin1x1/restricting-states

Slide 14

Slide 14 text

shin1x1͞Μͷൃදʮ੍໿ͷྗʯ https://speakerdeck.com/shin1x1/restricting-states

Slide 15

Slide 15 text

shin1x1͞Μͷൃදʮ੍໿ͷྗʯ https://speakerdeck.com/shin1x1/restricting-states

Slide 16

Slide 16 text

t-wada͞Μͷൃදʮ༧๷ʹউΔ๷ޚͳ͠ʯ https://speakerdeck.com/twada/growing-reliable-code-phperkaigi-2022

Slide 17

Slide 17 text

t-wada͞Μͷൃදʮ༧๷ʹউΔ๷ޚͳ͠ʯ https://speakerdeck.com/twada/growing-reliable-code-phperkaigi-2022

Slide 18

Slide 18 text

ܕΛ͚ͭΔͧʂʂͷϞνϕʔγϣϯ ( )

Slide 19

Slide 19 text

:

Slide 20

Slide 20 text

PHPͷܕએݴ PHP php.net https://www.php.net/manual/ja/language.types.declarations.php

Slide 21

Slide 21 text

( )

Slide 22

Slide 22 text

int $l, $r int int NULL DB (= )

Slide 23

Slide 23 text

int int int int ( )

Slide 24

Slide 24 text

PHP7 ( ) ࢀߟ 
 ݫີͳܕ෇͚ 
 IUUQTXXXQIQOFUNBOVBMKBMBOHVBHFUZQFTEFDMBSBUJPOTQIQMBOHVBHFUZQFTEFDMBSBUJPOTTUSJDU

Slide 25

Slide 25 text

خ͍͠Ͱ͔͢ʁ int t-wada

Slide 26

Slide 26 text

خ͍͠Ͱ͔͢ʁ = (DB HTTP ) etc ( ) = int =>

Slide 27

Slide 27 text

int / $response->getBody() $body- >eof() string, bool, Response, Stream ࢀߟ 
 IUUQTHJUIVCDPNTMJNQIQ4MJNCMPC4MJN 3FTQPOTF&NJUUFSQIQ

Slide 28

Slide 28 text

Interface ࢀߟ 
 IUUQTHJUIVCDPNTMJNQIQ4MJNCMPC4MJN 3FTQPOTF&NJUUFSQIQ StreamInterface bool int: 0 
 (Content-Length ) int int string bool

Slide 29

Slide 29 text

ܕͱͷ෇͖߹͍ํ ( ) Interface strict_types=1 private public

Slide 30

Slide 30 text

:

Slide 31

Slide 31 text

ΨϯΨϯܕએݴΛ͢Δ͜ͱͷऑ఺ͱ͔ PHPDoc PHP PHP

Slide 32

Slide 32 text

੩తղੳπʔϧΛ࢖͓͏ / PHPStan https://phpstan.org/ PHPStan scans your whole codebase and looks for both obvious & tricky bugs. Even in those rarely executed if statements that certainly aren't covered by tests. ( )

Slide 33

Slide 33 text

੩తղੳπʔϧΛ࢖͓͏ / Psalm https://psalm.dev/ Psalm is a static analysis tool that attempts to dig into your program and fi nd as many type-related bugs as possible. PHPStan

Slide 34

Slide 34 text

੩తղੳπʔϧΛ࢖͓͏ / PhpStorm PHP PHPStan Psalm lint ( )

Slide 35

Slide 35 text

PHPStan

Slide 36

Slide 36 text

= CI ࢀߟ 
 IUUQTQIQTUBOPSHSGDGBDGCDCEGC

Slide 37

Slide 37 text

PHPDoc @param / @return ࢀߟ 
 IUUQTQIQTUBOPSHSGFFECEEEDG

Slide 38

Slide 38 text

: `key-of- <***>` `value-of-<***>` ( ) ࢀߟ 
 IUUQTQIQTUBOPSHSCDGDFCBCCBBEE

Slide 39

Slide 39 text

: int `int` ࢀߟ 
 IUUQTQIQTUBOPSHSEFFGDDCCBEEFE

Slide 40

Slide 40 text

: (key, value ) iterable array mixed ࢀߟ 
 IUUQTQIQTUBOPSHSEG ff EDFBBBFD

Slide 41

Slide 41 text

: DTO ࢀߟ 
 IUUQTQIQTUBOPSHSDCFGGCGGGFE

Slide 42

Slide 42 text

৭ʑͳܕͷදݱ : https://phpstan.org/writing-php- code/phpdoc-types

Slide 43

Slide 43 text

PHPStan

Slide 44

Slide 44 text

Πϯετʔϧ͢Δ

Slide 45

Slide 45 text

ઃఆϑΝΠϧΛઃஔ͢Δ https://phpstan.org/con fi g-reference

Slide 46

Slide 46 text

࣮ߦ͢Δ

Slide 47

Slide 47 text

( OK)

Slide 48

Slide 48 text

ಋೖ࣌ʹ͓֮͑ͯ͘ͱྑͦ͞͏ͳ֓೦ Error Rule/RuleSet Rule RuleSet Rule Rule level PHPStan Ruleset 0 9(max) level

Slide 49

Slide 49 text

Rule Levelͷྫ https://github.com/phpstan/phpstan-src/blob/1.10.25/conf/con fi g.level6.neon

Slide 50

Slide 50 text

ಋೖ࣌ʹ͓֮͑ͯ͘ͱྑͦ͞͏ͳ֓೦ Baseline ( ) Error `phpstan analyze generate-baseline` Error ignoreErrors con fi g 1 ( )Error Baseline ignoreErrors

Slide 51

Slide 51 text

Baselineͷྫ(CakePHP) https://github.com/cakephp/cakephp/blob/4.4.15/phpstan-baseline.neon

Slide 52

Slide 52 text

PHPStanͷ΍͍͞͠ಋೖʹ޲͚ͯ PJ Level OK Baseline PHPStan ignoreErrors( Baseline ) Error 
 (cf: https://phpstan.org/user-guide/ignoring-errors#reporting-unused- ignores)

Slide 53

Slide 53 text

PHPStanͷ΍͍͞͠ಋೖʹ޲͚ͯ PJ Level CI 5 6

Slide 54

Slide 54 text

PHPStanͷ΍͍͞͠ಋೖʹ޲͚ͯ 連 Error etc

Slide 55

Slide 55 text

΋ͬͱڧྗͳ׆༻ʹ޲͚ͯ PHPStan https://phpstan.org/writing-php-code/phpdoc-types https://phpstan.org/con fi g-reference https://phpstan.org/user-guide/extension-library OSS

Slide 56

Slide 56 text

ͱͯ΋ࢀߟʹͳΔࢿྉͨͪ PHPStan #phpcon_okinawa pixivFANBOX 
 https://tadsan.fanbox.cc/posts/4348669 #phpcon_okinawa pixivFANBOX 
 https://tadsan.fanbox.cc/posts/2558149 level=0 PHPStan(Larastan) - Shin x Blog 
 https://blog.shin1x1.com/entry/getting-stated-with-phpstan CI / Great static analysis with CI - Speaker Deck 
 https://speakerdeck.com/oogfranz/great-static-analysis-with-ci