Issues (1369)

classes/Fleet/FleetWatchdog.php (5 issues)

1
<?php
2
/** Created by Gorlum 27.10.2024 18:43 */
3
4
namespace Fleet;
5
6
use Core\Scheduler\Lock;
7
use SN;
8
9
class FleetWatchdog {
10
  /** @var $workBegin */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $workBegin at position 0 could not be parsed: Unknown type name '$workBegin' at position 0 in $workBegin.
Loading history...
11
  public static $workBegin;
12
13
14
  /** @var float $eventStartedAt */
15
  public static $eventStartedAt = SN_TIME_NOW;
16
//  /** @var float $lastEventEnd */
17
//  public static $lastEventEnd = SN_TIME_NOW;
18
19
  /** @var int $currentMission */
20
  public static $currentMission = MT_NONE;
21
  /** @var int $eventsProcessed */
22
  public static $eventsProcessed = 0;
23
  /** @var int $currentEvent */
24
  public static $currentEvent = EVENT_FLEET_NONE;
25
  /** @var int $processedIPR Processed IPR on this run */
26
  public static $processedIPR = -1;
27
28
  const TASK_COMPLETE = 0;
29
  const CONTINUE_EXECUTION = 2;
30
  const TASK_TOO_LONG = 1;
31
  const TASK_ALREADY_LOCKED = -1;
32
  const LOCK_ACQUIRED = -2;
33
  const FLEET_IS_EMPTY = 'FLEET_EMPTY';
34
35
  const EVENT_DISPATCH_STARTED = 'EVENT_DISPATCH_STARTED';
36
  /** @var Lock $runLock */
37
  private static $runLock;
38
39
40
  public function __construct() {
41
    // Dispatch started
42
    self::$workBegin = microtime(true);
43
44
    self::$eventStartedAt  = self::$workBegin;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::workBegin can also be of type string. However, the property $eventStartedAt is declared as type double. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
45
    self::$currentMission  = MT_NONE;
46
    self::$currentEvent    = EVENT_FLEET_NONE;
47
    self::$eventsProcessed = 0;
48
  }
49
50
  /**
51
   * @return int
52
   */
53
  public function acquireLock() {
54
    // Trying to acquire lock for current task
55
    self::$runLock = $runLock = SN::$gc->fleetDispatcher->buildLock();
56
    if (!$runLock->attemptLock()) {
57
      return self::TASK_ALREADY_LOCKED;
58
    }
59
60
    register_shutdown_function(function () use ($runLock) {
61
//      $this->log_file('Shutting down');
62
      $timeLock = $runLock->isLocked();
63
      if ($timeLock > 0 || $timeLock === 0) {
64
        $runLock->unLock(true);
65
        $this->logTermination();
66
//        $this->log_file('UNLOCKING');
67
      }
68
//      $this->log_file(SN::$gc->config->pass()->fleet_update_run_lock);
69
//      $this->log_file('ALL RELEASED');
70
    });
71
72
    return self::LOCK_ACQUIRED;
73
  }
74
75
  public function begin(FleetDispatchEvent $fleetEvent) {
76
    // Watchdog timer
77
    // If flying fleet handler works more than `fleet_update_dispatch_time` seconds - stopping it
78
    // Let next run handle rest of fleets
79
// var_dump(microtime(true), self::$workBegin, (microtime(true) - self::$workBegin) >= SN::$config->fleet_update_dispatch_time, $fleetEvent->fleet);
80
    if ((microtime(true) - self::$workBegin) >= SN::$config->fleet_update_dispatch_time) {
81
      $this->logTermination();
82
83
      return FleetWatchdog::TASK_TOO_LONG;
84
    }
85
86
    if (empty($fleetEvent->fleet)) {
87
      // Fleet was destroyed in course of previous actions
88
      return FleetWatchdog::FLEET_IS_EMPTY;
89
    }
90
91
    self::$processedIPR = $fleetEvent::$processedIPR;
92
93
    self::$eventStartedAt = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $eventStartedAt is declared as type double. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
94
    self::$currentMission = !empty($fleetEvent->fleet[FleetDispatcher::F_FLEET_MISSION]) ? $fleetEvent->fleet[FleetDispatcher::F_FLEET_MISSION] : MT_NONE;
95
    self::$currentEvent   = !empty($fleetEvent->event) ? $fleetEvent->event : MT_NONE;
0 ignored issues
show
Documentation Bug introduced by
It seems like ! empty($fleetEvent->eve...->event : Fleet\MT_NONE can also be of type string. However, the property $currentEvent is declared as type integer. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
96
    self::$eventsProcessed++;
97
98
    return FleetWatchdog::CONTINUE_EXECUTION;
99
  }
100
101
  /**
102
   */
103
  public function logTermination() {
104
    SN::$debug->warning(
0 ignored issues
show
The method warning() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
    SN::$debug->/** @scrutinizer ignore-call */ 
105
                warning(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
      $this->getTerminationMessage(),
106
      'FFH Warning',
107
      504
108
    );
109
  }
110
111
  public function unlock() {
112
    self::$runLock->unLock(true);
113
  }
114
115
  /**
116
   * @return string
117
   */
118
  public function getTerminationMessage() {
119
    return sprintf(
120
      'Flying fleet handler works %1$s seconds (> %2$s) - skipping rest. Processed %8$d IPRs, %3$d / %7$d events. Last event: mission %4$s event %6$s (%5$ss)',
121
      number_format(microtime(true) - self::$workBegin, 4),
122
      SN::$config->fleet_update_dispatch_time,
123
      self::$eventsProcessed,
124
      !empty(SN::$lang['type_mission'][self::$currentMission]) ? SN::$lang['type_mission'][self::$currentMission] : '!TERMINATED BY TIMEOUT!',
125
      number_format(microtime(true) - self::$eventStartedAt, 4),
126
      !empty(SN::$lang['fleet_events'][self::$currentEvent]) ? SN::$lang['fleet_events'][self::$currentEvent] : '!TERMINATED BY TIMEOUT!',
127
      count(FleetDispatcher::$fleet_event_list),
128
      self::$processedIPR
129
    );
130
  }
131
132
}
133