Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
楽をするためのPHPDocと配列型の落とし穴 / PHPDoc and pitfalls of...
Search
takaram
August 24, 2020
Programming
3
5.2k
楽をするためのPHPDocと配列型の落とし穴 / PHPDoc and pitfalls of array
takaram
August 24, 2020
Tweet
Share
More Decks by takaram
See All by takaram
レガシーシステムに自動テストを導入する第一歩 / 2020-phpcon-tdd-in-legacy-product
takaram
1
3k
Other Decks in Programming
See All in Programming
Bytecode Manipulation 으로 생산성 높이기
bigstark
2
360
関数型まつりレポート for JuliaTokai #22
antimon2
0
130
社内での開発コミュニティ活動とモジュラーモノリス標準化事例のご紹介/xPalette and Introduction of Modular monolith standardization
m4maruyama
1
130
業務自動化をJavaとSeleniumとAWS Lambdaで実現した方法
greenflagproject
1
120
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
41
27k
Development of an App for Intuitive AI Learning - Blockly Summit 2025
teba_eleven
0
120
セキュリティマネジャー廃止とクラウドネイティブ型サンドボックス活用
kazumura
1
190
複数アプリケーションを育てていくための共通化戦略
irof
10
4k
Passkeys for Java Developers
ynojima
3
870
プロダクト開発でも使おう 関数のオーバーロード
yoiwamoto
0
160
機械学習って何? 5分で解説頑張ってみる
kuroneko2828
0
220
[初登壇@jAZUG]アプリ開発者が気になるGoogleCloud/Azure+wasm/wasi
asaringo
0
130
Featured
See All Featured
The Cult of Friendly URLs
andyhume
79
6.4k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
4
200
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
Agile that works and the tools we love
rasmusluckow
329
21k
Producing Creativity
orderedlist
PRO
346
40k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.9k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Code Reviewing Like a Champion
maltzj
524
40k
Optimising Largest Contentful Paint
csswizardry
37
3.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Transcript
楽をするためのPHPDocと 配列型の落とし穴 2020.8.21 @PHP LT Fes
自己紹介 • 名前 ◦ 荒巻 拓哉 • 所属 ◦ 株式会社ラクス
▪ 配配メール というサービスの開発をしてます
PHPDoc、書いてますか?
https://github.com/symfony/symfony/blob/03155addd7c96a9455660b7d51d993cf4f8eb201/src/Symfony/Component/ HttpFoundation/InputBag.php
• コードを読む際に理解しやすい • ドキュメントを生成できる → phpDocumentor • IDEの補完が効きやすくなる → PhpStorm
• 静的解析ができる → PHPStan, Phan など 何のために書くの?
ドキュメント生成 https://github.com/PHPMailer/PHPMailer/blob/e7acd3d92fa02728c51bf64b90615bffc84815f0/src/PHPMailer.php
ドキュメント生成 http://phpmailer.github.io/PHPMailer/classes/PHPMailer.PHPMailer.PHPMailer.html#method_html2text
IDEで補完
静的解析 引数の型が違う、プロパティ・メソッドが存在しない、などを検出 DateTimeクラスにないメ ソッド 引数の型が違う
静的解析 PHPStan
静的解析 Phan
PHPDocの仕様? 各ツールでサポートする記法が少しずつ異なる PSR-5, PSR-19として標準化する動きはあるが、現時点ではまだ提案段階
PHPDocを書いてみる 題材:Userクラス ORマッパーのEntityクラスのイメージ
None
PHPDocを書いてみる PHPDocをちゃんと書いてるからヨシ! ……? → 全ユーザのユーザ名を表示する関数を書いてみる
補完が効かない
補完が効かない
静的解析でエラー検知できない PHPStan Phan
なんで? array型は中の要素の型はわからない → 補完も静的解析もできない
なんとかならないの? User[] と書きましょう プリミティブの配列でもOK • int[] • string[] • (User|string)[]※PhpStorm
2020.2時点で未対応
→
None
PHPStan Phan
その他の配列型の書き方 @return array<int> @return array<string, int> @return array{id: int, name:
string} 上記3つはPhpStorm 2020.2では未対応 (ただのarray扱いになる)
まとめ • PHPDocを書くと楽できる ◦ 自動でドキュメントを作ってくれる ◦ IDEで補完できる ◦ 静的解析ができる •
配列は要素の型も書く ◦ int[]形式 ◦ 連想配列はarray<string, int> か array{id: int}形式 ▪ ただしPhpStormは未対応 • PHPDocはしっかり書きましょう!!!
参考資料 • 2018年のPHPDoc事情とPSR-5 - Qiita https://qiita.com/tadsan/items/72b02339d12120ca37d7 • array shapes記法(Object-like arrays)と旧PSR-5記法で型をつける
- Qiita https://qiita.com/tadsan/items/bfa9465166c351da37e5 • PSR-19: PHPDoc tagsを予習してみよう - BASE開発チームブログ https://devblog.thebase.in/entry/2019/12/14/110000