Skip to content

Commit

Permalink
VACMS-20371: Clean up extensions (#20730)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
omahane authored Mar 3, 2025
1 parent ae6b6ff commit bd466ed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,21 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
if (empty($original_extension)) {
return "No extension found for paragraph id $item";
}
$phone_parent_field_name = $phone_paragraph->get('parent_field_name')->value;
// Don't try to change 'field_phone_numbers_paragraph'.
if ($phone_parent_field_name === 'field_phone_numbers_paragraph') {
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.";
}
$number_only_extension = $this->replaceNonNumerals($original_extension);
if ($original_extension === $number_only_extension) {
return "No change to extension $original_extension in paragraph id $item";
return "No change to extension '$original_extension' in paragraph id $item";
}
$phone_paragraph->set(name: 'field_phone_extension', value: $number_only_extension);
$phone_paragraph->save();
return "Extension updated for paragraph id $item from '$original_extension' to '$number_only_extension'";
}
catch (\Exception $e) {
$message = "Exception during update of paragraph id $item with extension: $original_extension";
$message = "Exception during update of paragraph id $item with extension: '$original_extension'";
return $message;
}

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

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

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

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

return $just_numbers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
// Do the extension work.
$original_extension = $phone_paragraph->get('field_phone_extension')->value;
$separated_extensions = $this->splitExtensions($original_extension);
// There is something amiss with the extensions.
if (empty($separated_extensions)) {
return "Either no change is necessary for paragraph $item or '$original_extension' cannot be successfully separated.";
}
$original_label = $phone_paragraph->get('field_phone_label')->value;

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

// Create the second extension and phone.
Expand All @@ -116,7 +120,7 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
$phone_parent_paragraph = \Drupal::entityTypeManager()->getStorage('paragraph')->load($phone_parent_id);
$phone_parent_paragraph->get($phone_parent_field_name)->appendItem($second_phone);
$phone_parent_paragraph->save();
$message .= "2nd extension created for paragraph $second_phone_id from '$original_extension' to $separated_extensions[1]'." . PHP_EOL;
$message .= "2nd extension created for paragraph $second_phone_id from '$original_extension' to '$separated_extensions[1]'.";
}
}
catch (\Exception $e) {
Expand All @@ -133,20 +137,25 @@ public function processOne(string $key, mixed $item, array &$sandbox): string {
* @param string $dual_extension
* The two-part extension.
*
* @return array
* @return array|bool
* Array with two extensions (or empty).
* E.g. '2132,2995' becomes ['2132','2995']
*/
public static function splitExtensions(string $dual_extension): array {
if (!preg_match('/[^0-9]/', $dual_extension)) {
$pattern = '/(^\d+[,|;]\s?\d+)|(^\d+\sor\s\d+)|(^\d+\/\d+)|(^\d+\sthen\s\d+)/i';
if (!preg_match($pattern, $dual_extension)) {
return [];
}
$first_extension = [];
preg_match('/^\d+/', $dual_extension, $first_extension);
$second_extension = [];
preg_match('/\d+$/', $dual_extension, $second_extension);
// If either of these are empty, we want to bail out of this.
if (empty($first_extension[0]) or empty($second_extension[0])) {
return [];
}

return [$first_extension[0], $second_extension[0]];
return [trim($first_extension[0]), trim($second_extension[0])];
}

}

0 comments on commit bd466ed

Please sign in to comment.