Skip to content

Commit

Permalink
Improved preg_match handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LaravelFreelancerNL committed Sep 18, 2024
1 parent 45fdc88 commit 557a931
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/Query/Concerns/CompilesJoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public function extractTableAndAlias(Builder $query, $join): array
{
if ($join->table instanceof Expression) {
$tableParts = [];
preg_match("/(^.*) as (.*?)$/", (string) $join->table->getValue($query->grammar), $tableParts);
$table = $tableParts[1];
$alias = $tableParts[2];

$query->registerTableAlias($join->table, $alias);
if (preg_match("/(^.*) as (.*?)$/", (string) $join->table->getValue($query->grammar), $tableParts)) {
$table = $tableParts[1];
$alias = $tableParts[2];

return [$table, $alias];
$query->registerTableAlias($join->table, $alias);

return [$table, $alias];
}
}

$table = (string) $this->wrapTable($join->table);
Expand Down
11 changes: 6 additions & 5 deletions src/Query/Concerns/HandlesAliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ public function registerTableAlias(string|Expression $table, string $alias = nul
}

/** @phpstan-ignore-next-line */
if ($alias == null && stripos($table, ' as ') !== false) {
if ($alias == null && is_string($table) && stripos($table, ' as ') !== false) {
$tableParts = [];
/** @phpstan-ignore-next-line */
preg_match("/(^.*) as (.*?)$/", $table, $tableParts);
$table = $tableParts[1];
$alias = $tableParts[2];

if (preg_match("/(^.*) as (.*?)$/", $table, $tableParts)) {
$table = $tableParts[1];
$alias = $tableParts[2];
}
}

if ($alias == null) {
Expand Down

0 comments on commit 557a931

Please sign in to comment.