Skip to content

Commit 4aadd25

Browse files
committed
Simplify rules merging
We first sanitize the merged array and we can later use it without checking. Signed-off-by: Michal Čihař <michal@cihar.com>
1 parent bda11c8 commit 4aadd25

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

src/Utils/Formatter.php

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -212,44 +212,39 @@ protected function getDefaultFormats()
212212
private static function mergeFormats(array $formats, array $newFormats)
213213
{
214214
$added = array();
215+
$integers = array('flags', 'type');
216+
$strings = array('html', 'cli', 'function');
215217

218+
/* Sanitize the array so that we do not have to care later */
219+
foreach ($newFormats as $j => $new) {
220+
foreach ($integers as $name) {
221+
if (! isset($new[$name])) {
222+
$newFormats[$j][$name] = 0;
223+
}
224+
}
225+
foreach ($strings as $name) {
226+
if (! isset($new[$name])) {
227+
$newFormats[$j][$name] = '';
228+
}
229+
}
230+
}
231+
232+
/* Process changes to existing formats */
216233
foreach ($formats as $i => $original) {
217234
foreach ($newFormats as $j => $new) {
218-
if (isset($new['type'])
219-
&& $new['type'] === $original['type']
220-
&& (
221-
(
222-
isset($new['flags'])
223-
&& $original['flags'] === $new['flags']
224-
)
225-
|| (
226-
!isset($new['flags'])
227-
&& $original['flags'] == 0
228-
)
229-
)
235+
if ($new['type'] === $original['type']
236+
&& $original['flags'] === $new['flags']
230237
) {
231-
$formats[$i] = array(
232-
'type' => $original['type'],
233-
'flags' => isset($new['flags']) ? $new['flags'] : 0,
234-
'html' => isset($new['html']) ? $new['html'] : '',
235-
'cli' => isset($new['cli']) ? $new['cli'] : '',
236-
'function' => isset($new['function']) ? $new['function'] : '',
237-
);
238-
238+
$formats[$i] = $new;
239239
$added[] = $j;
240240
}
241241
}
242242
}
243243

244+
/* Add not already handled formats */
244245
foreach ($newFormats as $j => $new) {
245-
if (!in_array($j, $added) && isset($new['type'])) {
246-
$formats[] = array(
247-
'type' => $new['type'],
248-
'flags' => isset($new['flags']) ? $new['flags'] : 0,
249-
'html' => isset($new['html']) ? $new['html'] : '',
250-
'cli' => isset($new['cli']) ? $new['cli'] : '',
251-
'function' => isset($new['function']) ? $new['function'] : '',
252-
);
246+
if (! in_array($j, $added)) {
247+
$formats[] = $new;
253248
}
254249
}
255250

tests/Utils/FormatterTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public function mergeFormats()
5959
array(
6060
'type' => 0,
6161
'flags' => 0,
62+
'html' => '',
63+
'cli' => '',
64+
'function' => '',
6265
),
6366
),
6467
array( // overriding
@@ -69,6 +72,9 @@ public function mergeFormats()
6972
array(
7073
'type' => 0,
7174
'flags' => 0,
75+
'html' => '',
76+
'cli' => '',
77+
'function' => '',
7278
),
7379
),
7480
),

0 commit comments

Comments
 (0)