Completed
Pull Request — master (#95)
by
unknown
05:41
created

Test::onEndMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace eXpansion\Bundle\Acme\Plugins;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyMap;
6
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceMpLegacyMaplist;
7
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
8
use eXpansion\Framework\Core\Helpers\Time;
9
use eXpansion\Framework\Core\Model\UserGroups\Group;
10
use eXpansion\Framework\Core\Plugins\Gui\ManialinkFactory;
11
use eXpansion\Framework\Core\Plugins\StatusAwarePluginInterface;
12
use eXpansion\Framework\Core\Services\Console;
13
use Maniaplanet\DedicatedServer\Connection;
14
use Maniaplanet\DedicatedServer\Structures\Map;
15
16
class Test implements ListenerInterfaceMpLegacyMap, ListenerInterfaceExpTimer, StatusAwarePluginInterface
17
{
18
19
    public $memoryMsg;
20
    /** @var Connection */
21
    protected $connection;
22
23
    /** @var Console */
24
    protected $console;
25
26
    /** @var Time */
27
    protected $time;
28
29
    /** @var float|int */
30
    private $previousMemoryValue = 0;
31
    private $startMemValue = 0;
32
33
    /**
34
     * @var ManialinkFactory
35
     */
36
    private $mlFactory;
37
    /**
38
     * @var Group
39
     */
40
    private $players;
41
42
    function __construct(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
        Connection $connection,
44
        Console $console,
45
        Time $time,
46
        ManialinkFactory $mlFactory,
47
        Group $players
48
    ) {
49
        $this->connection = $connection;
50
        $this->console = $console;
51
        $this->time = $time;
52
        $this->mlFactory = $mlFactory;
53
        $this->players = $players;
54
        $this->startMemValue = memory_get_usage(true);
55
    }
56
57
    public function onBeginMap(Map $map)
58
    {
59
        $this->console->writeln('$0f0Begin Map: $fff'.$map->name);
60
    }
61
62
    public function onEndMap(Map $map)
63
    {
64
        $this->console->writeln('$0f0End Map: $fff'.$map->name);
65
    }
66
67
    public function onPreLoop()
68
    {
69
        // do nothing
70
    }
71
72
    public function onPostLoop()
73
    {
74
        // do nothing
75
    }
76
77
    public function onEverySecond()
78
    {
79
        $mem = memory_get_usage(false);
80
81
        if ($this->previousMemoryValue != $mem) {
82
            $diff = ($mem - $this->previousMemoryValue);
83
            $msg = 'Memory: $0d0'.round($mem / 1024)."kb ";
84
85
            if ($this->previousMemoryValue < $mem) {
86
                $msg .= ' $f00+'.round($diff / 1024)."kb";
87
            } else {
88
                $msg .= ' $0f0'.round($diff / 1024)."kb";
89
            }
90
91
            $diff = ($mem - $this->startMemValue);
92
            if ($this->startMemValue  < $diff) {
93
                $msg .= ' $f00('.round($diff / 1024)."kb)";
94
            } else {
95
                $msg .= ' $0f0('.round($diff / 1024)."kb)";
96
            }
97
98
            $this->memoryMsg = $msg;
99
            $this->mlFactory->setMemory($msg);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class eXpansion\Framework\Core...ns\Gui\ManialinkFactory as the method setMemory() does only exist in the following sub-classes of eXpansion\Framework\Core...ns\Gui\ManialinkFactory: eXpansion\Bundle\Acme\Pl...Gui\MemoryWidgetFactory. 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...
100
            $this->mlFactory->update($this->players);
101
            // $this->console->writeln($msg);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
        }
103
        $this->previousMemoryValue = $mem;
104
105
    }
106
107
    /**
108
     * @param Map[] $oldMaps
109
     * @param string $currentMapUid
110
     * @param string $nextMapUid
111
     * @param bool $isListModified
112
     * @return mixed
113
     */
114
    public function onMapListModified($oldMaps, $currentMapUid, $nextMapUid, $isListModified)
0 ignored issues
show
Unused Code introduced by
The parameter $oldMaps 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...
Unused Code introduced by
The parameter $currentMapUid 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...
Unused Code introduced by
The parameter $nextMapUid 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...
Unused Code introduced by
The parameter $isListModified 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...
115
    {
116
        // TODO: Implement onMapListModified() method.
117
    }
118
119
    public function onExpansionMapChange($currentMap, $previousMap)
0 ignored issues
show
Unused Code introduced by
The parameter $currentMap 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...
Unused Code introduced by
The parameter $previousMap 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...
120
    {
121
        // TODO: Implement onExpansionMapChange() method.
122
    }
123
124
    public function onExpansionNextMapChange($nextMap, $previousNextMap)
0 ignored issues
show
Unused Code introduced by
The parameter $nextMap 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...
Unused Code introduced by
The parameter $previousNextMap 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...
125
    {
126
        // TODO: Implement onExpansionNextMapChange() method.
127
    }
128
129
    /**
130
     * Set the status of the plugin
131
     *
132
     * @param boolean $status
133
     *
134
     * @return null
135
     */
136
    public function setStatus($status)
137
    {
138
        if ($status) {
139
            $this->mlFactory->create($this->players);
140
        }
141
    }
142
143
    /**
144
     * @return mixed
145
     */
146
    public function getMemoryMsg()
147
    {
148
        return $this->memoryMsg;
149
    }
150
}
151