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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
takaram
August 24, 2020
Programming
5.6k
3
Share
楽をするためのPHPDocと配列型の落とし穴 / PHPDoc and pitfalls of array
takaram
August 24, 2020
More Decks by takaram
See All by takaram
レガシーシステムに自動テストを導入する第一歩 / 2020-phpcon-tdd-in-legacy-product
takaram
1
3.3k
Other Decks in Programming
See All in Programming
PDI: Como Alavancar Sua Carreira e Seu Negócio
marcelgsantos
0
120
[RubyKaigi 2026] Require Hooks
palkan
1
200
Cache-moi si tu peux : patterns et pièges du cache en production - Devoxx France 2026 - Conférence
slecache
0
240
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
4
3k
mruby on C#: From VM Implementation to Game Scripting (RubyKaigi 2026)
hadashia
2
460
PHPで TLSのプロトコルを実装してみるをもう一度しゃべりたい
higaki_program
0
200
iOS機能開発のAI環境と起きた変化
ryunakayama
0
180
VueエンジニアがReactを触って感じた_設計の違い
koukimiura
0
180
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
320
How We Benchmarked Quarkus: Patterns and anti-patterns
hollycummins
1
140
Reactive ❤️ Loom: A Forbidden Love Story
franz1981
2
240
Coding as Prompting Since 2025
ragingwind
0
830
Featured
See All Featured
The Spectacular Lies of Maps
axbom
PRO
1
700
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Are puppies a ranking factor?
jonoalderson
1
3.3k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
370
Thoughts on Productivity
jonyablonski
76
5.1k
How to build a perfect <img>
jonoalderson
1
5.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.6k
Technical Leadership for Architectural Decision Making
baasie
3
330
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
510
Building an army of robots
kneath
306
46k
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