1- # Clean Code PHP
1+ # Clean Code PHP
22
33## Table of Contents
44
1414 * [ Avoid Mental Mapping] ( #avoid-mental-mapping )
1515 * [ Don't add unneeded context] ( #dont-add-unneeded-context )
1616 * [ Use default arguments instead of short circuiting or conditionals] ( #use-default-arguments-instead-of-short-circuiting-or-conditionals )
17- 3 . [ Comparaison ] ( #comparaison )
18- * [ Use identical comparison] ( #identical_comparison )
17+ 3 . [ Comparison ] ( #comparison )
18+ * [ Use identical comparison] ( #use-identical-comparison )
1919 4 . [ Functions] ( #functions )
2020 * [ Function arguments (2 or fewer ideally)] ( #function-arguments-2-or-fewer-ideally )
2121 * [ Functions should do one thing] ( #functions-should-do-one-thing )
@@ -57,7 +57,7 @@ Not every principle herein has to be strictly followed, and even fewer will be u
5757agreed upon. These are guidelines and nothing more, but they are ones codified over many
5858years of collective experience by the authors of * Clean Code* .
5959
60- Inspired from [ clean-code-javascript] ( https://github.com/ryanmcdermott/clean-code-javascript )
60+ Inspired from [ clean-code-javascript] ( https://github.com/ryanmcdermott/clean-code-javascript ) .
6161
6262Although many developers still use PHP 5, most of the examples in this article only work with PHP 7.1+.
6363
@@ -188,7 +188,7 @@ saveCityZipCode($matches['city'], $matches['zipCode']);
188188
189189### Avoid nesting too deeply and return early (part 1)
190190
191- Too many if else statements can make your code hard to follow. Explicit is better
191+ Too many if- else statements can make your code hard to follow. Explicit is better
192192than implicit.
193193
194194** Bad:**
@@ -390,34 +390,38 @@ function createMicrobrewery(string $breweryName = 'Hipster Brew Co.'): void
390390
391391## Comparison
392392
393- ** [ ⬆ back to top] ( #table-of-contents ) **
394-
395393### Use [ identical comparison] ( http://php.net/manual/en/language.operators.comparison.php )
396394
397395** Not good:**
398396
397+ The simple comparison will convert the string in an integer.
398+
399399``` php
400400$a = '42';
401401$b = 42;
402- Use the simple comparison will convert the string in an int
403402
404- if( $a != $b ) {
405- //The expression will always passes
403+ if ( $a != $b) {
404+ // The expression will always pass
406405}
407-
408406```
409- The comparison $a != $b return false but in fact it's true !
410- The string '42' is different than the int 42
407+
408+ The comparison ` $a != $b ` returns ` FALSE ` but in fact it's ` TRUE ` !
409+ The string ` 42 ` is different than the integer ` 42 ` .
411410
412411** Good:**
413- Use the identical comparison will compare type and value
412+
413+ The identical comparison will compare type and value.
414+
414415``` php
415- if( $a !== $b ) {
416- //The expression is verified
417- }
416+ $a = '42';
417+ $b = 42;
418418
419+ if ($a !== $b) {
420+ // The expression is verified
421+ }
419422```
420- The comparison $a !== $b return true.
423+
424+ The comparison ` $a !== $b ` returns ` TRUE ` .
421425
422426** [ ⬆ back to top] ( #table-of-contents ) **
423427
@@ -783,7 +787,7 @@ var_dump($newName); // ['Ryan', 'McDermott'];
783787
784788Polluting globals is a bad practice in many languages because you could clash with another
785789library and the user of your API would be none-the-wiser until they get an exception in
786- production. Let's think about an example: what if you wanted to have configuration array.
790+ production. Let's think about an example: what if you wanted to have configuration array?
787791You could write global function like ` config() ` , but it could clash with another library
788792that tried to do the same thing.
789793
@@ -1356,10 +1360,10 @@ pattern reduces the verbosity of the code (for example the [PHPUnit Mock Builder
13561360or the [ Doctrine Query Builder] ( http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/query-builder.html ) ),
13571361more often it comes at some costs:
13581362
1359- 1 . Breaks [ Encapsulation] ( https://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 )
1360- 2 . Breaks [ Decorators] ( https://en.wikipedia.org/wiki/Decorator_pattern )
1361- 3 . Is harder to [ mock] ( https://en.wikipedia.org/wiki/Mock_object ) in a test suite
1362- 4 . Makes diffs of commits harder to read
1363+ 1 . Breaks [ Encapsulation] ( https://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 ) .
1364+ 2 . Breaks [ Decorators] ( https://en.wikipedia.org/wiki/Decorator_pattern ) .
1365+ 3 . Is harder to [ mock] ( https://en.wikipedia.org/wiki/Mock_object ) in a test suite.
1366+ 4 . Makes diffs of commits harder to read.
13631367
13641368For more informations you can read the full [ blog post] ( https://ocramius.github.io/blog/fluent-interfaces-are-evil/ )
13651369on this topic written by [ Marco Pivetta] ( https://github.com/Ocramius ) .
@@ -1799,7 +1803,7 @@ ISP states that "Clients should not be forced to depend upon interfaces that
17991803they do not use."
18001804
18011805A good example to look at that demonstrates this principle is for
1802- classes that require large settings objects. Not requiring clients to setup
1806+ classes that require large settings objects. Not requiring clients to set up
18031807huge amounts of options is beneficial, because most of the time they won't need
18041808all of the settings. Making them optional helps prevent having a "fat interface".
18051809
@@ -1989,7 +1993,7 @@ tomatoes, onions, garlic, spices, etc. If you have multiple lists that
19891993you keep this on, then all have to be updated when you serve a dish with
19901994tomatoes in them. If you only have one list, there's only one place to update!
19911995
1992- Oftentimes you have duplicate code because you have two or more slightly
1996+ Often you have duplicate code because you have two or more slightly
19931997different things, that share a lot in common, but their differences force you
19941998to have two or more separate functions that do much of the same things. Removing
19951999duplicate code means creating an abstraction that can handle this set of different
@@ -1999,7 +2003,7 @@ Getting the abstraction right is critical, that's why you should follow the
19992003SOLID principles laid out in the [ Classes] ( #classes ) section. Bad abstractions can be
20002004worse than duplicate code, so be careful! Having said this, if you can make
20012005a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
2002- updating multiple places anytime you want to change one thing.
2006+ updating multiple places any time you want to change one thing.
20032007
20042008** Bad:**
20052009
@@ -2093,5 +2097,9 @@ This is also available in other languages:
20932097 * [ panuwizzle/clean-code-php] ( https://github.com/panuwizzle/clean-code-php )
20942098* :fr : ** French:**
20952099 * [ errorname/clean-code-php] ( https://github.com/errorname/clean-code-php )
2096-
2100+ * :vi: ** Vietnamese**
2101+ * [ viethuongdev/clean-code-php] ( https://github.com/viethuongdev/clean-code-php )
2102+ * 🇰🇷 ** Korean:**
2103+ * [ yujineeee/clean-code-php] ( https://github.com/yujineeee/clean-code-php )
2104+
20972105** [ ⬆ back to top] ( #table-of-contents ) **
0 commit comments