Response::toResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/*
4
 * This file is part of Laravel Sense.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Sense\Http\Controllers\Queries\Collect;
15
16
use Illuminate\Contracts\Support\Responsable;
17
18
class Response implements Responsable
19
{
20
    /**
21
     * @var \Cog\Laravel\Sense\Db\Query\Models\Query[]
22
     */
23
    private $queries;
24
25
    public function __construct($queries)
26
    {
27
        $this->queries = $queries;
28
    }
29
30
    public function toResponse($request)
31
    {
32
        return $request->wantsJson() ? $this->toJson() : $this->toHtml();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request->wantsJs...son() : $this->toHtml() returns the type Illuminate\View\View|array which is incompatible with the return type mandated by Illuminate\Contracts\Sup...sponsable::toResponse() of Symfony\Component\HttpFoundation\Response.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
33
    }
34
35
    private function toHtml()
36
    {
37
        return view('sense::queries.index', [
38
            'queries' => $this->queries,
39
        ]);
40
    }
41
42
    private function toJson()
43
    {
44
        return [];
45
    }
46
}
47