Completed
Push — master ( c26f0f...c7af1e )
by Konstantinos
18:52
created

ConversationSubscriber::onTeamMembershipChange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 2
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
c 3
b 0
f 2
cc 2
eloc 4
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * This file contains a class that responds to events
4
 *
5
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
6
 */
7
8
namespace BZIon\Event;
9
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * An event subscriber for events related to conversations
14
 */
15
class ConversationSubscriber implements EventSubscriberInterface
16
{
17
    /**
18
     * Returns all the events that this subscriber handles, and which method
19
     * handles each one
20
     *
21
     * @return array
22
     */
23
    public static function getSubscribedEvents()
24
    {
25
        return array(
26
            'team.abandon' => array(
27
                array('onTeamMembershipChange'),
28
                array('onTeamLeave')
29
            ),
30
            'team.kick' => array(
31
                array('onTeamMembershipChange'),
32
                array('onTeamLeave')
33
            )
34
        );
35
    }
36
37
    /**
38
     * Called every time a member is added/removed from a team
39
     *
40
     * @param TeamAbandonEvent|TeamJoinEvent|TeamKickEvent $event The event
41
     * @param string $type The type of the event
42
     */
43
    public function onTeamMembershipChange(Event $event, $type)
44
    {
45
        $query = \Conversation::getQueryBuilder()->forTeam($event->getTeam());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class BZIon\Event\Event as the method getTeam() does only exist in the following sub-classes of BZIon\Event\Event: BZIon\Event\TeamAbandonEvent, BZIon\Event\TeamDeleteEvent, BZIon\Event\TeamJoinEvent, BZIon\Event\TeamKickEvent, BZIon\Event\TeamLeaderChangeEvent. 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...
46
47
        foreach ($query->getModels() as $conversation) {
48
            \ConversationEvent::storeEvent($conversation->getId(), $event, $type);
49
        }
50
    }
51
52
    /**
53
     * When a player leaves a team, remove them from every conversation that
54
     * includes that team
55
     *
56
     * @param TeamAbandonEvent|TeamKickEvent $event The event
57
     */
58
    public function onTeamLeave(Event $event)
59
    {
60
        // We don't need to check which conversations include the player; a
61
        // player_conversations entry will have `distinct` set to 0 only if the
62
        // player belongs to a conversation because they are a member of this
63
        // team
64
        \Database::getInstance()->execute(
65
            "DELETE FROM `player_conversations`
66
                WHERE player = ?
67
                AND `distinct` = 0", array($event->getPlayer()->getId())
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class BZIon\Event\Event as the method getPlayer() does only exist in the following sub-classes of BZIon\Event\Event: BZIon\Event\ConversationRenameEvent, BZIon\Event\TeamAbandonEvent, BZIon\Event\TeamJoinEvent, BZIon\Event\TeamKickEvent, BZIon\Event\WelcomeEvent. 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...
68
        );
69
    }
70
}
71