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

PhpStormの静的解析

 PhpStormの静的解析

JetBrains IDEといえば超強力リファクタリング機能!
なのに何故なんだ、何故PHPだとうまく動かない?
その原因はやはり動的型付け言語故の問題があった。

それでもPhpStorm動き方の癖を覚えればバッチリ安心して使える。
癖を覚えてリファクタリングをガンガン使っていこう!

そんな内容をPhpStormの静的解析機能を紹介しながら語りました。

Satoshi Kawashima

July 25, 2017
Tweet

More Decks by Satoshi Kawashima

Other Decks in Technology

Transcript

  1. $male = new Male();
 $male->greeting();
 
 $robot = new Robot();


    $robot->greeting();
 
 $str = 'string desuyo';
 $str->greeting();
 
 $male->greeting(); 1IQ4UPSNͷܕਪ࿦ class Male
 {
 public function greeting()
 {
 echo '͜Μʹͪ͸';
 }
 } class Robot
 {
 public function greeting()
 {
 echo 'ίϯχνϋ';
 }
 } %FDMBSBUJPO %FDMBSBUJPO NBMF͸.BMFDMBTTͷ ΦϒδΣΫτ΍ʂ SPCPU͸3PCPUDMBTTͷ ΦϒδΣΫτ΍ʂ TUSJOH͔ΒNFUIPEݺͼग़͠ ͳΜͯͰ͖΁ΜͰʂ 'JOE6TBHF
  2. %FDMBSBUJPOͱ'JOE6TBHF function greeting($target)
 {
 $str = '͜Μʹͪ͸';
 
 $str =

    'Hello, ';
 
 echo $str . $target;
 } %FDMBSBUJPO 'JOE6TBHF
  3. %FDMBSBUJPOͱ'JOE6TBHF function hogehoge()
 {
 echo 'hoge';
 } hogehoge(); %FDMBSBUJPO 'JOE6TBHF

    function hogehoge()
 {
 echo 'hoge';
 } hogehoge(); hogehoge(); hogehoge();
  4. abstract class Human
 {
 public function greeting()
 {
 echo 'Hello';


    }
 } %FDMBSBUJPOͱ'JOE6TBHF $male = new Male();
 $male->greeting();
 
 $female = new Female();
 $female->greeting(); class Male extends Human
 {
 public function greeting()
 {
 echo '͜Μʹͪ͸';
 }
 } class Female extends Human
 {
 } %FDMBSBUJPO %FDMBSBUJPO 'JOE6TBHF 'JOE6TBHF
  5. "OBMZ[F%BUB'MPX5Pͷ৔߹ class Male extends Human
 {
 private $data;
 
 public

    function __construct($data)
 {
 $this->data = $data;
 $this->data->hoge();
 
 parent::__construct('male');
 }
 } function makeModel()
 {
 $dsn = 'phptype://xxxxxxxxxxxxx';
 return new HogeModel($dsn);
 } $fuga = new Fuga();
 $mdl = makeModel();
 
 $fuga->setHuman($mdl); class Fuga
 {
 public function setHuman($mdl)
 {
 $this->male = new Male($mdl);
 }
 }
  6. 3FOBNF class Robot
 {
 public static function greeting()
 {
 //

    ...
 }
 }
 
 $robot = new Robot();
 
 Robot::greeting(); class Android
 {
 public static function greeting()
 {
 // ...
 }
 }
 
 $robot = new Android();
 
 Android::greeting(); OFX͍ͯ͠ΔՕॴΛࣗಈͰमਖ਼ TUBUJDGVODUJPOݺͼग़͠Օॴ΋ ࣗಈͰमਖ਼ ɾDMBTT໊Λมߋ
  7. $IBOHF4JHOBUVSF function hoge($a, $b)
 {
 return $a + $b;
 }


    
 hoge(1, 3); function hoge($b, $a, $c)
 {
 return $a + $b;
 }
 
 hoge(3, 1, 'fuga'); ɾҾ਺ͷॱংʢBͱCʣΛมߋ ɾDΛ௥ՃʢσϑΥϧτ`GVHB`ʣ ݺͼग़͠ݩͷҾ਺ॱΛ ม͑ͯ͘ΕΔ ৽͍͠Ҿ਺ʹσϑΥϧτ ͰೖΕΔ஋ΛܾΊΒΕΔ
  8. &YUSBDUܥ abstract class Human
 {
 private $name;
 
 public function

    __construct($age, $address, $name)
 {
 $name = 'hoge';
 
 $data['age'] = $age;
 $data['address'] = $address;
 }
 
 private function setData($age, $address)
 {
 $data['age'] = $age;
 $data['address'] = $address;
 return $data;
 }
 public function greeting()
 {
 echo '͜Μʹͪ͸';
 } } &YUSBDU'JFME ʢϑΟʔϧυ΁੾Γग़͠ʣ &YUSBDU1BSBNFUFS ʢҾ਺΁੾Γग़͠ʣ &YUSBDU.FUIPE ʢผNFUIPE΁੾Γग़͠ʣ JOUFSGBDF4QFBLBCMF*OUFSGBDF \ QVCMJDGVODUJPOTQFBL  ^ &YUSBDU*OUFSGBDF ʢΠϯλʔϑΣʔεͷ੾Γग़͠ʣ
  9. 1VTI.FNCFST%PXO1VMM.FNCFST6Q abstract class Human
 {
 public function speak()
 {
 echo

    'hoge';
 }
 } class Male extends Human
 { QVCMJDGVODUJPOTQFBL  \ FDIPbIPHF` ^ ^ 1VTI.FNCFST%PXO ʢϝϯόఆٛΛܧঝઌ΁Ҡಈʣ 1VMM.FNCFST6Q ʢϝϯόఆٛΛܧঝݩ΁Ҡಈʣ
  10. ର৅Λݶఆ͢Δ άϩʔόϧε ίʔϓͷม਺ ϩʔΧϧεί ʔϓͷม਺ GVODUJPO DMBTT QVCMJD QSPQFSUZ QSJWBUF

    QSPUFDUFE QSPQFSUZ QVCMJD NFUIPE QSJWBUF QSPUFDUFE NFUIPE %FDMBSBUJP O 'JOE 6TBHF
  11. ϩʔΧϧείʔϓͷม਺ͷ'JOE6TBHF function greeting($target)
 {
 $str = '͜Μʹͪ͸ɺ';
 $target .= '͞Μɻ';


    
 echo $str . $target;
 } %FDMBSBUJPO 'JOE6TBHF ˞جຊతʹ࿙Ε΋ɺϊΠζ΋ͳ͍
  12. DMBTTͷ'JOE6TBHF DMBTT)PHF  \ ^ DMBTT)PHF  \ ^ IPHFOFX)PHF

     %FDMBSBUJPO 'JOE6TBHF ˞جຊతʹ࿙Ε͸ͳ͍ ˞ಉ໊ͷDMBTT͕ͳ͚Ε͹શͯͷݺͼग़͠ݩ͕ಛఆͰ͖ΔʢϊΠζ͕ࠞ͟Βͳ͍ʣ IPHFOFX)PHF  )PHFTPNF.FUIPE  )PHFTPNF.FUIPE 
  13. QSJWBUFQSPUFDUFE QSPQFSUZNFUIPEͷ'JOE6TBHF class Male extends Human
 {
 public function __construct()


    {
 parent::__construct('Male');
 }
 
 public function greeting()
 {
 echo $this->name;
 }
 } abstract class Human
 {
 protected $name;
 
 public function __construct($name)
 {
 $this->name = $name;
 }
 } %FDMBSBUJPO 'JOE6TBHF ˞ಉ໊ͷDMBTT͕ͳ͚Ε͹࿙Ε΋ϊΠζ΋ͳ͍ ˞ಉ໊ͷDMBTT͕͋Δͱ͓͔͘͠ͳΔ
  14. ͜͜·Ͱͷ·ͱΊ άϩʔόϧε ίʔϓͷม਺ ϩʔΧϧεί ʔϓͷม਺ GVODUJPO DMBTT QVCMJD QSPQFSUZ QSJWBUF

    QSPUFDUFE QSPQFSUZ QVCMJD NFUIPE QSJWBUF QSPUFDUFE NFUIPE %FDMBSBUJP O ̋ ̋ ʢ˞̍ʣ ̋ ʢ˞̍ʣ ̋ ʢ˞̍ʣ ̋
 ʢ˞̍ʣ 'JOE 6TBHF ̋ ̋ ʢ˞̍ʣ ̋ ʢ˞̍ʣ ̋
 ʢ˞̍ʣ ̋
 ʢ˞̍ʣ ˞̍ɿಉ໊ͷGVODUJPODMBTTఆ͕ٛͳ͚Ε͹େৎ෉
  15. ͜͜·Ͱͷ·ͱΊ άϩʔόϧε ίʔϓͷม਺ ϩʔΧϧεί ʔϓͷม਺ GVODUJPO DMBTT QVCMJD QSPQFSUZ QSJWBUF

    QSPUFDUFE QSPQFSUZ QVCMJD NFUIPE QSJWBUF QSPUFDUFE NFUIPE %FDMBSBUJP O ̋ ̋ ʢ˞̍ʣ ̋ ʢ˞̍ʣ ̋ ʢ˞̍ʣ ̋
 ʢ˞̍ʣ 'JOE 6TBHF ̋ ̋ ʢ˞̍ʣ ̋ ʢ˞̍ʣ ̋
 ʢ˞̍ʣ ̋
 ʢ˞̍ʣ ˞̍ɿಉ໊ͷGVODUJPODMBTTఆ͕ٛͳ͚Ε͹େৎ෉ ͜͜Β΁Μ͕໰୊͕ൃੜ͠΍͍͢ʂʂ ˞࿦֎
  16. Կނ'JOE6TBHF͕ಇ͔ͳ͍ͷ͔ʁ wQVCMJDQSPQFSUZNFUIPEͷΞΫηε͸εςʔτϝϯτͰ֬ఆ͢Δ wϑΝΠϧείʔϓΛ·ͨ͗͏Δ wϑΝΠϧείʔϓΛ·͙ͨͱجຊతʹܕਪ࿦Ͱ͖ͳ͍ $male = new Male();
 $male->greeting();
 


    speak($male); function speak($male)
 {
 $male->greeting();
 } class Male
 {
 public function greeting()
 {
 echo 'Hello';
 }
 } NBMF͸.BMFDMBTTͷ ΦϒδΣΫτ΍ʂ ͜Εͳʹʁʁʁʁʁ
  17. ϩʔΧϧείʔϓͷม਺ͷܕਪ࿦ wJG͕͋ͬͨ৔߹ɺશͯͷՄೳੑͷ࿨ू߹ͱͳΔ w˞1IQ4UPSNͷܕਪ࿦͸ܕΛҰҙʹܾΊͣɺෳ਺ͷՄೳੑΛߟྀ͠ ͍ͯΔ function greeting()
 {
 $str = '͜Μʹͪ͸ɺ';


    $target = new Robot();
 
 if (rand(1, 0)) {
 $target = new Male();
 } else if (rand(1, 0)) {
 $target = new Female();
 }
 
 echo $str . $target->name;
 } 3PCPU ·ͨ͸ .BMF ·ͨ͸ 'FNBMF
  18. ϩʔΧϧείʔϓͷม਺ͷܕਪ࿦ wJGdFMTFͷ৔߹ɺJG෼ذͷதͷ͍ͣΕ͔͕࣮ߦ͞ΕΔ͸ͣͳͷͰɺͦΕ ΑΓ΋લͷܕ͸ແࢹ͞ΕΔ function greeting()
 {
 $str = '͜Μʹͪ͸ɺ';
 $target

    = new Robot();
 
 if (rand(1, 0)) {
 $target = new Male();
 } else {
 $target = new Female();
 }
 
 echo $str . $target->name;
 } .BMF ·ͨ͸ 'FNBMF
  19. ໭Γ஋ͷܕਪ࿦ wશͯͷSFUVSOͷ࿨ू߹ function makeHuman()
 {
 if (rand(0, 1)) {
 return

    new Male();
 } else {
 return new Female();
 }
 
 return new Robot();
 } $human = makeHuman(); .BMF ·ͨ͸ 'FNBMF ·ͨ͸ 3PCPU ࣮ߦ࣌ʹઈରݺ͹Εͳ͍ ໭Γ஋΋ߟྀʹ͍Εͯ͠ ·͏
  20. ໭Γ஋ͷܕਪ࿦ w໭Γ஋ͷܕࢦఆ 1)1 ·ͨ͸1)1%PD͕͋Ε͹ɺͦͪΒΛ༏ઌ͢Δ w྆ํ͋Δ৔߹͸ͦͷ࿨ू߹ͱͳΔ w༏ઌॱҐɿܕࢦఆ1)1%PDSFUVSOͷ࿨ू߹ /**
 * @return Male


    */
 function makeHuman(): Human
 {
 if (rand(0, 1)) {
 return new Male();
 }
 
 return new Robot();
 } $human = makeHuman(); )VNBO ·ͨ͸ .BMF
  21. QSPQFSUZͷܕਪ࿦ wͦͷείʔϓͰͷ୅ೖͱɺίϯετϥΫλͰͷ୅ೖͷ࿨ू߹ͱͳΔ w਌DMBTTͷίϯετϥΫλͰ୅ೖ͞Ε͍ͯͯ΋ղੳର৅ͱ͢Δ class Fuga extends Hoge
 {
 public function

    greeting()
 {
 $this->human = new Female();
 $this->human->greeting();
 }
 } class Hoge
 {
 protected $human;
 
 public function __construct()
 {
 $this->human = new Male();
 }
 } .BMF ·ͨ͸ 'FNBMF
  22. QSPQFSUZͷܕਪ࿦ wղੳର৅ͷDMBTTͷίϯετϥΫλͷํ͕ɺ਌DMBTTͷίϯετϥΫλ ΑΓ༏ઌ͞ΕΔʢͪΐͬͱෆࢥٞͳڍಈɾɾɾʣ class Fuga extends Hoge
 { public function

    __construct()
 {
 $this->human = new Robot();
 parent::__construct();
 } 
 public function greeting()
 {
 $this->human = new Female();
 $this->human->greeting();
 }
 } class Hoge
 {
 protected $human;
 
 public function __construct()
 {
 $this->human = new Male();
 }
 } 3PCPU ·ͨ͸ 'FNBMF ਌DMBTTͰ.BMFͰ୅ೖ͞Ε Δ͔Βҙຯ͕ແ͍୅ೖ
  23. QSPQFSUZͷܕਪ࿦ w1)1%PD͕͋Ε͹ɺͦΕ΋࿨ू߹ʹՃΘΔ w༏ઌॱҐɿͦͷείʔϓͰͷ୅ೖʹίϯετϥΫλʹ1)1%PD class Fuga extends Hoge
 {
 public function

    greeting()
 {
 $this->human = new Female();
 $this->human->greeting();
 }
 } class Hoge
 { /**
 * @var Robot
 */
 protected $human;
 
 public function __construct()
 {
 $this->human = new Male();
 }
 } .BMF ·ͨ͸ 'FNBMF ·ͨ͸ 3PCPU
  24. ·ͱΊ άϩʔόϧε ίʔϓͷม਺ ϩʔΧϧεί ʔϓͷม਺ GVODUJPO DMBTT QVCMJD QSPQFSUZ QSJWBUF

    QSPUFDUFE QSPQFSUZ QVCMJD NFUIPE QSJWBUF QSPUFDUFE NFUIPE %FDMBSBUJP O º ʢ˞̏ʣ ̋ ̋ ʢ˞̍ʣ ̋ ʢ˞̍ʣ ˚
 ʢ˞̎ʣ ̋ ʢ˞̍ʣ ˚
 ʢ˞̎ʣ ̋
 ʢ˞̍ʣ 'JOE 6TBHF º ʢ˞̏ʣ ̋ ̋ ʢ˞̍ʣ ̋ ʢ˞̍ʣ ˚
 ʢ˞̎ʣ ̋
 ʢ˞̍ʣ ˚
 ʢ˞̎ʣ ̋
 ʢ˞̍ʣ ˞̍ɿಉ໊ͷGVODUJPODMBTTఆ͕ٛͳ͚Ε͹େৎ෉ ˞̎ɿݺͼग़͠ݩશͯͰܕਪ࿦͕Ͱ͖͍ͯͳ͚Ε͹࿙Ε͕ൃੜ͢Δ ˞̏ɿશάϩʔόϧείʔϓͰϢχʔΫͳ໊લͷม਺ͩͬͨΒΘΓͱ͍͚Δ
  25. ༨ஊɿઈରʹղੳͰ͖ͳ͍ύλʔϯ̎ wϩʔΧϧείʔϓ಺ͰͷϑΝΠϧಡΈࠐΈ wϑΝΠϧ୯ҐͰղੳ͢ΔͬΆ͍͔Βɺάϩʔόϧείʔϓͱͯ͠ѻΘ Εͯ͠·͏ function requireFuga()
 {
 $male = new

    Male();
 
 require_once __DIR__ . '/fuga.php';
 } $male->greeting(); શͯͷϑΝΠϧͷάϩʔόϧείʔϓͷ ಉ໊ͷม਺ͷ࿨ू߹ ʢάϩʔόϧείʔϓͱͯ͠ղੳ͢Δ͔Βʣ GVHBQIQ