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
Bye-bye Magic Number
Search
hideki kinjyo
PRO
September 18, 2018
1
1.3k
Bye-bye Magic Number
CakePHP3アプリケーションで、「マジックナンバー」的な値とどう向き合っていくかを考えた結果
hideki kinjyo
PRO
September 18, 2018
Tweet
Share
More Decks by hideki kinjyo
See All by hideki kinjyo
色んなオートローダーを覗き見る #phpcon_okinawa
o0h
PRO
5
490
ヒューマンエラーの本を読んだ ~報告会~
o0h
PRO
3
260
みんなでワイワイ「テスト駆動開発」の話をやる会 #techramen24conf
o0h
PRO
3
490
SPLから始める「データ構造」入門
o0h
PRO
7
1.8k
PHPUnit11の新しい仲間たち
o0h
PRO
3
360
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
61
20k
パンフ記事 「初めてのリファクタリング!」 の裏側 #phperkaigi
o0h
PRO
2
150
phpunit/php-code-coverageって何をしてるんだ #phperkaigi
o0h
PRO
3
1.3k
Composerを便利に使うために私がやっていること #phperkaigi
o0h
PRO
1
2.6k
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Speed Design
sergeychernyshev
25
660
GitHub's CSS Performance
jonrohan
1030
460k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Navigating Team Friction
lara
183
15k
Why Our Code Smells
bkeepers
PRO
335
57k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
It's Worth the Effort
3n
183
28k
Transcript
Bye-Bye Magic Number ʙʮࣙॻʯΛ࡞ͬͨʙ Hideki Kinjyo twitter: @o0h_ / github:
o0h
ࣗݾհ • ίωώτגࣜձࣾ • αʔόʔαΠυΤϯδχΞ • ओʹCakePHPͳͲ
ࠓ͓͢Δ͜ͱ • ϚδοΫφϯόʔʹউͭͧɾɾɾʂ • ͦͷͨΊʹ”Ϟϊ”Λ࡞Γ·ͨ͠
Έͳ͞Μɺ ΞϓϦέʔγϣϯͷʮఆʯ ͷཧͲ͏͍ͯ͠·͔͢ʁ ϚδοΫφϯόʔ తͳͭ
͜͏͍͏ͭ • Ϣʔβʔͷొঢ়ଶ͕ɾɾ • 0: ϝʔϧΞυϨε֬ೝͪ • 1: ࢧ͍ใ֬ೝͪ •
2: ొྃ
ᶃϚδοΫφϯόʔਏ͍
ᶃϚδοΫφϯόʔਏ͍ • ಡΜͰ͍ͯʮಥવग़͖ͯͨʯײ͡ • ʮಛผͳҙຯͷ͋Δʯʁʮྔͱ͔߹͍Λද͍ͯ͠ Δʯʁ • ผ͕ͮ͠Β͍ • grep͠ʹ͘͞
=> ϦϑΝΫλ͠ʹ͘͞ • etc… (άάͬͨΒ৭ʑग़ͯ͘ΔͷͰׂѪ
ᶄϞσϧΫϥεͷఆʹʂ
ᶄϞσϧΫϥεͷఆʹʂ • ୯ޠͰ໊લ͕͚ΒΕ͍ͯΔ͔Β͔Γ͢ ͍Ͷʂ • ؔ࿈͢ΔࣝΛ1Օॴʹ·ͱΊΒΕ͔ͯΓ ͍͢Ͷʂ(scoped, contextual)
ᶄϞσϧΫϥεͷఆʹʂ • ʮͲͷΫϥεʹஔ͔͘ʯ·͍͠ • ͋ΕɺTableʹ࣋ͨͤΔʁEntityʹ࣋ͨͤ Δʁ • ͦͦϞσϧͱؔ࿈͠ͳ͍ࣝͲ͏͢Δʁ
ᶅConfigureΫϥεͰѻ͏ʂ
ᶅConfigureΫϥεͰѻ͏ʂ • ͜ΕͰ໊͚Մೳʂ • ֊ԽͰ͖Δʂ • ʮͲͷΫϥεʹଐ͢Δ͔ʯΛҙࣝͤͣʹ͓͚ Δʂ
ᶅConfigureΫϥεͰѻ͏ʂ • Ζ͏ͱࢥ͑ॻ͖͑ΒΕͪΌ͏ΑͶ… • ७ਮͳʮΞϓϦέʔγϣϯͷઃఆʯͱ ͍ࠞͬͯ͟ΔͷɺԿͱͳ͘ؾ࣋ͪѱ͍͔… • ෆ༻ҙʹ͘ͳΓ͕ͪɻ • ͪͳΈʹɺ
ίωώτʹ2,181ߦͷconstants.php͕͋Γ·͢
ͳΜ͔ྑ͍ͷͳ͍ͷ͔ͳʂ
ͭͬͯ͘Έͨʂ
None
ͳΜͰ͔͢ • ఆΫϥεΛϦονʹ͠·ͨ͠Έ͍ͨͳͷ • ΫϥεΛఆٛͯ͠ ʮ໊લʯͱʮʯͷએݴΛ͓ͯ͘͠ • ʮࣙॻʯ͕͍ͬͯΔใΛ ʮEnumʯͱͯ͠औΓग़ͤΔΑ͏ʹͳΔ •
PHPʹྻڍܕ͕ͳ͍͔ΒΦϨΦϨ࣮
ʮࣙॻʯͷ࡞Γํ • AbstractΫϥεΛܧঝͯ͠ • `ENUM_` prefixΛ࣋ͬͨconstΛએݴ͢Δ
ʮࣙॻʯͷ࡞Γํ
ʮࣙॻʯͷ͍ํ
ࣙॻͷԠ༻ᶃʮTypeʯ • Database\Type\EnumType • marshal/toPHPͰEnumΛѻ͑ΔΑ͏ʹ͢Δ
ࣙॻͷԠ༻ᶃʮEnumTypeʯ
ࣙॻͷԠ༻ᶃʮEnumTypeʯ
ࣙॻͷԠ༻ᶄʮValidationʯ • ʮରԠ͢Δࣙॻʹએݴ͞Ε͍ͯΔ͔ʯΛ νΣοΫ͢ΔValidationProvider • ʮ͜ͷྻ͜ͷࣙॻʯͱ͍͏ͷΛ ηοτ͓ͯ͘͠ͱɺະએݴͳΛڋ൱Ͱ͖Δ
ࣙॻͷԠ༻ᶄʮValidationʯ
ࣙॻͷԠ༻ᶄʮValidationʯ
·ͱΊ • લੈͷΞϓϦέʔγϣϯͰ ϚδοΫφϯόʔconstansͷരൃ͕ਏͯ͘ɾɾ • CakePHP3ͷPJ্ཱͪ͛࣌ʹ ݁ߏͳίετΛ͔͚ͭͭɺ͜ͷػߏͷ࡞Λߦ͍·ͨ͠ • ݁Ռɺॻ͖ํ͕౷Ұ͞Ε͍͍ͯײ͡ •
ͱΓΘ͚ɺCakePHP3ͷORMपΓ͔ͳΓڧԽ͞Ε͍ΔͷͰ Type/ValidationपΓͱͷ࿈ܞ͕ศར • ಠཱͨ͠ϨϙδτϦʹ͓͍ͨࣄͰϙʔλϏϦςΟ
͝ਗ਼ௌɺ ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ ࣮ͷͱ͔͍ͯ͠ͳ͍ͷͰ ͓ؾܰʹฉ͍͍ͯͩ͘͞