@@ -151,34 +151,36 @@ foreach ($locations as $location) {
151151
152152
153153### Don't add unneeded context
154+
154155If your class/object name tells you something, don't repeat that in your
155156variable name.
156157
157158** Bad:**
159+
158160``` php
159- $car = [
160- 'carMake' => 'Honda',
161- 'carModel' => 'Accord',
162- 'carColor' => 'Blue',
163- ] ;
161+ class Car
162+ {
163+ public $carMake;
164+ public $carModel;
165+ public $carColor ;
164166
165- function paintCar(& $car) {
166- $car['carColor'] = 'Red';
167+ //...
167168}
168169```
169170
170171** Good** :
172+
171173``` php
172- $car = [
173- 'make' => 'Honda',
174- 'model' => 'Accord',
175- 'color' => 'Blue',
176- ] ;
174+ class Car
175+ {
176+ public $make;
177+ public $model;
178+ public $color ;
177179
178- function paintCar(& $car) {
179- $car['color'] = 'Red';
180+ //...
180181}
181182```
183+
182184** [ ⬆ back to top] ( #table-of-contents ) **
183185
184186### Use default arguments instead of short circuiting or conditionals
@@ -335,7 +337,8 @@ function parseBetterJSAlternative($code) {
335337}
336338```
337339
338- ** Good** :
340+ ** Good:**
341+
339342``` php
340343function tokenize($code) {
341344 $regexes = [
@@ -395,9 +398,11 @@ a good abstraction, do it! Don't repeat yourself, otherwise you'll find yourself
395398updating multiple places anytime you want to change one thing.
396399
397400** Bad:**
401+
398402``` php
399- function showDeveloperList($developers) {
400- foreach($developers as $developer) {
403+ function showDeveloperList($developers)
404+ {
405+ foreach ($developers as $developer) {
401406 $expectedSalary = $developer->calculateExpectedSalary();
402407 $experience = $developer->getExperience();
403408 $githubLink = $developer->getGithubLink();
@@ -411,8 +416,9 @@ function showDeveloperList($developers) {
411416 }
412417}
413418
414- function showManagerList($managers) {
415- foreach($managers as $manager) {
419+ function showManagerList($managers)
420+ {
421+ foreach ($managers as $manager) {
416422 $expectedSalary = $manager->calculateExpectedSalary();
417423 $experience = $manager->getExperience();
418424 $githubLink = $manager->getGithubLink();
@@ -427,13 +433,15 @@ function showManagerList($managers) {
427433}
428434```
429435
430- ** Good** :
436+ ** Good:**
437+
431438``` php
432- function showList($employees) {
433- foreach($employees as $employe) {
434- $expectedSalary = $employe->calculateExpectedSalary();
435- $experience = $employe->getExperience();
436- $githubLink = $employe->getGithubLink();
439+ function showList($employees)
440+ {
441+ foreach ($employees as $employe) {
442+ $expectedSalary = $employe->calculateExpectedSalary();
443+ $experience = $employe->getExperience();
444+ $githubLink = $employe->getGithubLink();
437445 $data = [
438446 $expectedSalary,
439447 $experience,
@@ -444,6 +452,24 @@ function showList($employees) {
444452 }
445453}
446454```
455+
456+ ** Very good:**
457+
458+ It is better to use a compact version of the code.
459+
460+ ``` php
461+ function showList($employees)
462+ {
463+ foreach ($employees as $employe) {
464+ render([
465+ $employe->calculateExpectedSalary(),
466+ $employe->getExperience(),
467+ $employe->getGithubLink()
468+ ]);
469+ }
470+ }
471+ ```
472+
447473** [ ⬆ back to top] ( #table-of-contents ) **
448474
449475### Don't use flags as function parameters
@@ -684,32 +710,39 @@ function travelToTexas($vehicle) {
684710** [ ⬆ back to top] ( #table-of-contents ) **
685711
686712### Avoid type-checking (part 2)
713+
687714If you are working with basic primitive values like strings, integers, and arrays,
688- and you can't use polymorphism but you still feel the need to type-check,
689- you should consider type declaration or strict mode. It provides you with static
690- typing on top of standard PHP syntax. The problem with manually type-checking is
691- that doing it well requires so much extra verbiage that the faux "type-safety"
692- you get doesn't make up for the lost readability. Keep your PHP clean, write good
693- tests, and have good code reviews. Otherwise, do all of that but with PHP strict
694- type declaration or strict mode.
715+ and you use PHP 7+ and you can't use polymorphism but you still feel the need to
716+ type-check, you should consider
717+ [ type declaration] ( http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration )
718+ or strict mode. It provides you with static typing on top of standard PHP syntax.
719+ The problem with manually type-checking is that doing it well requires so much
720+ extra verbiage that the faux "type-safety" you get doesn't make up for the lost
721+ readability. Keep your PHP clean, write good tests, and have good code reviews.
722+ Otherwise, do all of that but with PHP strict type declaration or strict mode.
695723
696724** Bad:**
725+
697726``` php
698- function combine($val1, $val2) {
699- if (is_numeric($val1) && is_numeric($val2)) {
700- return $val1 + $val2;
727+ function combine($val1, $val2)
728+ {
729+ if (!is_numeric($val1) || !is_numeric($val2)) {
730+ throw new \Exception('Must be of type Number');
701731 }
702-
703- throw new \Exception('Must be of type Number') ;
732+
733+ return $val1 + $val2 ;
704734}
705735```
706736
707737** Good** :
738+
708739``` php
709- function combine(int $val1, int $val2) {
740+ function combine(int $val1, int $val2)
741+ {
710742 return $val1 + $val2;
711743}
712744```
745+
713746** [ ⬆ back to top] ( #table-of-contents ) **
714747
715748### Remove dead code
0 commit comments