好孩子的 PHP 撰碼指南 范圣佑 PHPCON2018

PHP直译式、弱型别、动态转型等特质,虽然在开发上方便且有极大的弹性,但在中大型专案开发时,往往造成团队合作上的困难。因此,建立撰码规范、使用语法产生器、检查器及除错工具等,将可以有效提升开发效率及产能。甚至更进一步,若能适度的导入测试、源码分析、持续整合等工作流程,势必对团队合作更有助益。在这场分享里,将介绍如何整合开发生态系里的撰码工具,来协助开发者高效产出有品质的程式码。除了当个团队里的好孩子外,也能更有自信的交付,或是在夜晚睡个好觉 :)

展开查看详情

1. অ਎ৼጱ ̴̴ 砰嘨瞲‫ܖ‬ — 舰肜֚ (Shengyou Fan) PHPCon China 2018 2018/05/19 Photo by Steven Libralon on Unsplash

2.舰肜֚ shengyoufan@gmail.com — • ஑疝ᑀದ Ꮈ绗㹓 • Laravel 螇䁰 Ԇ旰Ո • JetBrains ದ蔩䌕疑 • 盏敟磧磪㰷꧊䌕疑 • Ԇᥝ䌕裾傶 PHP / Laravel 盅ᒒದ蔩

3.Laravel 螇䁰 — ෝ 2014 ଙ౮缏牧膌‫ێ‬ෝ൉‫׀‬磧䌕禂ጱ Laravel 璢懺现觎㺔๐率牐౼ᛗፓ獮傶 ྊ牧૪㶧ۗ蜱 30 樌䋊໊现մ禂獉蟂蝱ᤈ 硽䋊抓纷现ದ蔩疩獈牐 https://www.laravel-dojo.com/

4.Laravel ‫ݣ‬傀 — ౮缏ෝ 2013 ଙ牧匍磪 6900 ֖አ䜛‫ے‬ 獈牧ྯ์犋ਧ๗磪 3 㮆ᐒᗭၚ㵕牧۱ ೡ物PHP 犖磪 Day牏Laradiner牏 Laradebut牐

5.LaravelConf Taiwan — ෝ 2017 ଙ獺旰ᒫӞ䌵牧犡ଙ 7/8 (෭) 膐 旰ᒫԫ䌵牧毆懯䨝磪 450 ֖䨝唰㷢膏牐 犡ଙय़䨝Ԇ氂傶̿ℂమဩ‫ک‬䋿匍̀牧Ԇ 氂窩荠ॺկኞ眲羬牏ॺկ樄咳ದૣ牏介 手牏蟂ᗟ牏硳胼牏虻ਞ缛Ԇ氂牐

6.Ԇ氂य़姛 — ℂ犥ӥ皃㮆犋‫ݶ‬ጱ獥獈讨牧㶧ۗ樄咳ᘏ ṛ硳叨‫ڊ‬磪ߝ搡ጱ纷ୗ嘨物 • ൉‫܋‬樄咳硳ሲ • 翄Ӟ觓໒憒塅 • 戺֌樄咳ߝ搡 • 介手樄咳౮ຎ • ྍ詽ᛔ㵕ਠ౮ Photo by Austris Augusts on Unsplash

7.൉‫܋‬樄咳硳ሲ — 憽஑䌃纷ୗ嘨অ臺碻樌牫手手蝡犚ૡٍ物 • Boilerplate • Scaffolding • Code snippet Photo by Markus Spiske on Unsplash

8.碝䌕礯‫ڡ‬ত玕 — ౯㮉ฎெ讕‫ڡ‬ত玕Ӟ㮆 {໛礍} 䌕礯ጱ牫 $ composer create-project \ {vendor/package} 匍䁰ᄍ纈

9.‫ڡ‬ত玕ԏ盅 — ‫ڡ‬ত玕䌕礯盅䨝狶ጱԪ物 • 叨ኞ஠ᥝ戔ਧ䲆 $ cp .env.example .env • 䁆ᤈ瞲犤 匍䁰ᄍ纈 $ artisan key:generate $ artisan package:discover • ਞ蕕ॺկ $ composer require {vendor/package}

