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

PHPにおける独自例外設計を考える / A way to design user-defined exception with PHP

PHPにおける独自例外設計を考える / A way to design user-defined exception with PHP

PHPカンファレンス沖縄2019の懇親会LTで話したPHPの独自例外、とくに独自基底例外クラスについてアイデアです

Kazuki Higashiguchi

October 12, 2019
Tweet

More Decks by Kazuki Higashiguchi

Other Decks in Technology

Transcript

  1. © - BASE, Inc. A way to design user-defined exception

    in PHP 〜PHPにおける独⾃例外設計を考える〜 PHPカンファレンス沖縄 2019 . . - @hgsgtk
  2. © - BASE, Inc. Talk Abstract • PHP ( >=)における独⾃例外設計の考え⽅

    • How to design user-defined exception with PHP ( >=) • 特にアプリケーション領域の基底クラスの設計 • Especially, a base exception class of application domain • Symfonyなどのフレームワークの内部実装も眺めて 参考にする • Refer to internal implementations of some application frameworks
  3. © - BASE, Inc. var_dump($hgsgtk); : @hgsgtk Kazuki Higashiguchi BASE

    BANK, Inc. / Dev Division / Tech Lead Backend Engineer
  4. © - BASE, Inc. Motivation of error handling by exception

    • 適切なスコープをきってハンドリングしたい • I want to handle errors with appropriate scopes • 実⾏時エラー‧検査時エラー • Runtime error / Inspection error • ⾃分たちのチームコードのドメイン内‧外 • Inside and outside of our development code
  5. PHP Exception classes • Throwable: the base interface • Error

    and Exception class implements Throwable interface. © - BASE, Inc. https://www.php.net/manual/en/class.throwable.php
  6. © - BASE, Inc. 正当性と堅牢性 (Legitimacy and Robustness) 正当性: システムの仕様、設計、実装の不具合のなさ

    Robustness: No faults in system specifications, design and implementation 堅牢性: 予期せぬ状況下でも機能し続けられる能⼒ Robustness: Ability to continue to function under unexpected circumstances Steve McConnell. Code Complete 第2版 完全なプログラミングを⽬指して / 第5部 コードの改良
  7. © - BASE, Inc. 正当性と堅牢性 (Legitimacy and Robustness) “正当性にこだわると堅牢性が損なわれるし、堅牢性 にこだわると正当性が損なわれる。”

    “Sticking to legitimacy impairs robustness, and sticking to legitimacy impairs legitimacy.” Steve McConnell. Code Complete 第2版 完全なプログラミングを⽬指して / 第5部 コードの改良
  8. © - BASE, Inc. Legitimate in the micro, robustness in

    the macro https://speakerdeck.com/twada/php-conference- ?slide= PHP で堅牢なコードを書く - 例外処理、表明プログラミング、契約による設計 / PHP Conference by Takuya Wada “個々のクラスは正当性を重視し堅牢性はアーキテ クチャ/フレームワーク等で保証するのがオススメ” “It is recommended that each class emphasizes legitimacy and robustness is guaranteed by architecture or framework etc”
  9. © - BASE, Inc. 考えられる独⾃例外の⽅針 (A exception design policy) •

    適切なスコープでのエラーハンドリングを促す • Encourage error handling with appropriate scope • 堅牢性をもったエラーハンドリングするための、 チームコードの⼤スコープを補⾜できる • Complement the large scope of team code for robust error handling
  10. © - BASE, Inc. ex: Symfony Component user-defined exception https://github.com/symfony/symfony/blob/cd

    ccf a d f ed c fd a c fd d /src/Symfony/Component/ Validator/Exception/InvalidArgumentException.php#L
  11. © - BASE, Inc. https://github.com/symfony/symfony/blob/ c fa ff fd d

    c fca f ac/src/Symfony/Component/ Validator/Exception/ExceptionInterface.php#L ex: Symfony Component user-defined exception
  12. © - BASE, Inc. • Componentのスコープで、Throwableを継承した 独⾃インタフェースを作成している • Create a

    user-defined interface that inherits Throwable in the scope of Component • ⇒⾃分たちで作る例外全体を補⾜できる • -> Supplement the entire exception you create ✍ Symfony Component user-defined exception
  13. © - BASE, Inc. • 独⾃例外はPHP標準の例外クラスを継承しつつ、独 ⾃インタフェースを実装する構成 • User-defined exception

    inherits PHP standard exception classes and implement user-defined interface • ⇒独⾃例外をそれぞれの要件に合わせた適切な粒 度へ • -> create exception classes proper granularity tailored to your requirements ✍ Symfony Component user-defined exception
  14. © - BASE, Inc. A Base exception interface implements throwable

    interface A idea to make your user-defined exception
  15. © - BASE, Inc. • Throwableを継承した独⾃インターフェースをもっ て、⽣まれうる例外クラス全体の⼤きなスコープを 作れる • You

    can create a large scope for the entire user-defined exception class Benefits by making your user-defined exception
  16. © - BASE, Inc. • 独⾃例外をスコープを意識して使うように促せる • Encourage developers to

    use user-defined exceptions with proper scope in mind • 例:実⾏時or例外時、アプリケーションレイ ヤー …etc) • ex. Runtime or Inspection, application layer etc Benefits by making your user-defined exception
  17. © - BASE, Inc. Summary • 適切なスコープで例外を使い分ける • Use different

    exceptions with appropriate scope • 正当性と堅牢性 • Legitimacy and Robustness • それらを可能にするためのPHPでの基底例外 • Base exception to make them possible in PHP