Completed
Pull Request — master (#186)
by Vladimir
05:50 queued 02:55
created

NewsController::listAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 3
1
<?php
2
3
use Symfony\Component\HttpFoundation\Request;
4
5
class NewsController extends CRUDController
6
{
7
    public function showAction(Player $me, News $article)
8
    {
9
        if ($article->isDraft() && (!$me->isValid() || !$me->hasPermission(News::EDIT_PERMISSION))) {
10
            throw new ForbiddenException('You do not have permission to view draft posts.');
11
        }
12
13
        return [
14
            'article' => $article,
15
            'categories' => $this->getCategories(),
16
        ];
17
    }
18
19
    public function listAction(Request $request, Player $me, NewsCategory $category = null)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
20
    {
21
        $currentPage = $this->getCurrentPage();
22
        $qb = $this->getQueryBuilder();
23
24
        $news = $qb
0 ignored issues
show
Bug introduced by
The method orderBy does only exist in QueryBuilderFlex, but not in QueryBuilder.

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...
25
            ->orderBy('created', 'DESC')
26
            ->limit(5)
27
            ->fromPage($currentPage)
28
        ;
29
30
        if ($category !== null) {
31
            $news->where('category', '=', $category->getId());
32
        }
33
34
        if (!$me->isValid() || !$me->hasPermission(News::CREATE_PERMISSION)) {
35
            $news->whereNot('is_draft', '=', true);
36
        }
37
38
        return [
39
            'news'        => $news->getModels(true),
40
            'categories'  => $this->getCategories(),
41
            'category'    => $category,
42
            'currentPage' => $currentPage,
43
            'totalPages'  => $qb->countPages(),
44
        ];
45
    }
46
47
    public function createAction(Player $me)
48
    {
49
        return $this->create($me);
50
    }
51
52
    public function editAction(Player $me, News $article)
53
    {
54
        return $this->edit($article, $me, "article");
55
    }
56
57
    public function deleteAction(Player $me, News $article)
58
    {
59
        return $this->delete($article, $me);
60
    }
61
62
    private function getCategories()
63
    {
64
        return NewsCategory::getQueryBuilder()
65
            ->orderBy('name')
66
            ->getModels(true)
67
        ;
68
    }
69
}
70