Passed
Push — master ( 4f50ad...2698b3 )
by Michael
02:21
created

RelatedPlusHelpers::getTableWithAlias()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Blasttech\EloquentRelatedPlus;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\Relation;
8
9
/**
10
 * Class RelatedPlusHelpers
11
 *
12
 * @property array order_fields
13
 * @property array order_defaults
14
 * @property array order_relations
15
 * @property array order_with
16
 * @property array search_fields
17
 * @property string connection
18
 */
19
class RelatedPlusHelpers
20
{
21
    /**
22
     * Return the sql for a query with the bindings replaced with the binding values
23
     *
24
     * @param Builder $builder
25
     * @return string
26
     */
27
    public static function toSqlWithBindings($builder)
28
    {
29
        $replacements = array_map('addslashes', $builder->getBindings());
0 ignored issues
show
Bug introduced by
It seems like $builder->getBindings() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $replacements = array_map('addslashes', /** @scrutinizer ignore-type */ $builder->getBindings());
Loading history...
30
        $sql = $builder->toSql();
31
32
        foreach ($replacements as &$replacement) {
33
            if (!is_numeric($replacement)) {
34
                $replacement = '"' . $replacement . '"';
35
            }
36
        }
37
38
        return preg_replace_callback(
39
            '/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/',
40
            function () use (&$replacements) {
41
                return array_shift($replacements);
42
            },
43
            $sql
44
        );
45
    }
46
47
    /**
48
     * Get table name from column name
49
     *
50
     * @param string $column
51
     * @return string
52
     */
53
    public static function getTableFromColumn($column)
54
    {
55
        $periodPos = strpos($column, '.');
56
57
        return ($periodPos !== false ? substr($column, 0, $periodPos) : $column);
0 ignored issues
show
introduced by
The condition $periodPos !== false can never be false.
Loading history...
58
    }
59
60
    /**
61
     * Remove any global scopes which contain $scopeName in their name
62
     *
63
     * @param RelatedPlusTrait|Model $model
64
     * @param Builder $query
65
     * @param string $scopeName
66
     * @return Relation|Builder
67
     */
68
    public static function removeGlobalScopes($model, $query, $scopeName)
69
    {
70
        $query->withoutGlobalScopes(collect($model->getGlobalScopes())->keys()->filter(function (
0 ignored issues
show
Bug introduced by
It seems like getGlobalScopes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $query->withoutGlobalScopes(collect($model->/** @scrutinizer ignore-call */ getGlobalScopes())->keys()->filter(function (
Loading history...
71
            $value
72
        ) use ($scopeName) {
73
            return stripos($value, $scopeName) !== false;
74
        })->toArray());
75
76
        return $query;
77
    }
78
}
79