Completed
Push — master ( 2d5c6f...ea456d )
by Alexander
06:30
created

Responder::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Flugg\Responder;
4
5
use Flugg\Responder\Contracts\Responder as ResponderContract;
6
use Flugg\Responder\Factories\ResponseFactory;
7
use Illuminate\Http\JsonResponse;
8
9
/**
10
 * The responder service. This class is responsible for generating JSON API responses.
11
 * It can also transform and serialize data using Fractal behind the scenes.
12
 *
13
 * @package Laravel Responder
14
 * @author  Alexander Tømmerås <[email protected]>
15
 * @license The MIT License
16
 */
17
class Responder implements ResponderContract
18
{
19
    /**
20
     *
21
     *
22
     * @var ResponseFactory
23
     */
24
    protected $successFactory;
25
26
    /**
27
     *
28
     *
29
     * @var ResponseFactory
30
     */
31
    protected $errorFactory;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param ResponseFactory $successFactory
37
     * @param ResponseFactory $errorFactory
38
     */
39
    public function __construct( ResponseFactory $successFactory, ResponseFactory $errorFactory )
40
    {
41
        $this->successFactory = $successFactory;
42
        $this->errorFactory = $errorFactory;
43
    }
44
45
    /**
46
     * Generate a successful JSON response.
47
     *
48
     * @param  mixed $data
49
     * @param  int   $statusCode
50
     * @param  array $meta
51
     * @return JsonResponse
52
     */
53
    public function success( $data = null, $statusCode = 200, array $meta = [ ] ):JsonResponse
54
    {
55
        if ( is_integer( $data ) ) {
56
            list( $data, $statusCode, $meta ) = [ null, $data, $statusCode ];
57
        } elseif ( is_array( $statusCode ) ) {
58
            list( $statusCode, $meta ) = [ 200, $statusCode ];
59
        }
60
61
        return $this->successFactory->make( $data, $statusCode, $meta );
0 ignored issues
show
Unused Code introduced by
The call to ResponseFactory::make() has too many arguments starting with $meta.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
    }
63
64
    /**
65
     * Generate an unsuccessful JSON response.
66
     *
67
     * @param  string $errorCode
68
     * @param  int    $statusCode
69
     * @param  mixed  $message
70
     * @return JsonResponse
71
     */
72
    public function error( string $errorCode, int $statusCode = 500, $message = null ):JsonResponse
73
    {
74
        return $this->errorFactory->make( $errorCode, $statusCode, $message );
0 ignored issues
show
Documentation introduced by
$errorCode is of type string, but the function expects a array.

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...
Unused Code introduced by
The call to ResponseFactory::make() has too many arguments starting with $message.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
    }
76
}