ArticleController::category()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Article;
6
use App\Models\ArticleCategory;
7
use Illuminate\Http\Response;
8
use Laravel\Lumen\Routing\Controller as BaseController;
9
10
/**
11
 * Class ArticleController.
12
 */
13
class ArticleController extends BaseController
14
{
15
    /**
16
     * Render a specific view of Article set.
17
     *
18
     * @param string $countryId
19
     * @param string $articleCategory
20
     *
21
     * @return Response
22
     */
23
    public function many(string $countryId, string $articleCategory): Response
24
    {
25
        $category = ArticleCategory::find(strstr(($articleCategory =
26
            str_replace('.html', '', $articleCategory)), '_', true));
27
28
        $categoryPage = strstr(strrev($articleCategory), '_', true);
29
30
        return $articleCategory == 'front' ? $this->front() : $this->category($countryId, $category, $categoryPage,
31
            $categoryPage == 1 ? 0 : (10 * ($categoryPage - 1)));
32
    }
33
34
    /**
35
     * Render the Front Page of the Articles Page.
36
     *
37
     * @return Response
38
     */
39
    protected function front(): Response
40
    {
41
        return response(view('habbo-web-news.articles-front', ['set' => Article::orderBy('id', 'DESC')->limit(10)->get()]));
42
    }
43
44
    /**
45
     * Render a specific Category Articles Page.
46
     *
47
     * @TODO: Proper Way to use Country ID
48
     *
49
     * @param string          $countryId
50
     * @param ArticleCategory $category
51
     * @param int             $categoryPage
52
     * @param int             $start
53
     *
54
     * @return Response
55
     */
56
    protected function category(string $countryId, ArticleCategory $category, int $categoryPage, int $start): Response
0 ignored issues
show
Unused Code introduced by
The parameter $countryId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        $articles = Article::where('id', '>=', $start)->limit(10)->orderBy('id', 'DESC')->get()->filter(function ($item) use ($category) {
59
            return $category->name == 'all' || in_array($category, $item->categories);
60
        });
61
62
        return response(view('habbo-web-news.articles-category', [
63
            'category' => $category, 'page' => $categoryPage, 'categories' => ArticleCategory::all(), 'articles' => $articles,
64
        ]));
65
    }
66
67
    /**
68
     * Render a specific view of a specific Article.
69
     *
70
     * @TODO: Proper Way to use Country ID
71
     *
72
     * @param string $countryId
73
     * @param string $articleName
74
     *
75
     * @return Response
76
     */
77
    public function one(string $countryId, string $articleName): Response
0 ignored issues
show
Unused Code introduced by
The parameter $countryId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        if (($article = Article::find(strstr($articleName, '_', true))) == null) {
80
            return response()->json(null, 404);
0 ignored issues
show
Bug introduced by
The method json does only exist in Laravel\Lumen\Http\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
81
        }
82
83
        $related = ($latest = Article::all())->filter(function ($item) use ($article) {
84
            return in_array($article->categories[0], $item->categories);
85
        });
86
87
        return response(view('habbo-web-news.articles-view',
88
            ['article' => $article, 'latest' => $latest->slice(0, 5), 'related' => $related->slice(0, 5)]
89
        ));
90
    }
91
92
    /**
93
     * Get All Habbo Articles as XML/RSS.
94
     *
95
     * @return Response
96
     */
97
    public function getRss()
98
    {
99
        return response(view('habbo-web-pages.habbo-rss', ['articles' => Article::limit(20)->get()]))->header('Content-Type', 'text/xml');
0 ignored issues
show
Bug introduced by
The method header does only exist in Illuminate\Http\Response, but not in Laravel\Lumen\Http\ResponseFactory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
    }
101
}
102