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

CanOrderBy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 32
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A orderBy() 0 5 1
A orderByAsSql() 0 4 1
A generateOrderBy() 0 10 3
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