Completed
Push — master ( 3efc16...d615e7 )
by Woody
12s
created

CanOrderBy::orderByAsSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Latitude\QueryBuilder\Traits;
5
6
use Iterator;
7
use Latitude\QueryBuilder\Identifier;
8
9
trait CanOrderBy
10
{
11 6
    public function orderBy(array ...$sorting): self
12
    {
13 6
        $this->orderBy = $sorting;
14 6
        return $this;
15
    }
16
17 6
    protected function orderByAsSql(Identifier $identifier): string
18
    {
19 6
        return sprintf('ORDER BY %s', $this->stringifyIterator($this->generateOrderBy($identifier)));
0 ignored issues
show
Bug introduced by Peter Gnodde
It seems like stringifyIterator() 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...
20
    }
21
22
    /**
23
     * Generate a list of ORDER BY statements.
24
     */
25 6
    protected function generateOrderBy(Identifier $identifier): Iterator
26
    {
27 6
        foreach ($this->orderBy as $sort) {
28 6
            if (empty($sort[1])) {
29 3
                yield $identifier->escapeQualified($sort[0]);
30
            } else {
31 6
                yield $identifier->escapeQualified($sort[0]) . ' ' . \strtoupper($sort[1]);
32
            }
33
        }
34 6
    }
35
36
    /**
37
     * @var array
38
     */
39
    protected $orderBy;
40
}
41