Completed
Pull Request — master (#161)
by
unknown
13:26
created

SortParameter::__construct()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 8.8571
cc 5
eloc 7
nc 16
nop 3
crap 5
1
<?php namespace Neomerx\JsonApi\Encoder\Parameters;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \Neomerx\JsonApi\Factories\Exceptions;
20
use \Neomerx\JsonApi\Contracts\Encoder\Parameters\SortParameterInterface;
21
22
/**
23
 * @package Neomerx\JsonApi
24
 */
25
class SortParameter implements SortParameterInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $sortField;
31
32
    /**
33
     * @var null || string
34
     */
35
    private $sortRelationshipAttribute;
36
    
37
    /**
38
     * @var bool
39
     */
40
    private $isAscending;
41
42
43
    /**
44
     * @param $sortField
45
     * @param $isAscending
46
     * @param null $sortRelationAttribute
0 ignored issues
show
Documentation introduced by
There is no parameter named $sortRelationAttribute. Did you maybe mean $sortRelationshipAttribute?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
47
     */
48 17
    public function __construct($sortField, $isAscending, $sortRelationshipAttribute=null)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $sortRelationshipAttribute exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
49
    {
50 17
        is_string($sortField) === true ?: Exceptions::throwInvalidArgument('sortField', $sortField);
51 17
        is_string($sortRelationshipAttribute) === true || is_null($sortRelationshipAttribute) === true ?: Exceptions::throwInvalidArgument('sortRelationshipAttribute', $sortRelationshipAttribute);
52 17
        is_bool($isAscending) === true ?: Exceptions::throwInvalidArgument('isAscending', $isAscending);
53
54 17
        $this->sortField   = $sortField;
55 17
        $this->sortRelationshipAttribute = $sortRelationshipAttribute;
56 17
        $this->isAscending = $isAscending;
57 17
    }
58
59
    /**
60
     * @return string
61
     */
62 1
    public function __toString()
63
    {
64 1
        $prefix = $this->isAscending() ? '' : '-';
65
66 1
        return $prefix . $this->getField();
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 5
    public function getField()
73
    {
74 5
        return $this->sortField;
75
    }
76
77
78
    /**
79
     * @return null || string
80
     */
81
    public function getRelationshipAttribute()
82
    {
83
        return $this->sortRelationshipAttribute;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 3
    public function isAscending()
90
    {
91 3
        return $this->isAscending;
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 2
    public function isDescending()
98
    {
99 2
        return !$this->isAscending;
100
    }
101
}
102