Passed
Push — master ( 148695...692a71 )
by Gabriel
04:05 queued 10s
created

HasResponseTrait::payload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
ccs 4
cts 4
cp 1
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Nip\Controllers\Traits;
4
5
use Nip\Controllers\Response\ResponseFactory;
6
use Nip\Controllers\Response\ResponsePayload;
7
use Nip\Controllers\Response\ResponsePayloadTransformer;
8
use Nip\Http\Response\Response;
9
10
/**
11
 * Trait HasResponseTrait
12
 * @package Nip\Controllers\Traits
13
 */
14
trait HasResponseTrait
15
{
16
    use \Nip\Http\Response\ResponseAwareTrait;
17
18
    protected $responseFactory = null;
19
    protected $responsePayload = null;
20
21
    /**
22
     * @return Response
23
     */
24 1
    public function newResponse()
25
    {
26 1
        return ResponsePayloadTransformer::make($this, $this->payload());
0 ignored issues
show
Bug introduced by
$this->payload() of type null is incompatible with the type Nip\Controllers\Response\ResponsePayload expected by parameter $payload of Nip\Controllers\Response...loadTransformer::make(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        return ResponsePayloadTransformer::make($this, /** @scrutinizer ignore-type */ $this->payload());
Loading history...
Bug introduced by
Are you sure the usage of $this->payload() targeting Nip\Controllers\Traits\HasResponseTrait::payload() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
27
    }
28
29
    /**
30
     * @return null
31
     */
32
    protected function getResponseFactory()
33
    {
34
        if ($this->responseFactory === null) {
35
            $this->responseFactory = new ResponseFactory();
36
        }
37
38
        return $this->responseFactory;
39
    }
40
41
    /**
42
     * @return null
43
     */
44 1
    public function payload()
45
    {
46 1
        if ($this->responsePayload === null) {
47 1
            $this->responsePayload = $this->generateResponsePayload();
48
        }
49
50 1
        return $this->responsePayload;
51
    }
52
53
    /**
54
     * @return ResponsePayload
55
     */
56 1
    protected function generateResponsePayload()
57
    {
58 1
        $payload = new ResponsePayload();
59 1
        if (method_exists($this, 'getView')) {
60
            $payload->withDefaultFormat('view');
61
        }
62 1
        return $payload;
63
    }
64
}
65