1 | <?php |
||
29 | class MailChimp |
||
30 | { |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $apiKey; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $storeId; |
||
40 | |||
41 | /** |
||
42 | * @var Client |
||
43 | */ |
||
44 | private $httpClient; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $endPoint; |
||
50 | |||
51 | /** |
||
52 | * Initializes MailChimp |
||
53 | * |
||
54 | * @param string $apiKey Mailchimp api key |
||
55 | * @param string $storeId |
||
56 | * @param Client $httpClient The HTTP Guzzle Client |
||
57 | * @param bool $ssl Enable secure connectionss |
||
58 | */ |
||
59 | public function __construct($apiKey, $storeId, Client $httpClient, $ssl = true) |
||
60 | { |
||
61 | $this->apiKey = $apiKey; |
||
62 | $this->storeId = $storeId; |
||
63 | $this->httpClient = $httpClient; |
||
64 | |||
65 | $key = preg_split("/-/", $this->apiKey); |
||
66 | |||
67 | if ($ssl) { |
||
68 | $this->endPoint = 'https://'.$key[1].'.api.mailchimp.com/3.0/ecommerce/stores/'.$storeId; |
||
69 | } else { |
||
70 | $this->endPoint = 'http://'.$key[1].'.api.mailchimp.com/3.0/ecommerce/stores'.$storeId; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * MailChimp Requests |
||
76 | * |
||
77 | * @param string $method |
||
78 | * @param array $body |
||
79 | * @param string $resource |
||
80 | * |
||
81 | * @return \Psr\Http\Message\ResponseInterface|null |
||
82 | * @throws CustomerNotFoundException |
||
83 | * @throws ProductNotFoundException |
||
84 | * @throws \Exception |
||
85 | */ |
||
86 | public function doRequest($method, $body, $resource = '') |
||
120 | } |
||
121 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.