Passed
Push — master ( 666edc...4ba5ff )
by Stephen
06:52
created

WhereBetweenSplitter::isArrayValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Builders\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
trait WhereBetweenSplitter
8
{
9
    /**
10
     * whereBetween clause extension that accepts a string value instead of array.
11
     *
12
     * @param string $column DB column
13
     * @param string|mixed $values Two 'between' values with a separator
14
     * @param string $sep separator character
15
     * @param bool $allow_where Allow for standard 'where' clause $values cannot be split
16
     * @param string $where_operator
17
     * @return WhereBetweenSplitter|Builder
18
     */
19
    public function whereBetweenSplitter(string $column, $values, string $sep = '-', bool $allow_where = true, $where_operator = '=')
20
    {
21
        // Use standard whereBetween if $values is an array
22
        if (self::isArrayValue($values)) {
23
            $this->whereBetween($column, $values);
0 ignored issues
show
Bug introduced by
The method whereBetween() does not exist on Sfneal\Builders\Traits\WhereBetweenSplitter. Did you maybe mean whereBetweenSplitter()? ( Ignorable by Annotation )

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

23
            $this->/** @scrutinizer ignore-call */ 
24
                   whereBetween($column, $values);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
        }
25
26
        // Determine if the $value is splitable
27
        elseif (! self::isSplitableValue($values, $sep) && $allow_where) {
28
            $this->where($column, $where_operator, $values);
0 ignored issues
show
Bug introduced by
It seems like where() 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

28
            $this->/** @scrutinizer ignore-call */ 
29
                   where($column, $where_operator, $values);
Loading history...
29
        }
30
31
        // $values is a string and that $sep is in $values
32
        else {
33
            $this->whereBetween($column, array_map(function ($str) {
34
                return trim($str);
35
            }, explode($sep, $values)));
36
        }
37
38
        return $this;
39
    }
40
41
    /**
42
     * Determine if the values to be split are 'splitable'.
43
     *
44
     * Value is splittable if...
45
     *  - A standard 'where' clause can be used
46
     *  - $values is not a string
47
     *  - $sep is found not in $values
48
     *
49
     * @param $values
50
     * @param string $sep
51
     * @return bool
52
     */
53
    private static function isSplitableValue($values, string $sep = '-')
54
    {
55
        return is_string($values) && inString($values, $sep);
0 ignored issues
show
Bug introduced by
The function inString was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

55
        return is_string($values) && /** @scrutinizer ignore-call */ inString($values, $sep);
Loading history...
56
    }
57
58
    /**
59
     * Determine if the values are valid for use in standard 'whereBetween' clause.
60
     *
61
     * @param $values
62
     * @return bool
63
     */
64
    private static function isArrayValue($values)
65
    {
66
        return is_array($values) && count($values) > 1;
67
    }
68
}
69