Skip to content

Commit

Permalink
Improve targeted regex patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshSalway committed Dec 20, 2024
1 parent 5be0d95 commit 1a1b896
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Foundation/Console/ApiInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected function addTraitToModel(string $trait, string $model)

$modified = false;

// 1. Add the top-level `use` statement if it doesn't exist
// Add the top-level `use` statement if it doesn't exist
$content = preg_replace(
'/^(namespace\s+[\w\\\\]+;\s*(?:\/\/.*\n)*)((?:use\s+[\w\\\\]+;\n)*)\s*/m',
'$1$2use ' . $trait . ";\n",
Expand All @@ -209,13 +209,14 @@ protected function addTraitToModel(string $trait, string $model)
$modified = true;
}

// 2. Add the trait usage within the class
// Add the trait usage within the class, avoiding duplicate additions
if (preg_match('/class\s+\w+\s+extends\s+\w+[A-Za-z\\\\]*\s*\{/', $content, $matches, PREG_OFFSET_CAPTURE)) {
$insertPosition = $matches[0][1] + strlen($matches[0][0]);

if (preg_match('/use\s+(.*?);/s', $content, $useMatches, PREG_OFFSET_CAPTURE, $insertPosition)) {
$traits = array_map('trim', explode(',', $useMatches[1][0]));

// Only add the trait if it doesn't already exist in the class-level use block
if (! in_array($traitBasename, $traits)) {
$traits[] = $traitBasename;
$content = substr_replace(
Expand All @@ -227,6 +228,7 @@ protected function addTraitToModel(string $trait, string $model)
$modified = true;
}
} else {
// No existing use block in the class, insert a new one
$content = substr_replace(
$content,
"\n use $traitBasename;",
Expand Down

0 comments on commit 1a1b896

Please sign in to comment.