Slide 1

Slide 1 text

© - BASE, Inc. A way to design user-defined exception in PHP 〜PHPにおける独⾃例外設計を考える〜 PHPカンファレンス沖縄 2019 . . - @hgsgtk

Slide 2

Slide 2 text

© - BASE, Inc. HAISAI OKINAWA!!!

Slide 3

Slide 3 text

© - 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

Slide 4

Slide 4 text

© - BASE, Inc. PHP >= sorry

Slide 5

Slide 5 text

© - BASE, Inc. var_dump($hgsgtk); : @hgsgtk Kazuki Higashiguchi BASE BANK, Inc. / Dev Division / Tech Lead Backend Engineer

Slide 6

Slide 6 text

© - BASE, Inc. ++ in Okinawa https://speakerdeck.com/hgsgtk/design-not-to-broke- additional-window https://speakerdeck.com/hgsgtk/understanding-design- pattern-from-the-origin 10.11 前夜祭 Session 10.12 本編 LT

Slide 7

Slide 7 text

© - 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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

© - 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部 コードの改良

Slide 10

Slide 10 text

© - BASE, Inc. 正当性と堅牢性 (Legitimacy and Robustness) “正当性にこだわると堅牢性が損なわれるし、堅牢性 にこだわると正当性が損なわれる。” “Sticking to legitimacy impairs robustness, and sticking to legitimacy impairs legitimacy.” Steve McConnell. Code Complete 第2版 完全なプログラミングを⽬指して / 第5部 コードの改良

Slide 11

Slide 11 text

© - 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”

Slide 12

Slide 12 text

© - BASE, Inc. 考えられる独⾃例外の⽅針 (A exception design policy) • 適切なスコープでのエラーハンドリングを促す • Encourage error handling with appropriate scope • 堅牢性をもったエラーハンドリングするための、 チームコードの⼤スコープを補⾜できる • Complement the large scope of team code for robust error handling

Slide 13

Slide 13 text

© - 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

Slide 14

Slide 14 text

© - 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

Slide 15

Slide 15 text

© - 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

Slide 16

Slide 16 text

© - 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

Slide 17

Slide 17 text

© - BASE, Inc. A idea to make your user-defined exception

Slide 18

Slide 18 text

© - BASE, Inc. A Base exception interface implements throwable interface A idea to make your user-defined exception

Slide 19

Slide 19 text

© - BASE, Inc. 独⾃基底例外インターフェースを実装した個別の例外 クラスを作成していく Create exception classes that implement own base exception interface A idea to make your user-defined exception

Slide 20

Slide 20 text

© - BASE, Inc. • Throwableを継承した独⾃インターフェースをもっ て、⽣まれうる例外クラス全体の⼤きなスコープを 作れる • You can create a large scope for the entire user-defined exception class Benefits by making your user-defined exception

Slide 21

Slide 21 text

© - 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

Slide 22

Slide 22 text

© - BASE, Inc. Summary • 適切なスコープで例外を使い分ける • Use different exceptions with appropriate scope • 正当性と堅牢性 • Legitimacy and Robustness • それらを可能にするためのPHPでの基底例外 • Base exception to make them possible in PHP

Slide 23

Slide 23 text

© - BASE, Inc.