Completed
Push — master ( b8b783...188bc3 )
by De Cramer
9s
created

AbstractApplication::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace eXpansion\Core\Services\Application;
5
6
use eXpansion\Core\Services\Console;
7
use Maniaplanet\DedicatedServer\Connection;
8
use Symfony\Component\Console\Output\ConsoleOutputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
abstract class AbstractApplication implements RunInterface
12
{
13
    /** @var Connection */
14
    protected $connection;
15
16
    /** @var Dispatcher */
17
    protected $dispatcher;
18
19
    /** @var Console */
20
    protected $console;
21
22
    /** @var bool  */
23
    protected $isRunning = true;
24
25
    /** Base eXpansion callbacks. */
26
    const EVENT_RUN = "expansion.run";
27
28
    /**
29
     * Application constructor.
30
     *
31
     * @param DispatcherInterface $dispatcher
32
     * @param Connection $connection
33
     * @param Console $output
34
     */
35 10
    public function __construct(
36
        DispatcherInterface $dispatcher,
37
        Connection $connection,
38
        Console $output
39
    ) {
40 10
        $this->connection = $connection;
41 10
        $this->dispatcher = $dispatcher;
0 ignored issues
show
Documentation Bug introduced by
$dispatcher is of type object<eXpansion\Core\Se...on\DispatcherInterface>, but the property $dispatcher was declared to be of type object<eXpansion\Core\Se...Application\Dispatcher>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
42 10
        $this->console = $output;
43 10
    }
44
45
    /**
46
     * Initialize eXpansion.
47
     *
48
     * @param ConsoleOutputInterface $console
49
     *
50
     * @return $this
51
     */
52 1
    public function init(OutputInterface $console)
53
    {
54 1
        $this->console->init($console);
0 ignored issues
show
Compatibility introduced by
$console of type object<Symfony\Component...Output\OutputInterface> is not a sub-type of object<Symfony\Component...ConsoleOutputInterface>. It seems like you assume a child interface of the interface Symfony\Component\Console\Output\OutputInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
55 1
        $this->dispatcher->init();
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * Run eXpansion
62
     *
63
     */
64 2
    public function run()
65
    {
66 2
        $this->connection->enableCallbacks(true);
67
68 2
        $startTime = microtime(true);
69 2
        $nextCycleStart = $startTime;
70 2
        $cycleTime = 1 / 60;
71
72 2
        $this->console->writeln("Running preflight checks...");
73
74
        // need to send this for scripts to start callback handling
75 2
        $this->connection->triggerModeScriptEvent("XmlRpc.EnableCallbacks", ["True"]);
76
77 2
        $this->dispatcher->dispatch(self::EVENT_RUN, []);
78
79 2
        $this->console->writeln("And takeoff");
80
81
        do {
82 2
            $this->executeRun();
83
84 2
            $endCycleTime = microtime(true) + $cycleTime / 10;
85
            do {
86 2
                $nextCycleStart += $cycleTime;
87 2
            } while ($nextCycleStart < $endCycleTime);
88
89 2
            @time_sleep_until($nextCycleStart);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
90
91 2
        } while ($this->isRunning);
92 2
    }
93
94
    /**
95
     * Stop eXpansion.
96
     */
97 2
    public function stopApplication()
98
    {
99 2
        $this->isRunning = false;
100 2
    }
101
102
    abstract protected function executeRun();
103
}