10.犥ॺկ吚च皈狶珸ত۱ # 戔ਧᛔ懪ॺկ㬵რ — # ~/.composer/config.json { "repositories": [ { "type": "...", "url": "..." Composer ጱӮኴ薪物 } • ಅ磪ጱ䌕礯᮷憙傶加缏ॺկ ] • ‫ݝ‬ᥝ‫ޞ‬藗ਙॺկ֖ᗝ疰瞟஑‫ک‬ } # 犥ᛔ懪ॺկ狶䌕礯珸ত۱ $ composer create-project {vendor/package}

11.翉ਧ Composer Ԫկ # 翉ਧԪկ — # ./composer.json { "scripts": { "post-root-package-install": [ "..." ], ࢥय़Ԫկ觊ࣳ物 "post-create-project-cmd": [ • Command Events "..." • Installer Events ], • Package Events "post-autoload-dump": [ • Plugin Events "..." https://getcomposer.org/doc/articles/scripts.md ] } } # 䔶‫ګ‬藉咳Ԫկ $ composer run-script {event}

12.䋿֢ᛔ૩ጱ珸ত۱ — 翎‫ݳ‬犥Ӥ秚‫ګ‬牧಑蝨ᒧ‫ݳ‬ᛔ૩禂率襑穩ጱ䌕 礯珸ত۱物 • ୌ缏㱪ਂ䓚牏൉Ի牏Ӥ tag牏Push ᛗ螐ᒒ • ਧ嬝ᛔ懪ॺկ㬵რ 匍䁰ᄍ纈 • አ Composer ୌ缏䌕礯 • 藉咳 Script Events

13.㷢ᘍ䌘虡 Laravel 䌕礯虻碘䄸礍䯤 — app/ PHP ܻত嘨 bootstrap/ config/ ‫ݢ‬㷢ᘍ‫ݱ‬໛礍ጱ䌕礯珸ত۱ெ讕狶牫 倵薩翕殷 database/ • Laravel物laravel/laravel public/ 翕ᒊ礬ፓ袅 • Symfony物symfony/website-skeleton ֵአᘏᒒ HTTP index.php 蝱獈讨 • Zend物zendframework/skeleton-application ֑๐瑊 • Yii物yiisoft/yii2-app-basic routes/ resources/ storage/ tests/ vendor/

14.PHP League skeleton — ‫ݢ‬犥㷢ᘍ PHP League ಅ纈塅ጱ䌕礯礍 䯤物 http://thephpleague.com https://github.com/thephpleague/skeleton

15.PHP Package development standard — ‫ݢ‬犥㷢ᘍ PHP Package development standard 㬵‫ڡ‬ত玕 Package 䌕礯物 http://php-pds.com/ https://github.com/php-pds

16.盠蝧叨ኞ஠ᥝ䲆礯 — ࣁ Laravel 愊牧磪 artisan 瞲犤‫ݢ‬犥盠蝧 叨ኞ䋿֢ಅ襑ጱ஠ᥝ䲆礯牐ᴻԧ樄䲆牏 ಑ਁጱ碻樌क़牧犖‫ݢ‬犥嘦狒䲆礯硯ᗝࣁ ྋ嘦ጱ᪠䕩ӥ牐 匍䁰ᄍ纈

17.盅‫ݣ‬叨ኞ瑊 — ૱ᶎӤ磪盄ग़禊盢觊犲ጱ盅‫ݣ‬叨ኞ瑊牧‫ݢ‬犥 盠蝧笕᪃च๜ CRUD 襑穩物 • https://laravelvoyager.com/ • https://www.getcraftable.com/ • https://backpackforlaravel.com/

18.䋿֢ᛔ૩ጱ叨ኞ瑊 — 礬硁ᛔ૩ጱ䋿率襑穩牧಑蝨դ嘨叨ኞ瑊牐 塅ֺ物 https://github.com/jonathantorres/construct 匍䁰ᄍ纈

