Passed
Push — master ( 3d413f...381e35 )
by Chito
08:34
created

LampagerPaginator::orderBy()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 2
crap 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 11 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
// @codeCoverageIgnoreStart
4
App::uses('Model', 'Model');
5
App::uses('LampagerTransformer', 'Lampager.Model');
6
// @codeCoverageIgnoreEnd
7
8
use Lampager\Paginator as BasePaginator;
9
use Lampager\Query\Order;
10
11
class LampagerPaginator extends BasePaginator
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /** @var Model */
14
    public $builder;
15
16
    /** @var array */
17
    public $query;
18
19
    /** @var LampagerTransformer */
20
    public $transformer;
21
22 20
    public function __construct(Model $builder, array $query)
23
    {
24 20
        $this->builder = $builder;
25 20
        $this->fromArray($query);
26 20
        $this->transformer = new LampagerTransformer($this);
27 20
    }
28
29
    /**
30
     * @param  Model             $builder Model.
31
     * @param  array             $query   Query.
32
     * @return static
33
     */
34 20
    public static function create(Model $builder, array $query)
35
    {
36 20
        return new static($builder, $query);
37
    }
38
39
    /**
40
     * Add cursor parameter name for ORDER BY statement.
41
     *
42
     * @param  string|int $column
43
     * @param  string     $order
44
     * @return $this
45
     */
46 20
    public function orderBy($column, $order = Order::ASC)
47
    {
48 20
        if (is_int($column)) {
49 2
            list($column, $order) = explode(' ', $order) + [1 => 'ASC'];
50
        }
51 20
        if (strpos($column, '.') === false) {
52 2
            $column = "{$this->builder->alias}.{$column}";
53
        }
54 20
        return parent::orderBy($column, strtolower($order));
55
    }
56
57
    /**
58
     * Define options from an associative array.
59
     *
60
     * @param  (bool|int|string[])[] $options
61
     * @return $this
62
     */
63 20
    public function fromArray(array $options)
64
    {
65
        // Not supported in CakePHP 2 version
66 20
        unset($options['orders']);
67
68
        // Merge with existing query
69 20
        $this->query = array_replace_recursive($this->query ?: [], $options);
70
71 20
        if (isset($options['order'])) {
72 20
            foreach ($options['order'] as $column => $order) {
0 ignored issues
show
Bug introduced by
The expression $options['order'] of type boolean|integer|array<integer,string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
73 20
                $this->orderBy($column, $order);
74
            }
75
        }
76
77 20
        return parent::fromArray($options);
0 ignored issues
show
Documentation introduced by
$options is of type array<integer|string,boo...integer,string>|null"}>, but the function expects a array<integer,boolean|in...array<integer,string>>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
    }
79
}
80