VOOZH about

URL: https://qiita.com/tadsan/items/c47eb327684530721e8a

โ‡ฑ PHP: Clean Code (clean-code-php) ่œœๆŸ‘่–ฌ #PHP - Qiita


๐Ÿ‘ Image
216

Go to list of users who liked

221

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

@tadsan(ๅœ’ๅณถ ใฌใฌ)

PHP: Clean Code (clean-code-php) ่œœๆŸ‘่–ฌ

216
Last updated at Posted at 2017-09-02

ใ‚‚ใใ˜

  1. ใฏใ˜ใ‚ใซ
  2. ๅค‰ๆ•ฐ
  3. ้–ขๆ•ฐ
  4. ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใจใƒ‡ใƒผใ‚ฟๆง‹้€ 
  5. ใ‚ฏใƒฉใ‚น
    1. S: ๅ˜ไธ€่ฒฌไปปใฎๅŽŸๅ‰‡ (SRP)
    2. O: ใ‚ชใƒผใƒ—ใƒณ/ใ‚ฏใƒญใƒผใ‚บใƒ‰ใฎๅŽŸๅ‰‡ (OCP)
    3. L: ใƒชใ‚นใ‚ณใƒ•ใฎ็ฝฎๆ›ๅŽŸๅ‰‡ (LSP)
    4. I: ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใ‚คใ‚นๅˆ†้›ขใฎๅŽŸๅ‰‡ (ISP)
    5. D: ไพๅญ˜้€†่ปขใฎๆณ•ๅ‰‡ (DIP)
  6. ๅŒใ˜ใ“ใจใ‚’็นฐใ‚Š่ฟ”ใ™ใช (DRY)

ใฏใ˜ใ‚ใซ

ใ“ใฎ่จ˜ไบ‹ใฏRobert C. Martinใฎๆœฌใ€ŒClean Codeใ€ใฎใ‚ฝใƒ•ใƒˆใ‚ฆใ‚งใ‚ขใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒชใƒณใ‚ฐใฎๆณ•ๅ‰‡ใ‚’PHPใซ้ฉๅˆใ•ใ›ใŸใ‚‚ใฎใงใ™ใ€‚ใ“ใ‚Œใฏใ‚นใ‚ฟใ‚คใƒซใ‚ฌใ‚คใƒ‰ใงใฏใ‚ใ‚Šใพใ›ใ‚“ใ€‚่ชญใฟใ‚„ใ™ใใ€ๅ†ๅˆฉ็”จใ—ใ‚„ใ™ใใ€ใใ—ใฆใƒชใƒ•ใ‚กใ‚ฏใ‚ฟใƒชใƒณใ‚ฐใ—ใ‚„ใ™ใ„PHPใ‚ณใƒผใƒ‰ใ‚’ๆ›ธใใŸใ‚ใฎใ‚ฌใ‚คใƒ‰ใงใ™ใ€‚

ใ“ใ“ใงๆŒ™ใ’ใ‚‰ใ‚Œใ‚‹ใ™ในใฆใฎๅŽŸๅ‰‡ใฏๅŽณๅฏ†ใซๅฎˆใ‚‰ใชใใฆใฏใ„ใ‘ใชใ„ใ‚ใ‘ใงใฏใชใใ€ๅฐ‘ใ—ๅฎˆใ‚‰ใชใ‹ใฃใŸใจใ“ใ‚ใงไธ€่ˆฌใซใฏ่จฑๅฎนใ•ใ‚Œใพใ™ใ€‚ใ‚ใใพใงใ‚ฌใ‚คใƒ‰ใƒฉใ‚คใƒณใงใ™ใŒใ€Clean Codeใฎ่‘—่€…ใŸใกใŒใฟใช้•ทๅนดใซๆธกใฃใฆ็ตŒ้จ“ใ—ใฆใใŸใ“ใจใงใ™ใ€‚

ใ“ใฎ่จ˜ไบ‹(clean-code-php)ใฏclean-code-javascriptใซ่งฆ็™บใ•ใ‚Œใพใ—ใŸใ€‚

็ฟป่จณใซใคใ„ใฆ

ใ“ใฎ่จ˜ไบ‹ใฏclean-code-phpใ‚’ใ†ใ•ใฟใ‘ใ‚“ใŸ@tadsanใŒ้›‘ใซๆ—ฅๆœฌ่ชžใซ่จณใ—ใŸใ‚‚ใฎใงใ™ใ€‚ (ๅ…ƒใฎใƒชใƒ“ใ‚ธใƒงใƒณใฏ1db3e53ใงใ™ใ€‚)

่จ˜ไบ‹ใฎ็ฟป่จณไฝœๆฅญใฏ2017ๅนด9ๆœˆ3ๆ—ฅๆ™‚็‚นใงๆœชๅฎŒไบ†ใงใ™ใ€‚ๅŠๅˆ†็จ‹ๅบฆใพใง่จณใ—ใพใ—ใŸใ€‚
ใ‚„ใ‚‹ๆฐ—ใŒๆฎ‹ใฃใฆใŸใ‚‰ใ€ๆ˜Žๆ—ฅใ‹ๅพŒๆ—ฅใซใฏใ€ใพใŸ็ถšใใ‚’่จณใ—ใพใ™ใ€‚

ๅŽŸๆ–‡ใฎ่‘—ไฝœๆจฉ่กจ่จ˜ใŠใ‚ˆใณใƒฉใ‚คใ‚ปใƒณใ‚น่จฑ่ซพใฏไปฅไธ‹ใฎๆ–‡ใงใ™ใ€‚

The MIT License (MIT)

Copyright (c) 2016 Ryan McDermott

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE

่จณ่€…ใฏๅŽŸ่‘—ใ€ŒClean Codeใ€ใŠใ‚ˆใณ็ฟป่จณๆ›ธใฏๆœช่ชญใงใ™ใ€่จณ่ชžใฏWebใงไธ€่ˆฌใซ่ฆ‹ใ‚‰ใ‚Œใ‚‹ใ‚‚ใฎใ€ใ‚ใ‚‹ใ„ใฏ็‹ฌ่‡ชใงๅฎ›ใฆใŸใ‚‚ใฎใงใ™ใ€‚

ใพใŸใ€ใ“ใ‚Œใฏ้ฃฝใใพใง็ฟป่จณ่จ˜ไบ‹ใงใ™ใ€‚็›ด่จณใงๆ„ๅ‘ณใฎใจใ‚Šใซใใ„ใ‚‚ใฎใฏๆ–‡ใ‚’่ฃœใฃใฆๆ„่จณใ—ใฆใ‚ใ‚Šใพใ™(ๆฐ—ใซใชใ‚‹ใฒใจใฏๅŽŸๆ–‡ใ‚’่ชญใ‚“ใงใใ ใ•ใ„)ใ€‚ใŸใ ใ—ๆŠ•็จฟ่€…(@tadsan)ใฎ่ฆ‹่งฃใจใฏ็•ฐใชใ‚‹็‚นใซใคใ„ใฆใฏใ€ๅฏ่ƒฝใช้™ใ‚Š็ง่ฆ‹ใ‚’ๅ…ฅใ‚Œใšใ€ๅŽŸๆ–‡ใฎๆ„ๅ›ณใ‚’ๅฏ่ƒฝใช้™ใ‚Šๆฑฒใฟใพใ—ใŸใ€‚

ๅค‰ๆ•ฐ

ๆ„ๅ‘ณใŒใ‚ใฃใฆ็™บ้Ÿณใงใใ‚‹ๅค‰ๆ•ฐๅใซใ™ใ‚‹

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

$ymdstr = $moment->format('y-m-d');

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

$currentDate = $moment->format('y-m-d');

ๅŒใ˜ๅž‹ใฎๅค‰ๆ•ฐใซใฏๅŒใ˜ๅ˜่ชžใ‚’ๅฎ›ใฆใ‚‹

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

getUserInfo();
getClientData();
getCustomerRecord();

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

getUser();

ๆคœ็ดขใ—ใ‚„ใ™ใ„ๅค‰ๆ•ฐๅใซใ™ใ‚‹