19.盠蝧叨ኞդ嘨 — ‫ڥ‬አ‫ ݱ‬Editor/IDE 愊᮷磪դ嘨玟璸 (Code Snippet) ‫ۑ‬胼牧盠蝧叨ኞଉአ纷 ୗ嘨牐 匍䁰ᄍ纈

20.ᛔ懪纷ୗ嘨玟璸 — ࣁᛔ૩殼ಋጱ Editor/IDE 愊ᛔ懪纷ୗ嘨 玟璸牧ी‫ے‬叨ኞ纷ୗ嘨ጱ蝧ଶ޾仂੝಑ ਁ碻樌牐 臺讨碻樌戔ਧ牏藲໊ᛔ૩ጱૡٍ牧‫ݢ‬犥 य़य़൉‫܋‬ኞ叨‫ێ‬牐臺ๅग़碻樌ࣁๅ磪硳 ፅጱԪ眐Ӥ牐

21.翄Ӟ觓໒憒塅 — 獨Ո䌃ጱ纷ୗ嘨౯᮷፡犋聜䙼牧扗ெ讕旰牫 • ୌ缏砰嘨觓໒憒塅 • ֵአૡٍ㬵狕ྋ匍磪纷ୗ嘨 Photo by rawpixel on Unsplash

22. if () { 砰嘨觓໒ // ... — } elseif () { //... } else { //... } 纷ଧ㹓‫ݘ܉‬篷薹ጱ懵抷物螀֢奾ຎਠ獊 Ӟ䰬ጱ纷ୗ嘨牧ࡅ稭ߺӞ圵砰嘨觓໒牫 if() { //... } else if() { //... } else { //... }

23.PHP-FIG — ኧ PHP-FIG 蝢螂ጱ PHP Standards Recommendations 愊牧ٌӾ PSR-1 现 PSR-2 ‫ܨ‬朼䌘砰嘨觓໒狶‫ڊ‬憒塅牐 https://www.php-fig.org

24.PSR-1 — ‣ 䲆礯愊Ӟਧ‫ֵݝ‬አ <?php ޾ <?= 秂壆牐 ‣ 䲆礯Ӟਧࣁ PHP 纷ୗ嘨愊ֵአ䷱磪 BOM ጱ UTF-8 牐 ‣ Ӟ㮆䲆礯愊‫ݝ‬胼ࣁਯ‫ ޞ‬symbols (觊獨牏獢碍牏ଉ碍…缛) ౲ฎ叨ኞ PSR-1: Basic Coding Standard side-effects (ই物叨ኞ蜍‫ڊ‬牏虋ๅ .ini 戔ਧ…缛) Ӿ䢔Ӟ牧֕ PSR-1物च器砰嘨憒塅 犋‫ݢ‬獋ᘏ᮷狶牐 https://www.php-fig.org/psr/psr-1/ ‣ ޸‫ݷ‬绚樌 (Namespaces) ޾觊獨Ӟਧ螞ਝ PSR-4 ᛔ㵕斉獈秂伛牐 ‣ 觊獨޸‫ݷ‬ӞਧአḒਁय़䌃ጱ洨痷ୗ޸‫( ݷ‬StudlyCaps)牐 ‣ 觊獨ଉ碍޸‫ݷ‬Ӟਧአ獊य़䌃޾皈娄奲౮牐 ‣ 觊獨ොဩ޸‫ݷ‬Ӟਧአ洨痷ୗ޸‫( ݷ‬camelCase)牐

25. # Foo.php 塅ֺ物觊獨ොဩ޸‫ݷ‬ <?php — namespace Vendor\Model; class Foo { 觊獨ොဩӞਧ犥洨痷ୗ޸‫( ݷ‬camelCase) public function fooBar() { // method body } }

26. ‣ 纷ୗ嘨Ӟਧ螞ਝ PSR-1牐 PSR-2 ‣ 纷ୗ嘨Ӟਧ犥 4 㮆绚໒牧ᘒ犋ฎአ tabs 吚֢婘矎牐 — ‣ 犋‫ݢ‬Ꮭ௔憒ਧ㻌ᤈ裾ଶ牪㻌ᤈਁ‫ز‬碍Ӟਧ褖‫ࣁګ‬磧ग़ 120 ਁ‫ૢز‬ ‫ݦ‬牪ྯᤈਁ‫ز‬碍Ӟਧࣁ 80 ਁ‫ز‬౲ๅ੝牐 ‣ ࣁ namespace ਯ‫ޞ‬盅Ӟਧ绚Ӟᤈ牧ࣁ獊蟂 use ਯ‫ޞ‬ਠ盅Ӟਧ绚Ӟ ᤈ牐 PSR-2: Coding Style Guide ‣ 觊獨ਯ‫ޞ‬ጱૢय़ೡ蒈Ӟਧ硯ࣁ觊獨‫ݷ‬圸ጱӥӞᤈ牧‫ݦ‬य़ೡ蒈Ӟਧ硯ࣁ PSR-2物砰嘨觓໒瞲‫ܖ‬ 觊獨獉਻磧盅㻌加Ӟᤈ牐 https://www.php-fig.org/psr/psr-2/ ‣ ොဩਯ‫ޞ‬ጱૢय़ೡ蒈Ӟਧ硯ࣁොဩ‫ݷ‬圸ጱӥӞᤈ牧‫ݦ‬य़ೡ蒈Ӟਧ硯ࣁ ොဩ獉਻磧盅㻌加Ӟᤈ牐 ‣ ಅ磪痀௔޾ොဩਯ‫ޞ‬Ӟਧ‫ے‬Ӥ‫ݢ‬憎௔ (Visibility)牏abstract ޾ final Ӟਧ䌃ࣁ‫ݢ‬憎௔ԏ獮牏static Ӟਧ䌃ࣁ‫ݢ‬憎௔ԏ盅牐 ‣ 矒‫ګ‬奾䯤ጱ橕棎ਁԏ盅Ӟਧ绚Ӟ໒牪֕ොဩ޾獢碍޷‫ݞ‬盅犋‫ݢ‬绚໒牐 ‣ 矒‫ګ‬奾䯤ጱૢय़ೡ蒈Ӟਧ硯ࣁ‫ݶ‬Ӟᤈ牧‫ݦ‬य़ೡ蒈Ӟਧ硯ࣁ獉਻磧盅㻌 加Ӟᤈ牐 ‣ 矒‫ګ‬奾䯤ጱૢੜೡ蒈ԏ盅犋‫ݢ‬绚໒牧‫ݦ‬ੜೡ蒈ԏ獮犖犋‫ݢ‬绚໒牐

27. <?php 塅ֺ物PSR-2 — # 矒‫ګ‬奾䯤 if ($expr1) { // if body else ޾ elseif ᥝ޾ӤӞ㮆ጱ‫ݦ‬य़ೡ蒈硯ࣁ‫ݶ‬Ӟ } elseif ($expr2) { ᤈ牐傶ԧ虏ಅ磪矒‫ګ‬奾䯤橕棎ਁ፡蚏㬵ฎӞ㮆 // elseif body 㻌ਁ牧Ӟਧֵአ橕棎ਁ elseif ᘒ犋ฎ else if牐 } else { // else body; } # 蝅瑹 for ($i = 0; $i < 10; $i++) { // for body } for 现 foreach 檔蝄承ဩ፡蚏㬵奾䯤ই‫ݦ‬牧ဳ఺ ੜೡ蒈牏绚໒޾य़ೡ蒈ጱ֖ᗝ牐 foreach ($iterable as $key => $value) { // foreach body }

28.PSR-12 — PSR-12: Extended Coding Style Guide PSR-12物皤֐砰嘨觓໒瞲‫)ܖ‬ 朼䌘 PHP 7 碝承ဩಅ൉‫ڊ‬ጱ砰嘨觓໒憒 塅牧ፓ獮ྋࣁ疛໐褩ྦྷ牐

29.EditorConfig — 䒻ۗ樄咳ᘏࣁ犋‫ݶ‬ጱ Editor/IDE ԏ樌䟖 አӞ膌ጱ砰嘨觓໒牐