Test Failed
Branch master (1557a3)
by Adam
08:20
created

Predictions::filter()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Services\Skills;
4
5
use Coyote\Repositories\Contracts\GuestRepositoryInterface as GuestRepository;
6
use Coyote\Repositories\Contracts\TagRepositoryInterface as TagRepository;
7
use Illuminate\Http\Request;
8
use Coyote\Repositories\Contracts\PageRepositoryInterface as PageRepository;
9
10
class Predictions
11
{
12
    /**
13
     * @var Request
14
     */
15
    protected $request;
16
17
    /**
18
     * @var PageRepository
19
     */
20
    protected $page;
21
22
    /**
23
     * @var GuestRepository
24
     */
25
    protected $guest;
26
27
    /**
28
     * @var TagRepository
29
     */
30
    protected $tag;
31
32
    /**
33
     * @param Request $request
34
     * @param PageRepository $page
35
     * @param GuestRepository $guest
36
     * @param TagRepository $tag
37
     */
38
    public function __construct(Request $request, PageRepository $page, GuestRepository $guest, TagRepository $tag)
39
    {
40
        $this->request = $request;
41
        $this->page = $page;
42
        $this->guest = $guest;
43
        $this->tag = $tag;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function getTags()
50
    {
51
        if (!($tags = $this->getRefererTags())) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $tags is correct as $this->getRefererTags() targeting Coyote\Services\Skills\P...tions::getRefererTags() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
52
            $tags = $this->getUserPopularTags();
53
        }
54
55
        return $tags;
56
    }
57
58
    /**
59
     * @return \Coyote\Tag[]|null
60
     */
61
    private function getRefererTags()
62
    {
63
        $referer = filter_var($this->request->headers->get('referer'), FILTER_SANITIZE_URL);
64
        if (!$referer) {
65
            return null;
66
        }
67
68
        $path = parse_url(urldecode($referer), PHP_URL_PATH);
69
        if (!$path) {
70
            return null;
71
        }
72
73
        $page = $this->page->findByPath($path);
74
75
        if (!$page || !$page->tags) {
0 ignored issues
show
introduced by
The condition ! $page || ! $page->tags can never be false.
Loading history...
76
            return null;
77
        }
78
79
        return $this->tag->getCategorizedTags($page->tags) ?? null;
80
    }
81
82
    /**
83
     * @return \Coyote\Tag[]|null
84
     */
85
    private function getUserPopularTags()
86
    {
87
        /** @var \Coyote\Guest $guest */
88
        $guest = $this->guest->find($this->request->session()->get('guest_id'));
89
90
        if (empty($guest) || empty($guest->interests)) {
91
            return null;
92
        }
93
94
        $ratio = $guest->interests['ratio'];
95
        arsort($ratio);
96
97
        // get only five top tags
98
        $result = $this->tag->getCategorizedTags(array_slice(array_keys($ratio), 0, 4));
99
100
        if (!$result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type Coyote\Tag[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
101
            return null;
102
        }
103
104
        // only one tag please...
105
        return collect()->push($result->random());
0 ignored issues
show
Bug Best Practice introduced by
The expression return collect()->push($result->random()) returns the type Illuminate\Support\Collection which is incompatible with the documented return type null|Coyote\Tag[].
Loading history...
106
    }
107
}
108