ใ‚ใŸใ—ใŸใกใฏใ‚ณใƒผใƒ‰ใ‚’ๆ›ธใ็ถšใ‘ใ‚‹้™ใ‚Šใ€ๆ›ธใไปฅไธŠใซๅ‘จๅ›ฒใฎใ‚ณใƒผใƒ‰ใ‚’่ชญใ‚€ใ“ใจใซใชใ‚Šใพใ™ใ€‚ใชใฎใงใ€ใ‚ณใƒผใƒ‰ใฏ่ชญใฟใ‚„ใ™ใใ€ๆคœ็ดขใ—ใ‚„ใ™ใ„ใ‚ˆใ†ใซๆ›ธใใ“ใจใŒๅคงไบ‹ใงใ™ใ€‚ๆ„ๅ‘ณใ‚’ใคใ‹ใฟใซใใ„ๅค‰ๆ•ฐๅใฏใƒ—ใƒญใ‚ฐใƒฉใƒ ใ‚’็†่งฃใ™ใ‚‹ไธŠใงๆ”ฏ้šœใจใชใ‚Šใ€ใฎใกใฎใก่ชญ่€…ใ‚’ๅ›ฐใ‚‰ใ›ใ‚‹ใ“ใจใซใชใ‚Šใพใ™ใ€‚ใใ‚Œใใ‚Œใ‚‚ใ€ๅค‰ๆ•ฐใซใฏๆคœ็ดขใ—ใ‚„ใ™ใ„ๅๅ‰ใ‚’ใคใ‘ใฆใใ ใ•ใ„ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

// 86400ใฃใฆไฝ•โ€ฆ??
addExpireAt(86400);

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

// constใ‚ญใƒผใƒฏใƒผใƒ‰ใงใ€ๅ…จ้ƒจๅคงๆ–‡ๅญ—ใฎๅฎšๆ•ฐๅใงๅฎš็พฉใ™ใ‚‹
interface DateGlobal {
 const SECONDS_IN_A_DAY = 86400;
}

addExpireAt(DateGlobal::SECONDS_IN_A_DAY);

่ชฌๆ˜Ž็š„ใชๅค‰ๆ•ฐๅใซใ™ใ‚‹

๐Ÿ‘ :no_good_tone1:
Bad: (ใ ใ‚)

$address = 'One Infinite Loop, Cupertino 95014';
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
preg_match($cityZipCodeRegex, $address, $matches);

saveCityZipCode($matches[1], $matches[2]);

๐Ÿ‘ :thinking:
Not bad: (ๆ‚ชใใฏใชใ„)

ใ•ใฃใใ‚ˆใ‚Š่‰ฏใ„ใงใ™ใŒใ€ใพใ ๆญฃ่ฆ่กจ็พใฎใƒžใƒƒใƒ้ †ๅบใซๅผทใไพๅญ˜ใŒใ‚ใ‚Šใพใ™ใ€‚

$address = 'One Infinite Loop, Cupertino 95014';
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
preg_match($cityZipCodeRegex, $address, $matches);

list(, $city, $zipCode) = $matchers;
saveCityZipCode($city, $zipCode);

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

ๅๅ‰ไป˜ใใ‚ตใƒ–ใƒ‘ใ‚ฟใƒผใƒณใซใ™ใ‚‹ใ“ใจใงใƒžใƒƒใƒ้ †ๅบใซๅฝฑ้Ÿฟใ•ใ‚ŒใชใใชใฃใŸใฎใงใ€ๆญฃ่ฆ่กจ็พใฎใธใฎไพๅญ˜ใŒๆธ›ใ‚Šใพใ—ใŸใ€‚

$address = 'One Infinite Loop, Cupertino 95014';
$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(?<city>.+?)\s*(?<zipCode>\d{5})?$/';
preg_match($cityZipCodeRegex, $address, $matches);

saveCityZipCode($matches['city'], $matches['zipCode']);

ใƒกใƒณใ‚ฟใƒซใƒžใƒƒใƒ”ใƒณใ‚ฐใ‚’้ฟใ‘ใ‚‹

่งฃ่ชญใŒๅฟ…่ฆใซใชใ‚‹ใ‚ˆใ†ใชใ€็‹ฌ็‰นใฎๆ„ๅ‘ณใ‚’ๆŒใฃใŸๅค‰ๆ•ฐๅใ‚’ใคใ‘ใชใ„ใงใใ ใ•ใ„ใ€‚ๅค‰ๆ•ฐใฎๆ„ๅ‘ณใŒใฏใฃใใ‚Šใจๆ˜Ž็ขบใงใ‚ใ‚‹ใ“ใจใฏใ€ๆš—้ป™็š„ใงใ‚ใ‚‹ใ‚ˆใ‚Šใ‚‚่‰ฏใ„ใ“ใจใงใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

$l = ['Austin', 'New York', 'San Francisco'];

for ($i = 0; $i < count($l); $i++) {
 $li = $l[$i];
 doStuff();
 doSomeOtherStuff();
 // ...
 // ...
 // ...
 // ใพใŸ `$li` ใŒๅ‡บใฆใใŸใ‘ใฉโ€ฆ ใ“ใ‚Œใฏไฝ•๏ผŸ
 dispatch($li);
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

$locations = ['Austin', 'New York', 'San Francisco'];

foreach ($locations as $location) {
 doStuff();
 doSomeOtherStuff();
 // ...
 // ...
 // ...
 dispatch($location);
});

ๅๅ‰ใซใ‚ณใƒณใƒ†ใ‚ญใ‚นใƒˆใ‚’ใใฃใคใ‘ใชใ„

