@@ -89,7 +89,10 @@ preg_match($cityZipCodeRegex, $address, $matches);
8989saveCityZipCode($matches[1], $matches[2]);
9090```
9191
92- ** Good** :
92+ ** Not bad** :
93+
94+ It's better, but we are still heavily dependent on regex.
95+
9396``` php
9497$address = 'One Infinite Loop, Cupertino 95014';
9598$cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/';
@@ -98,6 +101,17 @@ preg_match($cityZipCodeRegex, $address, $matches);
98101list(, $city, $zipCode) = $matchers;
99102saveCityZipCode($city, $zipCode);
100103```
104+
105+ ** Good** :
106+
107+ Decrease dependence on regex by naming subpatterns.
108+ ``` php
109+ $address = 'One Infinite Loop, Cupertino 95014';
110+ $cityZipCodeRegex = '/^[^,\\]+[,\\\s]+(?<city >.+?)\s*(?<zipCode >\d{5})?$/';
111+ preg_match($cityZipCodeRegex, $address, $matches);
112+
113+ saveCityZipCode($matches['city'], $matches['zipCode']);
114+ ```
101115** [ ⬆ back to top] ( #table-of-contents ) **
102116
103117### Avoid Mental Mapping
@@ -108,9 +122,9 @@ Explicit is better than implicit.
108122``` php
109123$l = ['Austin', 'New York', 'San Francisco'];
110124
111- foreach ($i= 0; $i< count ($l); $i++) {
125+ for ($i = 0; $i < count($l); $i++) {
112126 $li = $l[$i];
113- oStuff ();
127+ doStuff ();
114128 doSomeOtherStuff();
115129 // ...
116130 // ...
@@ -124,9 +138,7 @@ foreach($i=0; $i<count($l); $i++) {
124138``` php
125139$locations = ['Austin', 'New York', 'San Francisco'];
126140
127- foreach($i =0; $i <count($locations); $i++) {
128- $location = $locations[$i];
129-
141+ foreach ($locations as $location) {
130142 doStuff();
131143 doSomeOtherStuff();
132144 // ...
@@ -208,7 +220,8 @@ function createMenu($title, $body, $buttonText, $cancellable) {
208220
209221** Good** :
210222``` php
211- class menuConfig() {
223+ class MenuConfig
224+ {
212225 public $title;
213226 public $body;
214227 public $buttonText;
@@ -519,7 +532,7 @@ would be much better to use singleton design pattern and simple set configuratio
519532``` php
520533function config() {
521534 return [
522- 'foo': 'bar',
535+ 'foo' => 'bar',
523536 ]
524537}
525538```
@@ -788,7 +801,7 @@ class BankAccount {
788801$bankAccount = new BankAccount();
789802
790803// Buy shoes...
791- $bankAccount->withdrawBalance(- $shoesPrice);
804+ $bankAccount->withdrawBalance($shoesPrice);
792805
793806// Get balance
794807$balance = $bankAccount->getBalance();
@@ -873,7 +886,7 @@ class UserAuth {
873886 $this->user = user;
874887 }
875888
876- protected function verifyCredentials() {
889+ public function verifyCredentials() {
877890 // ...
878891 }
879892}
@@ -912,7 +925,7 @@ abstract class Adapter {
912925
913926class AjaxAdapter extends Adapter {
914927 public function __construct() {
915- parent::__construct();
928+ parent::__construct();
916929 $this->name = 'ajaxAdapter';
917930 }
918931}
@@ -1106,7 +1119,7 @@ class Square extends Shape {
11061119 }
11071120
11081121 public function getArea() {
1109- return $this->length * $this->length ;
1122+ return pow( $this->length, 2) ;
11101123 }
11111124}
11121125
@@ -1168,7 +1181,7 @@ class Manager {
11681181 /** @var WorkerInterface $worker **/
11691182 private $worker;
11701183
1171- public void setWorker(WorkerInterface $worker) {
1184+ public function setWorker(WorkerInterface $worker) {
11721185 $this->worker = $worker;
11731186 }
11741187
@@ -1202,7 +1215,7 @@ class Worker implements WorkableInterface, FeedableInterface {
12021215}
12031216
12041217class Robot implements WorkableInterface {
1205- public void work() {
1218+ public function work() {
12061219 // ....working
12071220 }
12081221}
@@ -1296,11 +1309,11 @@ class Manager {
12961309 /** @var Worker $worker **/
12971310 private $worker;
12981311
1299- public void __construct(WorkerInterface $worker) {
1312+ public function __construct(WorkerInterface $worker) {
13001313 $this->worker = $worker;
13011314 }
13021315
1303- public void manage() {
1316+ public function manage() {
13041317 $this->worker->work();
13051318 }
13061319}
0 commit comments