Skip to content

Commit bd466ed

Browse files
authored
VACMS-20371: Clean up extensions (#20730)
* VACMS-20371: Clean up extensions * VACMS-20371: Adds a couple more guard clauses. * VACMS-20371: Adds more defensive code. * VACMS-20371: Adds a different defense in light of PHPStan * VACMS-20371: Adds case insensitivity to the patterns for split numbers
1 parent ae6b6ff commit bd466ed

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

docroot/modules/custom/va_gov_batch/src/cbo_scripts/RemoveNonNumericalCharactersFromExtensions.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,21 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
7272
if (empty($original_extension)) {
7373
return "No extension found for paragraph id $item";
7474
}
75+
$phone_parent_field_name = $phone_paragraph->get('parent_field_name')->value;
76+
// Don't try to change 'field_phone_numbers_paragraph'.
77+
if ($phone_parent_field_name === 'field_phone_numbers_paragraph') {
78+
return "The phone data from 'field_phone_numbers_paragraph' '$item' on 'health_care_local_health_service' has already been migrated to the Service location paragraph previously. This is a vestigial field that is unused.";
79+
}
7580
$number_only_extension = $this->replaceNonNumerals($original_extension);
7681
if ($original_extension === $number_only_extension) {
77-
return "No change to extension $original_extension in paragraph id $item";
82+
return "No change to extension '$original_extension' in paragraph id $item";
7883
}
7984
$phone_paragraph->set(name: 'field_phone_extension', value: $number_only_extension);
8085
$phone_paragraph->save();
8186
return "Extension updated for paragraph id $item from '$original_extension' to '$number_only_extension'";
8287
}
8388
catch (\Exception $e) {
84-
$message = "Exception during update of paragraph id $item with extension: $original_extension";
89+
$message = "Exception during update of paragraph id $item with extension: '$original_extension'";
8590
return $message;
8691
}
8792

@@ -98,15 +103,15 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
98103
*/
99104
public static function replaceNonNumerals(string $extension): string {
100105

101-
$pattern_to_ignore = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/';
106+
$pattern_to_ignore = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/i';
102107

103108
// If the extension contains two numbers separate numbers, don't change it.
104109
if (preg_match($pattern_to_ignore, $extension) > 0) {
105110
return $extension;
106111
}
107112

108113
// Remove non-numerical characters from the extension.
109-
$just_numbers = preg_replace('/[^0-9]/', '', $extension);
114+
$just_numbers = trim(preg_replace('/[^0-9]/', '', $extension));
110115

111116
return $just_numbers;
112117
}

docroot/modules/custom/va_gov_batch/src/cbo_scripts/SplitExtensionWithTwoNumbers.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
8686
// Do the extension work.
8787
$original_extension = $phone_paragraph->get('field_phone_extension')->value;
8888
$separated_extensions = $this->splitExtensions($original_extension);
89+
// There is something amiss with the extensions.
90+
if (empty($separated_extensions)) {
91+
return "Either no change is necessary for paragraph $item or '$original_extension' cannot be successfully separated.";
92+
}
8993
$original_label = $phone_paragraph->get('field_phone_label')->value;
9094

9195
try {
@@ -96,7 +100,7 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
96100
}
97101
$phone_paragraph->set(name: 'field_phone_extension', value: $separated_extensions[0]);
98102
$phone_paragraph->save();
99-
$message = "1st extension for paragraph $item changed from '$original_extension' to $separated_extensions[0]' .";
103+
$message = "1st extension for paragraph $item changed from '$original_extension' to '$separated_extensions[0]'. ";
100104
}
101105

102106
// Create the second extension and phone.
@@ -116,7 +120,7 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
116120
$phone_parent_paragraph = \Drupal::entityTypeManager()->getStorage('paragraph')->load($phone_parent_id);
117121
$phone_parent_paragraph->get($phone_parent_field_name)->appendItem($second_phone);
118122
$phone_parent_paragraph->save();
119-
$message .= "2nd extension created for paragraph $second_phone_id from '$original_extension' to $separated_extensions[1]'." . PHP_EOL;
123+
$message .= "2nd extension created for paragraph $second_phone_id from '$original_extension' to '$separated_extensions[1]'.";
120124
}
121125
}
122126
catch (\Exception $e) {
@@ -133,20 +137,25 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
133137
* @param string $dual_extension
134138
* The two-part extension.
135139
*
136-
* @return array
140+
* @return array|bool
137141
* Array with two extensions (or empty).
138142
* E.g. '2132,2995' becomes ['2132','2995']
139143
*/
140144
public static function splitExtensions(string $dual_extension): array {
141-
if (!preg_match('/[^0-9]/', $dual_extension)) {
145+
$pattern = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/i';
146+
if (!preg_match($pattern, $dual_extension)) {
142147
return [];
143148
}
144149
$first_extension = [];
145150
preg_match('/^\d+/', $dual_extension, $first_extension);
146151
$second_extension = [];
147152
preg_match('/\d+$/', $dual_extension, $second_extension);
153+
// If either of these are empty, we want to bail out of this.
154+
if (empty($first_extension[0]) or empty($second_extension[0])) {
155+
return [];
156+
}
148157

149-
return [$first_extension[0], $second_extension[0]];
158+
return [trim($first_extension[0]), trim($second_extension[0])];
150159
}
151160

152161
}

0 commit comments

Comments
 (0)