ใ‚ฏใƒฉใ‚น/ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎๅๅ‰ใซๆ„ๅ‘ณใŒใ‚ใ‚‹ใชใ‚‰ใ€ใใ‚Œใ‚’ๅค‰ๆ•ฐๅใซ็นฐใ‚Š่ฟ”ใ•ใชใ„ใงใใ ใ•ใ„ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class Car
{
 public $carMake;
 public $carModel;
 public $carColor;

 //...
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class Car
{
 public $make;
 public $model;
 public $color;

 //...
}

็Ÿญ็ตก่ฉ•ไพกใงใฏใชใใ€ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆๅผ•ๆ•ฐใซใ™ใ‚‹

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function createMicrobrewery($name = null) {
 $breweryName = $name ?: 'Hipster Brew Co.';
 // ...
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

function createMicrobrewery($breweryName = 'Hipster Brew Co.') {
 // ...
}

้–ขๆ•ฐ

ๅผ•ๆ•ฐใฏ2ใคไปฅไธ‹ใŒ็†ๆƒณ

ใƒ‘ใƒฉใƒกใƒผใ‚ฟใฎๆ•ฐใ‚’ๅˆถ้™ใ™ใ‚‹ใ“ใจใฏใ€้–ขๆ•ฐใ‚’ใƒ†ใ‚นใƒˆใ—ใ‚„ใ™ใใ™ใ‚‹ใŸใ‚ใซ้‡่ฆใงใ™ใ€‚3ใคไปฅไธŠใซใชใ‚‹ใจ็ต„ใฟๅˆใ›็ˆ†็™บใ‚’ๅผ•ใ่ตทใ“ใ—ใพใ™ใ€‚ใใ†ใชใ‚‹ใจใ€ในใคในใคใฎๅ€คใ‚’ๆธกใ—ใฆใ€ใ„ใ‚ใ„ใ‚ใชใ‚ฑใƒผใ‚นใ‚’ใƒ†ใ‚นใƒˆใ—ใชใ‘ใ‚Œใฐใ„ใ‘ใชใใชใ‚Šใพใ™ใ€‚

ๅผ•ๆ•ฐใชใ—ใฏ็†ๆƒณ็š„ใงใ™ใ€‚1ใคใฎๅ ดๅˆใจ2ใคใฎๅ ดๅˆใฏ็‰นใซๅ•้กŒใ‚ใ‚Šใพใ›ใ‚“ใ€‚3ใคใฏ้ฟใ‘ใฆใใ ใ•ใ„ใ€‚4ๅ€‹ไปฅไธŠใชใ‚‰ใ€ใใ‚Œใฏ้€ฃ็ตใ™ใ‚‹ในใใงใ™ใ€‚ๅผ•ๆ•ฐใŒ2ใคไปฅไธŠใฏใ€ใŸใ„ใฆใ„ไธ€ใคใฎ้–ขๆ•ฐใซๅคšใใฎๅ‡ฆ็†ใ‚’ๆŒใกใ™ใŽใงใ™ใ€‚ใใ†ใงใชใ‘ใ‚Œใฐใ€้ซ˜็ดšใชใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใƒกใ‚ฝใƒƒใƒ‰ใฏใปใจใ‚“ใฉใฎๅ ดๅˆใซๅผ•ๆ•ฐใ‚’ใฒใจใคใ ใ‘ๅ–ใ‚Œใฐๅๅˆ†ใงใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function createMenu($title, $body, $buttonText, $cancellable) {
 // ...
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class MenuConfig
{
 public $title;
 public $body;
 public $buttonText;
 public $cancellable = false;
}

$config = new MenuConfig();
$config->title = 'Foo';
$config->body = 'Bar';
$config->buttonText = 'Baz';
$config->cancellable = true;

function createMenu(MenuConfig $config) {
 // ...
}

้–ขๆ•ฐใฏใฒใจใคใฎใ“ใจใ ใ‘ใ‚’ใ‚„ใ‚‹

ใ“ใ‚Œใฏใ‚ฝใƒ•ใƒˆใ‚ฆใ‚งใ‚ขใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒชใƒณใ‚ฐใฎใชใ‹ใงใ‚‚ใ€ใ‚‚ใฃใจใ‚‚ๅคงไบ‹ใชใƒซใƒผใƒซใงใ™ใ€‚้–ขๆ•ฐใŒๅคšใใฎใ“ใจใ‚’ใ—ใ‚ˆใ†ใจใ™ใ‚‹ใจใ€ๅฎŸ่ฃ…ใ€ใƒ†ใ‚นใƒˆใ€ใใ—ใฆๆŒ™ๅ‹•ใ‚’ๆŽจๆธฌใ™ใ‚‹ใ“ใจใฏ้›ฃใ—ใใชใ‚Šใพใ™ใ€‚้–ขๆ•ฐใ‚’ใฒใจใคใฎใ‚ขใ‚ฏใ‚ทใƒงใƒณใ ใ‘ใซ้š”้›ขใงใใ‚Œใฐใƒชใƒ•ใ‚กใ‚ฏใ‚ฟใƒชใƒณใ‚ฐใฏใšใฃใจ็ฐกๅ˜ใซใชใ‚Šใ€ใ‚ณใƒผใƒ‰ใฏๆด—็ทดใ•ใ‚Œใพใ™ใ€‚

ใ“ใฎใ‚ฌใ‚คใƒ‰ใ‹ใ‚‰ใปใ‹ใฎ้ …็›ฎใ‚’ๅ‰Š้™คใ—ใŸใจใ—ใฆใ‚‚ใ€ใ“ใ‚Œใ ใ‘ใฏ่ฆšใˆใฆใŠใ„ใฆใใ ใ•ใ„ใ€‚ใใ‚Œใ ใ‘ใงใ€ใปใ‹ใฎๅคงๅ‹ขใฎ้–‹็™บ่€…ใฎไธ€ๆญฉๅ…ˆใ‚’่กŒใใ“ใจใŒใงใใพใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function emailClients($clients) {
 foreach ($clients as $client) {
 $clientRecord = $db->find($client);
 if ($clientRecord->isActive()) {
 email($client);
 }
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

function emailClients($clients) {
 $activeClients = activeClients($clients);
 array_walk($activeClients, 'email');
}

function activeClients($clients) {
 return array_filter($clients, 'isClientActive');
}

function isClientActive($client) {
 $clientRecord = $db->find($client);
 return $clientRecord->isActive();
}

ไฝ•ใ‚’ใ™ใ‚‹ใฎ้–ขๆ•ฐใชใฎใ‹ใ‚ใ‹ใ‚‹ๅๅ‰ใซใ™ใ‚‹

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class Email
{
 //...

 public function handle()
 {
 mail($this->to, $this->subject, $this->body);
 }
}

$message = new Email(...);
// ใƒกใƒƒใ‚ปใƒผใ‚ธใฎใƒใƒณใƒ‰ใƒซใฃใฆใ€ใ“ใ‚Œใฏไฝ•ใ‚„ใฃใฆใ‚‹ใฎโ€ฆ? ใƒ•ใ‚กใ‚คใƒซใซๆ›ธใ่พผใ‚€ใฎ?
$message->handle();

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class Email 
{
 //...

 public function send()
 {
 mail($this->to, $this->subject, $this->body);
 }
}

$message = new Email(...);
// ้€ไฟกใ™ใ‚‹ใ€‚ไธ€็›ฎ็žญ็„ถใ€‚
$message->send();

้–ขๆ•ฐใฎๆŠฝ่ฑกๅŒ–ใƒฌใƒ™ใƒซใ‚’ๅŒใ˜ใซใ™ใ‚‹

ๆŠฝ่ฑกๅŒ–ใฎใƒฌใƒ™ใƒซใŒ็•ฐใชใ‚‹ใชใ‚‰ใ€ใใฎ้–ขๆ•ฐใฏๅคšใใฎใ“ใจใ‚’ใ‚„ใ‚Šใ™ใŽใงใ™ใ€‚้–ขๆ•ฐใ‚’ๅˆ†ๅ‰ฒใ™ใ‚‹ใ“ใจใงๅ†ๅˆฉ็”จๆ€งใŒๅ‘ไธŠใ—ใ€ใƒ†ใ‚นใƒˆใ—ใ‚„ใ™ใใชใ‚Šใพใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function parseBetterJSAlternative($code)
{
 $regexes = [
 // ...
 ];
 
 $statements = split(' ', $code);
 $tokens = [];
 foreach ($regexes as $regex) {
 foreach ($statements as $statement) {
 // ...
 }
 }
 
 $ast = [];
 foreach ($tokens as $token) {
 // ๅญ—ๅฅ่งฃๆž...
 }
 
 foreach ($ast as $node) {
 // ๆง‹ๆ–‡่งฃๆž...
 }
}

๐Ÿ‘ :no_good_tone1:
๐Ÿ‘ :no_good_tone1:
Bad too (ใพใ ใ ใ‚)

ๆฉŸ่ƒฝใ‚’ใ„ใใคใ‹ๅค–ใซๅ‡บใ—ใพใ—ใŸใŒใ€ใใ‚Œใงใ‚‚ใพใ parseBetterJSAlternative()ใฏ่ค‡้›‘ใงใƒ†ใ‚นใƒˆใ—ใซใใ„ใงใ™ใ€‚

function tokenize($code)
{
 $regexes = [
 // ...
 ];
 
 $statements = split(' ', $code);
 $tokens = [];
 foreach ($regexes as $regex) {
 foreach ($statements as $statement) {
 $tokens[] = /* ... */;
 }
 }
 
 return $tokens;
}

function lexer($tokens)
{
 $ast = [];
 foreach ($tokens as $token) {
 $ast[] = /* ... */;
 }
 
 return $ast;
}

function parseBetterJSAlternative($code)
{
 $tokens = tokenize($code);
 $ast = lexer($tokens);
 foreach ($ast as $node) {
 // parse...
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

ๆœ€้ซ˜ใฎ่งฃๆฑบๆ–นๆณ•ใฏใ€ไพๅญ˜ใ‚’parseBetterJSAlternative()ใ‹ใ‚‰ๅ–ใ‚ŠๅŽปใ‚‹ใ“ใจใงใ™ใ€‚

class Tokenizer
{
 public function tokenize($code)
 {
 $regexes = [
 // ...
 ];

 $statements = split(' ', $code);
 $tokens = [];
 foreach ($regexes as $regex) {
 foreach ($statements as $statement) {
 $tokens[] = /* ... */;
 }
 }

 return $tokens;
 }
}
class Lexer
{
 public function lexify($tokens)
 {
 $ast = [];
 foreach ($tokens as $token) {
 $ast[] = /* ... */;
 }

 return $ast;
 }
}
class BetterJSAlternative
{
 private $tokenizer;
 private $lexer;

 public function __construct(Tokenizer $tokenizer, Lexer $lexer)
 {
 $this->tokenizer = $tokenizer;
 $this->lexer = $lexer;
 }

 public function parse($code)
 {
 $tokens = $this->tokenizer->tokenize($code);
 $ast = $this->lexer->lexify($tokens);
 foreach ($ast as $node) {
 // parse...
 }
 }
}

็งใŸใกใฏไพๅญ˜ใ‚’ใƒขใƒƒใ‚ฏใงใใ‚‹ใ‚ˆใ†ใซใชใฃใŸใฎใงใ€BetterJSAlternative::parse()ใ ใ‘ใ‚’ใƒ†ใ‚นใƒˆใงใใ‚‹ใ‚ˆใ†ใซใชใ‚Šใพใ—ใŸใ€‚

ใƒ•ใƒฉใ‚ฐๅผ•ๆ•ฐใฏๅˆฉ็”จใ—ใชใ„

ๅผ•ๆ•ฐใซใƒ•ใƒฉใ‚ฐใ‚’ๅ—ใ‘ไป˜ใ‘ใ‚‹ใ“ใจใฏใ€้–ขๆ•ฐใŒ่ค‡ๆ•ฐใฎๅ‡ฆ็†ใ‚’ๆŒใคใ“ใจใ‚’็คบใ—ใพใ™ใ€‚้–ขๆ•ฐใฏใฒใจใคใฎไป•ไบ‹ใ ใ‘ใซๅฐ‚ๅฟตใ™ในใใงใ™ใ€‚ใƒ•ใƒฉใ‚ฐใซใ‚ˆใฃใฆ็•ฐใชใ‚‹ๅ‡ฆ็†ใ‚’ใ™ใ‚‹ใชใ‚‰ใฐใ€ใใ‚Œใฏๅˆฅใฎ้–ขๆ•ฐใซใ—ใฆใใ ใ•ใ„ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function createFile($name, $temp = false) {
 if ($temp) {
 touch('./temp/'.$name);
 } else {
 touch($name);
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

function createFile($name) {
 touch($name);
}

function createTempFile($name) {
 touch('./temp/'.$name);
}

ๅ‰ฏไฝœ็”จใ‚’้ฟใ‘ใ‚‹

ใ‚ใ‚‹้–ขๆ•ฐใŒใ€Œๅผ•ๆ•ฐใ‚’ๅ—ใ‘ๅ–ใฃใฆๅˆฅใฎๅ€คใ‚’่ฟ”ใ™ใ€ไปฅๅค–ใฎๅ‡ฆ็†ใ‚’ใ™ใ‚‹ใชใ‚‰ใฐใ€ใใ‚ŒใŒๅ‰ฏไฝœ็”จใงใ™ใ€‚ๅ‰ฏไฝœ็”จใจใฏใ€ใƒ•ใ‚กใ‚คใƒซๆ›ธใ่พผใฟใ€ใ‚ฐใƒญใƒผใƒใƒซๅค‰ๆ•ฐใฎๆ“ไฝœใ€ใใ—ใฆใƒˆใƒฉใƒ–ใƒซใงใ‚ใชใŸใฎๅ…จ่ฒก็”ฃใ‚’ใฉใ“ใ‹ใฎ่ชฐใ‹ใซ้€้‡‘ใ™ใ‚‹ใ“ใจใ€ใชใฉใงใ™ใ€‚

ใตใคใ†ใ€ใฒใจใคใฎใƒ—ใƒญใ‚ฐใƒฉใƒ ใซใŠใ„ใฆใ‚‚ใ„ใใคใ‚‚ใฎๅ‰ฏไฝœ็”จใ‚’ๆŒใŸใ›ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚ๅ‰ใฎไพ‹ใฎใ‚ˆใ†ใซใ€ใƒ•ใ‚กใ‚คใƒซใซๆ›ธใ่พผใ‚€ๅฟ…่ฆใŒใ‚ใ‚‹ใฎใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚ใ‚ใชใŸใŒใ‚„ใ‚ŠใŸใ„ใ“ใจใฏใ€ใใ‚Œใ‚’ใ™ใ‚‹ใจใ“ใ‚ใ‚’้›†็ด„ใ™ใ‚‹ใ“ใจใงใ™ใ€‚ใใฎใƒ•ใ‚กใ‚คใƒซใซๆ›ธใ่พผใ‚€้–ขๆ•ฐใ‚„ใ‚ฏใƒฉใ‚นใ‚’ๅˆ†ๆ•ฃใ•ใ›ใชใ„ใงใใ ใ•ใ„ใ€‚ใฒใจใคใฎใ‚ตใƒผใƒ“ใ‚นใซใใ‚Œใ‚’ใ•ใ›ใพใ™ใ€‚ๅ”ฏไธ€ใใ‚Œใ ใ‘ใงใ™ใ€‚

ใ€Œๆง‹้€ ไฝ“ใงใฏใชใ„ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎ้–“ใง็Šถๆ…‹ใ‚’ๅ…ฑๆœ‰ใ™ใ‚‹ใ€ใ€Œใฉใ“ใ‹ใ‚‰ใ‚‚ๅค‰ๆ›ดใ•ใ‚Œใ‚‹ใŠใใ‚ŒใŒใ‚ใ‚‹ใ‚‚ใฎใซใƒŸใƒฅใƒผใ‚ฟใƒ–ใƒซใชใƒ‡ใƒผใ‚ฟๅž‹ใ‚’ๅˆฉ็”จใ™ใ‚‹ใ€ใ€ใใ—ใฆใ€Œๅ‰ฏไฝœ็”จใฎ่ตทใ“ใ‚‹ๅ‡ฆ็†ใ‚’ไธ€็ฎ‡ๆ‰€ใซ้›†็ด„ใ—ใชใ„ใ€ใจใ„ใฃใŸ่ชฐใ‚‚ใŒ้™ฅใ‚‹็ฝ ใ‚’้ฟใ‘ใ‚‹ใ“ใจใŒใงใใ‚Œใฐใ€ใ‚ใชใŸใฏๅคงๅคšๆ•ฐใฎใƒ—ใƒญใ‚ฐใƒฉใƒžใƒผใ‚ˆใ‚Šใ‚‚ๅนธใ›ใซใชใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

// ใ‚ฐใƒญใƒผใƒใƒซๅค‰ๆ•ฐใŒใ“ใ‚Œใ‚‰ใฎ้–ขๆ•ฐใ‹ใ‚‰ๅ‚็…งใ•ใ‚Œใ‚‹
// ใ‚‚ใ—ๅŒใ˜ๅค‰ๆ•ฐๅใ‚’ไฝฟ็”จใ—ใ‚ˆใ†ใจใ™ใ‚‹ๅˆฅใฎ้–ขๆ•ฐใŒใ‚ใฃใŸใ‚‰ใ€ใ“ใฎๅ‡ฆ็†ใฏๅฃŠใ‚Œใพใ™
$name = 'Ryan McDermott';

function splitIntoFirstAndLastName() {
 global $name;

 $name = preg_split('/ /', $name);
}

splitIntoFirstAndLastName();

var_dump($name); // ['Ryan', 'McDermott'];

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

$name = 'Ryan McDermott';

function splitIntoFirstAndLastName($name) {
 return preg_split('/ /', $name);
}

$name = 'Ryan McDermott';
$newName = splitIntoFirstAndLastName($name);

var_dump($name); // 'Ryan McDermott';
var_dump($newName); // ['Ryan', 'McDermott'];

ใ‚ฏใƒญใƒผใƒใƒซ้–ขๆ•ฐใฏๆ›ธใ‹ใชใ„

ใ‚ใชใŸใฎใƒฉใ‚คใƒ–ใƒฉใƒชใจๅˆฅใฎใƒฉใ‚คใƒ–ใƒฉใƒชใจ้–ขๆ•ฐๅใŒ่ก็ชใ™ใ‚‹ๅฏ่ƒฝๆ€งใŒใ‚ใ‚‹ใฎใงใ€ๆœฌ็•ช็’ฐๅขƒใฎไพ‹ๅค–ใ‚’่ฆ‹ใ‚‹ใพใงใ‚ใชใŸใฎใƒฉใ‚คใƒ–ใƒฉใƒชใฎใƒฆใƒผใ‚ถใƒผใ‚’ๆททไนฑใ•ใ›ใ‚‹ใ“ใจใŒใ‚ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“1ใ€‚ใใฎใŸใ‚ใ€ๆฑšๆŸ“ใ•ใ‚ŒใŸใ‚ฐใƒญใƒผใƒใƒซๅๅ‰็ฉบ้–“ใฏๅคšใใฎใƒ—ใƒญใ‚ฐใƒฉใƒŸใƒณใ‚ฐ่จ€่ชžใงๆ‚ชใ„็ฟ’ๆ…ฃใจใ•ใ‚Œใพใ™ใ€‚

ไพ‹ใจใ—ใฆใ€ใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐใ‚’้…ๅˆ—ใจใ—ใฆๅ–ๅพ—ใ—ใŸใ„ใจใ—ใพใ™ใ€‚config()ใฎใ‚ˆใ†ใชใ‚ฐใƒญใƒผใƒใƒซ้…ๅˆ—ใ‚’ๅฎš็พฉใ™ใ‚‹ใ“ใจใ‚‚ใงใใพใ™ใŒใ€ๅˆฅใฎใƒฉใ‚คใƒ–ใƒฉใƒชใ‚‚ไผผใŸใ‚ˆใ†ใช็›ฎ็š„ใง่‡ชๅˆ†่‡ช่บซใฎใ‚ณใƒณใƒ•ใ‚ฃใ‚ฐใ‚’ๅ–ๅพ—ใ•ใ›ใ‚‹ใŸใ‚ใซconfig()ใ‚’ๅฎš็พฉใ™ใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function config()
{
 return [
 'foo' => 'bar',
 ]
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

่จญๅฎšใƒ•ใ‚กใ‚คใƒซใ‚’.phpใƒ•ใ‚กใ‚คใƒซใจใ—ใฆไฝœๆˆใ—ใพใ™ใ€‚

// config.php
return [
 'foo' => 'bar',
];
class Configuration
{
 private $configuration = [];

 public function __construct(array $configuration)
 {
 $this->configuration = $configuration;
 }

 public function get($key)
 {
 return isset($this->configuration[$key]) ? $this->configuration[$key] : null;
 }
}

ใƒ•ใ‚กใ‚คใƒซใ‹ใ‚‰่จญๅฎšใ‚’ใƒญใƒผใƒ‰ใ—ใฆใ€Configurationใ‚ฏใƒฉใ‚นใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ—ใพใ™ใ€‚

$configuration = new Configuration($configuration);

ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใงใฏConfigurationใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚

ใ‚ทใƒณใ‚ฐใƒซใƒˆใƒณใƒ‘ใ‚ฟใƒผใƒณใฏไฝฟ็”จใ—ใชใ„

Singleton ใƒ‘ใ‚ฟใƒผใƒณใฏใ‚ขใƒณใƒใƒ‘ใ‚ฟใƒผใƒณใงใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class DBConnection
{
 private static $instance;

 private function __construct($dsn)
 {
 // ...
 }

 public static function getInstance()
 {
 if (self::$instance === null) {
 self::$instance = new self();
 }

 return self::$instance;
 }

 // ...
}

$singleton = DBConnection::getInstance();

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class DBConnection
{
 public function __construct(array $dsn)
 {
 // ...
 }

 // ...
}

DBConnectionใ‚ฏใƒฉใ‚นใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ—ใ€DSNๆ–‡ๅญ—ๅˆ—ใงใใ‚Œใ‚’่จญๅฎšใ—ใพใ™ใ€‚

$connection = new DBConnection($dsn);

ใ‚ขใƒ—ใƒชใ‚ฑใƒผใ‚ทใƒงใƒณใงใฏDBConnectionใฎใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝฟ็”จใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚Šใพใ™ใ€‚

ๆกไปถใ‚’ใ‚ซใƒ—ใ‚ปใƒซๅŒ–ใ™ใ‚‹

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

if ($article->state === 'published') {
 // ...
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

if ($article->isPublished()) {
 // ...
}

้€†ใฎๆกไปถใ‚’้ฟใ‘ใ‚‹

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function isDOMNodeNotPresent($node) {
 // ...
}

if (!isDOMNodeNotPresent($node)) {
 // ...
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

function isDOMNodePresent($node) {
 // ...
}

if (isDOMNodePresent($node)) {
 // ...
}

ๆกไปถๆ–‡ใ‚’้ฟใ‘ใ‚‹

ใใ‚“ใชใ“ใจใฏไธๅฏ่ƒฝใชใ‚ˆใ†ใซ่ฆ‹ใˆใพใ™ใ€‚ๆกไปถๆ–‡ใ‚’้ฟใ‘ใ‚‹ใจ่žใ„ใŸใจใใ€ใปใจใ‚“ใฉใฎใฒใจใฏใ€Œifๆ–‡ใชใ—ใงไฝ•ใŒใงใใ‚‹ใฎ๏ผŸใ€ใจๆผใ‚‰ใ—ใพใ™ใ€‚ใใฎ็–‘ๅ•ใธใฎๅ›ž็ญ”ใฏใ€Œใปใจใ‚“ใฉใฎๅ ดๅˆใฏใƒใƒชใƒขใƒผใƒ•ใ‚ฃใ‚บใƒ ใงๅŒใ˜ใ“ใจใŒใงใใ‚‹ใ€ใงใ™ใ€‚

ๅคšใใฎใฒใจใฏ็ถšใ„ใฆใ€Œใใ‚Œใฏใ™ใ”ใ„ใ‚“ใ ใ‘ใฉใ•ใ€ใใ†ใ™ใ‚‹ใจไฝ•ใŒใฉใ†ใชใ‚‹ใฎ๏ผŸใ€ใจๅฐ‹ใญใพใ™ใ€‚ใใ‚Œใซๅฏพใ™ใ‚‹็งใฎๅ›ž็ญ”ใฏใ€ใ“ใฎ่จ˜ไบ‹ใ‚’้€šใ˜ใฆๅญฆใ‚“ใงใใŸใ€Œ้–ขๆ•ฐใฏใฒใจใคใฎใ“ใจใ ใ‘ใ‚’ใ‚„ใ‚‹ใ€ใงใ™ใ€‚่ฆšใˆใฆใŠใ„ใฆใใ ใ•ใ„ใ€‚ใ€Œ้–ขๆ•ฐใฏใฒใจใคใฎใ“ใจใ ใ‘ใ‚’ใ‚„ใ‚‹ใ€ใงใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class Airplane {
 // ...
 public function getCruisingAltitude() {
 switch ($this->type) {
 case '777':
 return $this->getMaxAltitude() - $this->getPassengerCount();
 case 'Air Force One':
 return $this->getMaxAltitude();
 case 'Cessna':
 return $this->getMaxAltitude() - $this->getFuelExpenditure();
 }
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class Airplane {
 // ...
}

class Boeing777 extends Airplane {
 // ...
 public function getCruisingAltitude() {
 return $this->getMaxAltitude() - $this->getPassengerCount();
 }
}

class AirForceOne extends Airplane {
 // ...
 public function getCruisingAltitude() {
 return $this->getMaxAltitude();
 }
}

class Cessna extends Airplane {
 // ...
 public function getCruisingAltitude() {
 return $this->getMaxAltitude() - $this->getFuelExpenditure();
 }
}

ๅž‹ใƒใ‚งใƒƒใ‚ฏใ‚’้ฟใ‘ใ‚‹ (ใใฎ1)

PHPใฎ้–ขๆ•ฐใซใฏๅž‹ใŒใชใใ€ใฉใ‚“ใชๅž‹ใฎๅผ•ๆ•ฐใงใ‚‚ๅ—ใ‘ไป˜ใ‘ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚ๆ™‚ใซใฏใ“ใฎ่‡ช็”ฑใซ่ถณใ‚’ๅ™›ใพใ‚Œใ€ๆ™‚ใซ้–ขๆ•ฐใงๅž‹ใƒใ‚งใƒƒใ‚ฏใ‚’ใ™ใ‚‹่ช˜ๆƒ‘ใซ้ง†ใ‚‰ใ‚Œใพใ™ใ€‚ใ—ใ‹ใ—ใ€ใ“ใ‚Œใ‚’้ฟใ‘ใ‚‹ๆ–นๆณ•ใฏใ„ใใคใ‚‚ใ‚ใ‚Šใพใ™ใ€‚ใฏใ˜ใ‚ใซ่€ƒๆ…ฎใ™ในใใฏใ€ไธ€่ฒซใ—ใŸAPIใงใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function travelToTexas($vehicle)
{
 if ($vehicle instanceof Bicycle) {
 $vehicle->peddleTo(new Location('texas'));
 } elseif ($vehicle instanceof Car) {
 $vehicle->driveTo(new Location('texas'));
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

function travelToTexas(Traveler $vehicle)
{
 $vehicle->travelTo(new Location('texas'));
}

ๅž‹ใƒใ‚งใƒƒใ‚ฏใ‚’้ฟใ‘ใ‚‹ (ใใฎ2)

ๆ–‡ๅญ—ๅˆ—ใ€ๆ•ดๆ•ฐใ€ใใ—ใฆ้…ๅˆ—ใฎใ‚ˆใ†ใชๅŸบๆœฌ็š„ใชใƒ—ใƒชใƒŸใƒ†ใ‚ฃใƒ–ๅ€คใ‚’ไฝฟใฃใฆ้–ขๆ•ฐใ‚’ๅฎš็พฉใ™ใ‚‹ใจใใ€ใ‚‚ใ—ใ‚ใชใŸใŒPHP7ไปฅ้™ใงใ€ใใ—ใฆใƒใƒชใƒขใƒผใƒ•ใ‚ฃใ‚บใƒ ใ‚’ๆŽก็”จใ™ใ‚‹ใ“ใจใฏใงใใชใ„ใŒๅž‹ใƒใ‚งใƒƒใ‚ฏใซ้ ผใ‚ŠใŸใใชใฃใŸใชใ‚‰ใ€ๅž‹ๅฎฃ่จ€ใจใ€ŒๅŽณๅฏ†ใชๅž‹ใƒใ‚งใƒƒใ‚ฏใ€ใƒขใƒผใƒ‰ใฏไธ€่€ƒใฎไฝ™ๅœฐใŒใ‚ใ‚Šใพใ™ใ€‚ใ“ใ‚Œใฏ้€šๅธธใฎPHPใฎๆง‹ๆ–‡ใฎไธŠใซ้™็š„ๅž‹ไป˜ใ‘ใ‚’่ฟฝๅŠ ใ—ใพใ™ใ€‚

ๆ‰‹ไฝœๆฅญใงใฎๅž‹ใƒใ‚งใƒƒใ‚ฏใฏใ€ใใ‚Œใ‚’ใ†ใพใๅ‹•ไฝœใ•ใ›ใ‚‹ใŸใ‚ใซใฏไฝ™ๅˆ†ใช่จ˜่ฟฐใŒๅฟ…่ฆใซใชใ‚‹ใ“ใจใŒๅ•้กŒใงใ™ใ€‚ใใ‚Œใซใ‚ˆใฃใฆๅพ—ใŸใพใ‚„ใ‹ใ—ใฎๅž‹ๅฎ‰ๅ…จๆ€งใฏใ€ๆใญใ‚‰ใ‚ŒใŸ่ชญใฟใซใใ•ใ‚’่ฃœๅฎŒใงใใพใ›ใ‚“ใ€‚PHPใฎใ‚ณใƒผใƒ‰ใ‚’็ถบ้บ—ใซไฟใกใ€่‰ฏใ„ใƒ†ใ‚นใƒˆใ‚’ๆ›ธใใ€ใใ—ใฆใใกใ‚“ใจใ‚ณใƒผใƒ‰ใƒฌใƒ“ใƒฅใƒผใ‚’ใ—ใฆใใ ใ•ใ„ใ€‚ใใ†ใงใชใ‘ใ‚Œใฐใ€PHPใฎๅž‹ๅฎฃ่จ€ใจใ€ŒๅŽณๅฏ†ใชๅž‹ใƒใ‚งใƒƒใ‚ฏใ€ใƒขใƒผใƒ‰ใ‚’ๅˆฉ็”จใ™ใ‚‹ใ“ใจใงใใ‚Œใ‚’ๅฎŸ็พใงใใพใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function combine($val1, $val2)
{
 if (!is_numeric($val1) || !is_numeric($val2)) {
 throw new \Exception('Must be of type Number');
 }

 return $val1 + $val2;
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

function combine(int $val1, int $val2)
{
 return $val1 + $val2;
}

ไธ่ฆใ‚ณใƒผใƒ‰ใฏๆถˆใ™

ใƒ‡ใƒƒใƒ‰ใ‚ณใƒผใƒ‰(ไธ่ฆใชใ‚ณใƒผใƒ‰)ใฏ้‡่ค‡ใ‚ณใƒผใƒ‰ใจๅŒใ˜ใๆ‚ชใงใ™ใ€‚็†็”ฑใ‚‚ใชใ„ใฎใซใใ‚Œใ‚’ๆฎ‹ใ•ใชใ„ใงใใ ใ•ใ„ใ€‚ใ‚‚ใ—ๅ‘ผใฐใ‚Œใชใ„ใชใ‚‰ใ€ๆถˆใ—ๅŽปใฃใฆใใ ใ•ใ„๏ผ ใ‚‚ใ—ๅฐ†ๆฅๅฟ…่ฆใซใชใ‚‹ใ ใ‚ใ†ใจๆ€ใฃใฆใ‚‚ใ€ใใ‚ŒใฏGitใชใฉใฎใƒใƒผใ‚ธใƒงใƒณ็ฎก็†ใ‚ทใ‚นใƒ†ใƒ ใฎๅฑฅๆญดใ‹ใ‚‰ๅฎ‰ๅ…จใซๅ–ใ‚Šๅ‡บใ™ใ“ใจใŒใงใใพใ™ใ€‚

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function oldRequestModule($url) {
 // ...
}

function newRequestModule($url) {
 // ...
}

$req = new newRequestModule($requestUrl);
inventoryTracker('apples', $req, 'www.inventory-awesome.io');

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

function requestModule($url) {
 // ...
}

$req = new requestModule($requestUrl);
inventoryTracker('apples', $req, 'www.inventory-awesome.io');

ใ„ใพใฎใจใ“ใ‚ใ€็ฟป่จณใฏใ“ใ“ใพใงใ€‚ๆš‡ใŒใ‚ใฃใŸใ‚‰ใ€ๆ˜Žๆ—ฅใฏๅพŒๆ—ฅใซใฏ็ถšใใ‚’่จณใ—ใพใ™ใ€‚


ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใจใƒ‡ใƒผใ‚ฟๆง‹้€ 

ใ‚ฒใƒƒใ‚ฟใƒผใจใ‚ปใƒƒใ‚ฟใƒผใ‚’ๅˆฉ็”จใ™ใ‚‹

PHPใงใฏใƒกใ‚ฝใƒƒใƒ‰ใซpublic, protected, privateใฎใ‚ญใƒผใƒฏใƒผใƒ‰ใ‚’ๆŒ‡ๅฎšใงใใพใ™ใ€‚ใ“ใ‚Œใ‚’ๅˆฉ็”จใ™ใ‚‹ใ“ใจใงใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฎๅค‰ๆ›ดใ‚’ๅˆถๅพกใงใใพใ™ใ€‚

  • When you want to do more beyond getting an object property, you don't have to look up and change every accessor in your codebase.
  • Makes adding validation simple when doing a set.
  • Encapsulates the internal representation.
  • Easy to add logging and error handling when getting and setting.
  • Inheriting this class, you can override default functionality.
  • You can lazy load your object's properties, let's say getting it from a
    server.

Additionally, this is part of Open/Closed principle, from object-oriented
design principles.

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class BankAccount {
 public $balance = 1000;
}

$bankAccount = new BankAccount();

// Buy shoes...
$bankAccount->balance -= 100;

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class BankAccount {
 private $balance;
 
 public function __construct($balance = 1000) {
 $this->balance = $balance;
 }
 
 public function withdrawBalance($amount) {
 if ($amount > $this->balance) {
 throw new \Exception('Amount greater than available balance.');
 }
 $this->balance -= $amount;
 }
 
 public function depositBalance($amount) {
 $this->balance += $amount;
 }
 
 public function getBalance() {
 return $this->balance;
 }
}

$bankAccount = new BankAccount();

// Buy shoes...
$bankAccount->withdrawBalance($shoesPrice);

// Get balance
$balance = $bankAccount->getBalance();

ใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใฎใƒ—ใƒญใƒ‘ใƒ†ใ‚ฃใฏprivate/protectedใซใ™ใ‚‹

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class Employee {
 public $name;
 
 public function __construct($name) {
 $this->name = $name;
 }
}

$employee = new Employee('John Doe');
echo 'Employee name: '.$employee->name; // Employee name: John Doe

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class Employee {
 protected $name;
 
 public function __construct($name) {
 $this->name = $name;
 }
 
 public function getName() {
 return $this->name;
 }
}

$employee = new Employee('John Doe');
echo 'Employee name: '.$employee->getName(); // Employee name: John Doe

ใ‚ฏใƒฉใ‚น

S: ๅ˜ไธ€่ฒฌไปปใฎๅŽŸๅ‰‡ (SRP)

As stated in Clean Code, "There should never be more than one reason for a class
to change". It's tempting to jam-pack a class with a lot of functionality, like
when you can only take one suitcase on your flight. The issue with this is
that your class won't be conceptually cohesive and it will give it many reasons
to change. Minimizing the amount of times you need to change a class is important.
It's important because if too much functionality is in one class and you modify a piece of it,
it can be difficult to understand how that will affect other dependent modules in
your codebase.

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class UserSettings {
 private $user;
 public function __construct($user) {
 $this->user = user;
 }
 
 public function changeSettings($settings) {
 if ($this->verifyCredentials()) {
 // ...
 }
 }
 
 private function verifyCredentials() {
 // ...
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class UserAuth {
 private $user;
 public function __construct($user) {
 $this->user = user;
 }
 
 public function verifyCredentials() {
 // ...
 }
}


class UserSettings {
 private $user;
 public function __construct($user) {
 $this->user = $user;
 $this->auth = new UserAuth($user);
 }
 
 public function changeSettings($settings) {
 if ($this->auth->verifyCredentials()) {
 // ...
 }
 }
}

O: ใ‚ชใƒผใƒ—ใƒณ/ใ‚ฏใƒญใƒผใ‚บใƒ‰ใฎๅŽŸๅ‰‡ (OCP)

As stated by Bertrand Meyer, "software entities (classes, modules, functions,
etc.) should be open for extension, but closed for modification." What does that
mean though? This principle basically states that you should allow users to
add new functionalities without changing existing code.

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

abstract class Adapter
{
 protected $name;

 public function getName()
 {
 return $this->name;
 }
}

class AjaxAdapter extends Adapter
{
 public function __construct()
 {
 parent::__construct();
 $this->name = 'ajaxAdapter';
 }
}

class NodeAdapter extends Adapter
{
 public function __construct()
 {
 parent::__construct();
 $this->name = 'nodeAdapter';
 }
}

class HttpRequester
{
 private $adapter;

 public function __construct($adapter)
 {
 $this->adapter = $adapter;
 }
 
 public function fetch($url)
 {
 $adapterName = $this->adapter->getName();

 if ($adapterName === 'ajaxAdapter') {
 return $this->makeAjaxCall($url);
 } elseif ($adapterName === 'httpNodeAdapter') {
 return $this->makeHttpCall($url);
 }
 }
 
 protected function makeAjaxCall($url)
 {
 // request and return promise
 }
 
 protected function makeHttpCall($url)
 {
 // request and return promise
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

interface Adapter
{
 public function request($url);
}

class AjaxAdapter implements Adapter
{
 public function request($url)
 {
 // request and return promise
 }
}

class NodeAdapter implements Adapter
{
 public function request($url)
 {
 // request and return promise
 }
}

class HttpRequester
{
 private $adapter;

 public function __construct(Adapter $adapter)
 {
 $this->adapter = $adapter;
 }
 
 public function fetch($url)
 {
 return $this->adapter->request($url);
 }
}

L: ใƒชใ‚นใ‚ณใƒ•ใฎ็ฝฎๆ›ๅŽŸๅ‰‡ (LSP)

This is a scary term for a very simple concept. It's formally defined as "If S
is a subtype of T, then objects of type T may be replaced with objects of type S
(i.e., objects of type S may substitute objects of type T) without altering any
of the desirable properties of that program (correctness, task performed,
etc.)." That's an even scarier definition.

The best explanation for this is if you have a parent class and a child class,
then the base class and child class can be used interchangeably without getting
incorrect results. This might still be confusing, so let's take a look at the
classic Square-Rectangle example. Mathematically, a square is a rectangle, but
if you model it using the "is-a" relationship via inheritance, you quickly
get into trouble.

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class Rectangle {
 private $width, $height;
 
 public function __construct() {
 $this->width = 0;
 $this->height = 0;
 }
 
 public function setColor($color) {
 // ...
 }
 
 public function render($area) {
 // ...
 }
 
 public function setWidth($width) {
 $this->width = $width;
 }
 
 public function setHeight($height) {
 $this->height = $height;
 }
 
 public function getArea() {
 return $this->width * $this->height;
 }
}

class Square extends Rectangle {
 public function setWidth($width) {
 $this->width = $this->height = $width;
 }
 
 public function setHeight(height) {
 $this->width = $this->height = $height;
 }
}

function renderLargeRectangles($rectangles) {
 foreach($rectangle in $rectangles) {
 $rectangle->setWidth(4);
 $rectangle->setHeight(5);
 $area = $rectangle->getArea(); // BAD: Will return 25 for Square. Should be 20.
 $rectangle->render($area);
 });
}

$rectangles = [new Rectangle(), new Rectangle(), new Square()];
renderLargeRectangles($rectangles);

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

abstract class Shape {
 private $width, $height;
 
 abstract public function getArea();
 
 public function setColor($color) {
 // ...
 }
 
 public function render($area) {
 // ...
 }
}

class Rectangle extends Shape {
 public function __construct {
 parent::__construct();
 $this->width = 0;
 $this->height = 0;
 }
 
 public function setWidth($width) {
 $this->width = $width;
 }
 
 public function setHeight($height) {
 $this->height = $height;
 }
 
 public function getArea() {
 return $this->width * $this->height;
 }
}

class Square extends Shape {
 public function __construct {
 parent::__construct();
 $this->length = 0;
 }
 
 public function setLength($length) {
 $this->length = $length;
 }
 
 public function getArea() {
 return pow($this->length, 2);
 }
}

function renderLargeRectangles($rectangles) {
 foreach($rectangle in $rectangles) {
 if ($rectangle instanceof Square) {
 $rectangle->setLength(5);
 } else if ($rectangle instanceof Rectangle) {
 $rectangle->setWidth(4);
 $rectangle->setHeight(5);
 }
 
 $area = $rectangle->getArea(); 
 $rectangle->render($area);
 });
}

$shapes = [new Rectangle(), new Rectangle(), new Square()];
renderLargeRectangles($shapes);

I: ใ‚คใƒณใ‚ฟใƒผใƒ•ใ‚งใ‚คใ‚นๅˆ†้›ขใฎๅŽŸๅ‰‡ (ISP)

ISP states that "Clients should not be forced to depend upon interfaces that
they do not use."

A good example to look at that demonstrates this principle is for
classes that require large settings objects. Not requiring clients to setup
huge amounts of options is beneficial, because most of the time they won't need
all of the settings. Making them optional helps prevent having a "fat interface".

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

interface WorkerInterface {
 public function work();
 public function eat();
}

class Worker implements WorkerInterface {
 public function work() {
 // ....working
 }
 public function eat() {
 // ...... eating in launch break
 }
}

class SuperWorker implements WorkerInterface {
 public function work() {
 //.... working much more
 }

 public function eat() {
 //.... eating in launch break
 }
}

class Manager {
 /** @var WorkerInterface $worker **/
 private $worker;
 
 public function setWorker(WorkerInterface $worker) {
 $this->worker = $worker;
 }

 public function manage() {
 $this->worker->work();
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

interface WorkerInterface extends FeedableInterface, WorkableInterface {
}

interface WorkableInterface {
 public function work();
}

interface FeedableInterface {
 public function eat();
}

class Worker implements WorkableInterface, FeedableInterface {
 public function work() {
 // ....working
 }

 public function eat() {
 //.... eating in launch break
 }
}

class Robot implements WorkableInterface {
 public function work() {
 // ....working
 }
}

class SuperWorker implements WorkerInterface {
 public function work() {
 //.... working much more
 }

 public function eat() {
 //.... eating in launch break
 }
}

class Manager {
 /** @var $worker WorkableInterface **/
 private $worker;

 public function setWorker(WorkableInterface $w) {
 $this->worker = $w;
 }

 public function manage() {
 $this->worker->work();
 }
}

D: ไพๅญ˜้€†่ปขใฎๆณ•ๅ‰‡ (DIP)

This principle states two essential things:

  1. High-level modules should not depend on low-level modules. Both should
    depend on abstractions.
  2. Abstractions should not depend upon details. Details should depend on
    abstractions.

This can be hard to understand at first, but if you've worked with PHP frameworks (like Symfony), you've seen an implementation of this principle in the form of Dependency
Injection (DI). While they are not identical concepts, DIP keeps high-level
modules from knowing the details of its low-level modules and setting them up.
It can accomplish this through DI. A huge benefit of this is that it reduces
the coupling between modules. Coupling is a very bad development pattern because
it makes your code hard to refactor.

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class Worker {
 public function work() {
 // ....working
 }
}

class Manager {
 /** @var Worker $worker **/
 private $worker;
 
 public function __construct(Worker $worker) {
 $this->worker = $worker;
 }
 
 public function manage() {
 $this->worker->work();
 }
}

class SuperWorker extends Worker {
 public function work() {
 //.... working much more
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

interface WorkerInterface {
 public function work();
}

class Worker implements WorkerInterface {
 public function work() {
 // ....working
 }
}

class SuperWorker implements WorkerInterface {
 public function work() {
 //.... working much more
 }
}

class Manager {
 /** @var Worker $worker **/
 private $worker;
 
 public function __construct(WorkerInterface $worker) {
 $this->worker = $worker;
 }
 
 public function manage() {
 $this->worker->work();
 }
}

ใƒกใ‚ฝใƒƒใƒ‰ใƒใ‚งใƒผใƒณใ‚’ๅˆฉ็”จใ™ใ‚‹

This pattern is very useful and commonly used it many libraries such
as PHPUnit and Doctrine. It allows your code to be expressive, and less verbose.
For that reason, I say, use method chaining and take a look at how clean your code
will be. In your class functions, simply return this at the end of every function,
and you can chain further class methods onto it.

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class Car {
 private $make, $model, $color;
 
 public function __construct() {
 $this->make = 'Honda';
 $this->model = 'Accord';
 $this->color = 'white';
 }
 
 public function setMake($make) {
 $this->make = $make;
 }
 
 public function setModel($model) {
 $this->model = $model;
 }
 
 public function setColor($color) {
 $this->color = $color;
 }
 
 public function dump() {
 var_dump($this->make, $this->model, $this->color);
 }
}

$car = new Car();
$car->setColor('pink');
$car->setMake('Ford');
$car->setModel('F-150');
$car->dump();

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class Car {
 private $make, $model, $color;
 
 public function __construct() {
 $this->make = 'Honda';
 $this->model = 'Accord';
 $this->color = 'white';
 }
 
 public function setMake($make) {
 $this->make = $make;
 
 // NOTE: Returning this for chaining
 return $this;
 }
 
 public function setModel($model) {
 $this->model = $model;
 
 // NOTE: Returning this for chaining
 return $this;
 }
 
 public function setColor($color) {
 $this->color = $color;
 
 // NOTE: Returning this for chaining
 return $this;
 }
 
 public function dump() {
 var_dump($this->make, $this->model, $this->color);
 }
}

$car = (new Car())
 ->setColor('pink')
 ->setMake('Ford')
 ->setModel('F-150')
 ->dump();

็ถ™ๆ‰ฟใ‚ˆใ‚Šใ‚‚ใ‚ณใƒณใƒใ‚ธใ‚ทใƒงใƒณ

As stated famously in Design Patterns by the Gang of Four,
you should prefer composition over inheritance where you can. There are lots of
good reasons to use inheritance and lots of good reasons to use composition.
The main point for this maxim is that if your mind instinctively goes for
inheritance, try to think if composition could model your problem better. In some
cases it can.

You might be wondering then, "when should I use inheritance?" It
depends on your problem at hand, but this is a decent list of when inheritance
makes more sense than composition:

  1. Your inheritance represents an "is-a" relationship and not a "has-a"
    relationship (Human->Animal vs. User->UserDetails).
  2. You can reuse code from the base classes (Humans can move like all animals).
  3. You want to make global changes to derived classes by changing a base class.
    (Change the caloric expenditure of all animals when they move).

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

class Employee {
 private $name, $email;
 
 public function __construct($name, $email) {
 $this->name = $name;
 $this->email = $email;
 }
 
 // ...
}

// Bad because Employees "have" tax data. 
// EmployeeTaxData is not a type of Employee

class EmployeeTaxData extends Employee {
 private $ssn, $salary;
 
 public function __construct($name, $email, $ssn, $salary) {
 parent::__construct($name, $email);
 $this->ssn = $ssn;
 $this->salary = $salary;
 }
 
 // ...
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

class EmployeeTaxData {
 private $ssn, $salary;
 
 public function __construct($ssn, $salary) {
 $this->ssn = $ssn;
 $this->salary = $salary;
 }
 
 // ...
}

class Employee {
 private $name, $email, $taxData;
 
 public function __construct($name, $email) {
 $this->name = $name;
 $this->email = $email;
 }
 
 public function setTaxData($ssn, $salary) {
 $this->taxData = new EmployeeTaxData($ssn, $salary);
 }
 // ...
}

ๅŒใ˜ใ“ใจใ‚’็นฐใ‚Š่ฟ”ใ™ใช (DRY)

Try to observe the DRY principle.

Do your absolute best to avoid duplicate code. Duplicate code is bad because
it means that there's more than one place to alter something if you need to
change some logic.

Imagine if you run a restaurant and you keep track of your inventory: all your
tomatoes, onions, garlic, spices, etc. If you have multiple lists that
you keep this on, then all have to be updated when you serve a dish with
tomatoes in them. If you only have one list, there's only one place to update!

Oftentimes you have duplicate code because you have two or more slightly
different things, that share a lot in common, but their differences force you
to have two or more separate functions that do much of the same things. Removing
duplicate code means creating an abstraction that can handle this set of different
things with just one function/module/class.

Getting the abstraction right is critical, that's why you should follow the
SOLID principles laid out in the Classes section. Bad abstractions can be
worse than duplicate code, so be careful! Having said this, if you can make
a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
updating multiple places anytime you want to change one thing.

๐Ÿ‘ :no_good_tone1:
Bad (ใ ใ‚)

function showDeveloperList($developers)
{
 foreach ($developers as $developer) {
 $expectedSalary = $developer->calculateExpectedSalary();
 $experience = $developer->getExperience();
 $githubLink = $developer->getGithubLink();
 $data = [
 $expectedSalary,
 $experience,
 $githubLink
 ];
 
 render($data);
 }
}

function showManagerList($managers)
{
 foreach ($managers as $manager) {
 $expectedSalary = $manager->calculateExpectedSalary();
 $experience = $manager->getExperience();
 $githubLink = $manager->getGithubLink();
 $data = [
 $expectedSalary,
 $experience,
 $githubLink
 ];
 
 render($data);
 }
}

๐Ÿ‘ :ok_woman_tone1:
Good (ใ‚ˆใ„)

function showList($employees)
{
 foreach ($employees as $employee) {
 $expectedSalary = $employee->calculateExpectedSalary();
 $experience = $employee->getExperience();
 $githubLink = $employee->getGithubLink();
 $data = [
 $expectedSalary,
 $experience,
 $githubLink
 ];
 
 render($data);
 }
}

Very good:

It is better to use a compact version of the code.

function showList($employees)
{
 foreach ($employees as $employee) {
 render([
 $employee->calculateExpectedSalary(),
 $employee->getExperience(),
 $employee->getGithubLink()
 ]);
 }
}

่„šๆณจ

  1. ่จณๆณจ: ๆœฌๅฝ“ใ‹ใ‚ˆโ€ฆ๏ผŸ (็ฟป่จณใซ่‡ชไฟกใŒๆŒใฆใชใ„) ๅŽŸๆ–‡ใงใฏไปฅไธ‹ใฎ้€šใ‚Š: Polluting globals is a bad practice in very languages because you could clash with another library and the user of your API would be none-the-wiser until they get an exception in production. โ†ฉ

216

Go to list of users who liked

221
2

Go to list of comments

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
216

Go to list of users who liked

221