1 | <?php |
||
19 | class NotificationBuilder |
||
20 | { |
||
21 | /** |
||
22 | * @var MessageInterface |
||
23 | */ |
||
24 | private $message; |
||
25 | |||
26 | /** |
||
27 | * @var \ArrayIterator |
||
28 | */ |
||
29 | private $notifications; |
||
30 | |||
31 | /** |
||
32 | * @param MessageInterface $message |
||
33 | */ |
||
34 | public function __construct(MessageInterface $message) |
||
40 | |||
41 | /** |
||
42 | * @return string |
||
43 | */ |
||
44 | public function getNotification() |
||
55 | |||
56 | /** |
||
57 | * Generates number of notifications by message recipient count |
||
58 | * and notification service limitations |
||
59 | * |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function buildNotifications() |
||
87 | |||
88 | /** |
||
89 | * Returns validated and encoded message |
||
90 | * |
||
91 | * @param array $recipients |
||
92 | * @return array |
||
93 | */ |
||
94 | private function buildNotification($recipients) |
||
109 | |||
110 | /** |
||
111 | * Check if maximum size allowed for a notification payload exceeded |
||
112 | * |
||
113 | * @param string $payload |
||
114 | * @throws MalformedNotificationException |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function validatePayload($payload) |
||
134 | } |
||
135 |
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.