Completed
Push — master ( b7ef6a...58a002 )
by Kirill
03:12
created

ExecutionsProvider::scalar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0185
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Query;
11
12
use Illuminate\Support\Arr;
13
use RDS\Hydrogen\Query;
14
use RDS\Hydrogen\Collection\Collection;
15
16
/**
17
 * Class ExecutionsProvider
18
 * @mixin Query
19
 */
20
trait ExecutionsProvider
21
{
22
    /**
23
     * @param string ...$fields
24
     * @return object[]|iterable
25
     */
26 20
    public function get(string ...$fields): iterable
27
    {
28 20
        $processor = $this->getRepository()->getProcessor();
0 ignored issues
show
Bug introduced by
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
30 20
        return $processor->getResult($this, ...$fields);
31
    }
32
33
    /**
34
     * Get the values of a given key.
35
     *
36
     * @param string|array $value
37
     * @param string|null $key
38
     * @return Collection|iterable
39
     */
40
    public function pluck($value, $key = null): array
41
    {
42
        return $this
0 ignored issues
show
Documentation Bug introduced by
The method pluck does not exist on object<RDS\Hydrogen\Collection\Collection>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
            ->collect(...\array_filter([$value, $key]))
44
            ->pluck($value, $key)
45
            ->toArray();
46
    }
47
48
    /**
49
     * @param string $field
50
     * @param string|null $typeOf
51
     * @return mixed
52
     * @throws \LogicException
53
     */
54 10
    public function scalar(string $field, string $typeOf = null)
55
    {
56 10
        $processor = $this->getRepository()->getProcessor();
0 ignored issues
show
Bug introduced by
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
58 10
        $result = $processor->getScalarResult($this, $field);
59
60 10
        if ($typeOf !== null) {
61 10
            return $this->cast($result, $typeOf);
62
        }
63
64
        return $result;
65
    }
66
67
    /**
68
     * @param mixed $result
69
     * @param string $typeOf
70
     * @return array|\Closure|object|mixed
71
     */
72 10
    private function cast($result, string $typeOf)
73
    {
74 10
        $typeOf = \strtolower($typeOf);
75
76
        switch ($typeOf) {
77 10
            case 'callable':
78
                return function (callable $applicator = null) use ($result) {
79
                    return ($applicator ?? '\\value')($result);
80
                };
81
82 10
            case 'object':
83
                return (object)$result;
84
85 10
            case 'array':
86 10
            case 'iterable':
87
                return (array)$result;
88
89 10
            case 'string':
90
                return (string)$result;
91
        }
92
93 10
        $function = $typeOf . 'val';
94
95 10
        if (! \function_exists($function)) {
96
            throw new \InvalidArgumentException('Could not cast to type ' . $typeOf);
97
        }
98
99 10
        return $function($result);
100
    }
101
102
    /**
103
     * @param string|null $field
104
     * @return int
105
     * @throws \LogicException
106
     */
107 2
    public function count(string $field = null): int
108
    {
109 2
        if ($field === null) {
110
            $field = \array_first($this->getMetadata()->identifier);
0 ignored issues
show
Bug introduced by
It seems like getMetadata() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
111
        }
112
113
        return $this
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
114 2
            ->select('COUNT(' . $field . ') AS __count')
115 2
            ->scalar('__count', 'int');
116
    }
117
118
    /**
119
     * @param string|null $field
120
     * @return int
121
     * @throws \LogicException
122
     */
123 2
    public function sum(string $field = null): int
124
    {
125
        return $this
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
126 2
            ->select('SUM(' . $field . ') AS __sum')
127 2
            ->scalar('__sum', 'int');
128
    }
129
130
    /**
131
     * @param string|null $field
132
     * @return int
133
     * @throws \LogicException
134
     */
135 2
    public function avg(string $field = null): int
136
    {
137
        return $this
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
138 2
            ->select('AVG(' . $field . ') AS __avg')
139 2
            ->scalar('__avg', 'int');
140
    }
141
142
    /**
143
     * @param string|null $field
144
     * @return int
145
     * @throws \LogicException
146
     */
147 2
    public function max(string $field = null): int
148
    {
149
        return $this
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
150 2
            ->select('MAX(' . $field . ') AS __max')
151 2
            ->scalar('__max', 'int');
152
    }
153
154
    /**
155
     * @param string|null $field
156
     * @return int
157
     * @throws \LogicException
158
     */
159 2
    public function min(string $field = null): int
160
    {
161
        return $this
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
162 2
            ->select('MIN(' . $field . ') AS __min')
163 2
            ->scalar('__min', 'int');
164
    }
165
166
    /**
167
     * @param string ...$fields
168
     * @return Collection
169
     */
170 2
    public function collect(string ...$fields): Collection
171
    {
172 2
        return Collection::wrap($this->get(...$fields));
173
    }
174
175
    /**
176
     * @param string[] $fields
177
     * @return object|null
178
     * @throws \LogicException
179
     */
180
    public function first(string ...$fields)
181
    {
182
        return \array_first($this->get(...$fields));
183
    }
184
}
185