Completed
Push — master ( e2e30a...b18a5e )
by Konstantinos
04:00
created

MatchController::getMessages()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 7
nc 4
nop 2
crap 4
1
<?php
2
3
use Symfony\Component\Form\FormError;
4
use Symfony\Component\HttpFoundation\Request;
5
6
class MatchController extends CRUDController
7
{
8 1
    public function listAction(Request $request, Team $team = null, $type = null)
9
    {
10 1
        $qb = $this->getQueryBuilder();
11
12 1
        $currentPage = $request->query->get('page', 1);
13
14 1
        $query = $qb->sortBy('time')->reverse()
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class QueryBuilder as the method with() does only exist in the following sub-classes of QueryBuilder: MatchQueryBuilder. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
15 1
               ->with($team, $type)
16 1
               ->limit(50)->fromPage($currentPage);
17
18
19
        return array(
20 1
            "matches"     => $query->getModels(),
21 1
            "team"        => $team,
22 1
            "currentPage" => $currentPage,
23 1
            "totalPages"  => $qb->countPages()
24
    );
25
    }
26
27 1
    public function createAction(Player $me)
28
    {
29 1
        return $this->create($me);
30
    }
31
32
    public function deleteAction(Player $me, Match $match)
33
    {
34
        if (!$match->getTeamA()->isLastMatch($match)
0 ignored issues
show
Documentation introduced by
$match is of type object<Match>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
         || !$match->getTeamB()->isLastMatch($match)) {
0 ignored issues
show
Documentation introduced by
$match is of type object<Match>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
            throw new BadRequestException("You can only delete the last match of a team");
37
        }
38
39
        return $this->delete($match, $me);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    protected function getMessages($type, $name = '')
46
    {
47 1
        $messages = parent::getMessages($type, $name);
48
49
        // Don't show the match info on the successful create/edit message
50 1
        foreach ($messages as &$action) {
51 1
            foreach ($action as &$status) {
52 1
                if (isset($status['named'])) {
53 1
                    $status['named'] = $status['unnamed'];
54
                }
55
            }
56
        }
57
58 1
        return $messages;
59
    }
60
61 1
    protected function validate($form)
62
    {
63
        // Make sure that two different teams participated in a match, i.e. a team
64
        // didn't match against itself
65 1
        $firstTeam  = $form->get('first_team')->get('team')->getData();
66 1
        $secondTeam = $form->get('second_team')->get('team')->getData();
67
68 1
        if (!$firstTeam || !$secondTeam) {
69
            return;
70
        }
71
72 1
        if ($firstTeam->isSameAs($secondTeam)) {
73 1
            $message = "You can't report a match where a team played against itself!";
74 1
            $form->addError(new FormError($message));
75
        }
76 1
    }
77
}
78