Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Types/HttpHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ class HttpHeader {
* @SuppressWarnings("PHPMD.StaticAccess")
*/
public static function create(array $options, array $tokens) {

$headers = empty($options['CURLOPT_HTTPHEADER']) ? [] : $options['CURLOPT_HTTPHEADER'];

$headers = empty($headers) ? [] : $headers;
$headers = is_string($headers) ? explode(',', $headers) : $headers;

$headers = array_merge(
Expand Down
2 changes: 1 addition & 1 deletion Types/HttpQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function create(array $tokens) {

$tableAlias = Table::alias($tokens);
$query = array_reduce($tokens['WHERE'], function($query, $token) use ($tableAlias) {
return $query . str_replace('"', '', str_replace('OR', '|', str_replace('AND', '&', str_replace($tableAlias . '.', '', $token['base_expr']))));
return $query . trim(str_replace('IN', '=', str_replace('"', '', str_replace('OR', '|', str_replace('AND', '&', str_replace($tableAlias . '.', '', $token['base_expr']))))), '()');
});

return preg_replace('/id\=\d*&*/', '', $query);
Expand Down
12 changes: 10 additions & 2 deletions Types/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function create(array $tokens) {

return array_reduce($tokens['WHERE'], function($carry, $token) use ($tokens, $idAlias) {
if (!is_int($carry)) return $carry;
if ($token['expr_type'] === 'colref' && $token['base_expr'] === $idAlias) return $tokens['WHERE'][$carry+2]['base_expr'];
if ($token['expr_type'] === 'colref' && $token['base_expr'] === $idAlias && !in_array($tokens['WHERE'][$carry+2]['expr_type'], array('in-list'))) return $tokens['WHERE'][$carry+2]['base_expr'];
if (!isset($tokens[$carry+1])) return '';
}, 0);
}
Expand Down Expand Up @@ -83,6 +83,14 @@ public static function column(array $tokens, MetaData $metaData) {

$idColumns = !empty($meta) ? end($meta)->getIdentifierColumnNames() : [];

return !empty($idColumns) ? end($idColumns) : 'id';
if (empty($idColumns)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all: Thank you for contributing! And sorry for being that late.

In general your bugfix is a good one. But we just need to clean up the code a bit. If you had a look at the rest of the code we tried...
... not to mutate existing variables and
... not to create/mutate variables in a branch

Example:

    if($x) {
        $var = 'value';
    } else {
        $var = 'anotherValue';
    }

turns to

$var = $x ? 'value' : 'anotherValue';

The idea is to use conditions as statements instead of expressions to ensure we don't generate side effects.

So what we need here is something like:

    if(!empty($idColumns)) return end($idColumns);

    $reservedTokens = array_filter($tokens['SELECT'], function($token) {
        return $token['expr'] === 'reserved';
    });

    $finalTokens = count($reservedTokens) > 0 ? $reservedTokens : $tokens;

    return end($finalTokens[0]['no_quotes']['parts']);

Maybe I missed a detail, but I hope it helps you to understand what I want to change here.
May you be so kind and change these few lines so I can merge the PR?

foreach ($tokens['SELECT'] as $key => $token) {
if ($token['expr_type'] == 'reserved') {
array_splice($tokens['SELECT'], $key, 1);
}
}
}

return !empty($idColumns) ? end($idColumns) : end($tokens['SELECT'][0]['no_quotes']['parts']);
}
}
6 changes: 6 additions & 0 deletions Types/SelectSingleResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public static function create(array $tokens, $content) {
HashMap::assert($tokens, 'tokens');
$tableAlias = Table::alias($tokens);

foreach ($tokens['SELECT'] as $key => $token) {
if ($token['expr_type'] == 'reserved') {
unset($tokens['SELECT'][$key]);
}
}

$attributeValueMap = array_map(function($token) use ($content, $tableAlias) {
$key = empty($token['alias']['name']) ? $token['base_expr'] : $token['alias']['name'];
$value = $content[str_replace($tableAlias . '.', '', $token['base_expr'])];
Expand Down