Skip to content

Commit 16bf1d6

Browse files
committed
library: improve types, cleanup, little fixes
1 parent 0968885 commit 16bf1d6

File tree

94 files changed

+406
-429
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+406
-429
lines changed

library/Eventtracker/Check/PluginState.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use gipfl\Cli\Screen;
66
use InvalidArgumentException;
7+
78
use function array_key_exists;
89
use function ctype_digit;
910
use function is_int;
@@ -12,19 +13,19 @@
1213

1314
class PluginState
1415
{
15-
const STATE_OK = 0;
16-
const STATE_WARNING = 1;
17-
const STATE_CRITICAL = 2;
18-
const STATE_UNKNOWN = 3;
16+
public const STATE_OK = 0;
17+
public const STATE_WARNING = 1;
18+
public const STATE_CRITICAL = 2;
19+
public const STATE_UNKNOWN = 3;
1920

20-
const NUMERIC_TO_NAME = [
21+
protected const NUMERIC_TO_NAME = [
2122
self::STATE_OK => 'OK',
2223
self::STATE_WARNING => 'WARNING',
2324
self::STATE_CRITICAL => 'CRITICAL',
2425
self::STATE_UNKNOWN => 'UNKNOWN',
2526
];
2627

27-
const NAME_TO_NUMERIC = [
28+
protected const NAME_TO_NUMERIC = [
2829
'OK' => self::STATE_OK,
2930
'WARNING' => self::STATE_WARNING,
3031
'CRITICAL' => self::STATE_CRITICAL,
@@ -38,9 +39,9 @@ class PluginState
3839
self::STATE_UNKNOWN => 'purple',
3940
];
4041

41-
protected $state;
42+
protected int $state;
4243

43-
public function __construct($state = self::STATE_OK)
44+
final public function __construct($state = self::STATE_OK)
4445
{
4546
$this->state = self::getNumeric($state);
4647
}
@@ -50,37 +51,37 @@ public function raise($state)
5051
$this->state = self::getWorst($this->state, $state);
5152
}
5253

53-
public function getExitCode()
54+
public function getExitCode(): int
5455
{
5556
return $this->state;
5657
}
5758

58-
public function toString()
59+
public function toString(): string
5960
{
6061
return self::getColorized($this->state);
6162
}
6263

63-
public function __toString()
64+
public function __toString(): string
6465
{
6566
return $this->toString();
6667
}
6768

68-
public static function ok()
69+
public static function ok(): PluginState
6970
{
7071
return new static(self::STATE_OK);
7172
}
7273

73-
public static function warning()
74+
public static function warning(): PluginState
7475
{
7576
return new static(self::STATE_WARNING);
7677
}
7778

78-
public static function critical()
79+
public static function critical(): PluginState
7980
{
8081
return new static(self::STATE_CRITICAL);
8182
}
8283

83-
public static function unknown()
84+
public static function unknown(): PluginState
8485
{
8586
return new static(self::STATE_UNKNOWN);
8687
}
@@ -106,7 +107,7 @@ public static function getColorized($state)
106107
return Screen::factory()->colorize(self::getName($state), self::STATE_TO_ANSI_COLOR[$state]);
107108
}
108109

109-
public static function getNumeric($state)
110+
public static function getNumeric($state): int
110111
{
111112
if ((is_int($state) || ctype_digit($state)) && array_key_exists((int) $state, self::NUMERIC_TO_NAME)) {
112113
return (int) $state;
@@ -120,7 +121,7 @@ public static function getNumeric($state)
120121
throw new InvalidArgumentException("$state is not a valid Check Plugin state");
121122
}
122123

123-
public static function getName($state)
124+
public static function getName($state): string
124125
{
125126
return self::NUMERIC_TO_NAME[self::getNumeric($state)];
126127
}

library/Eventtracker/ConfigHelperCi.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class ConfigHelperCi
88
{
99
public static function fillPlaceHoldersForIssue($string, Issue $issue, ZfDb $db)
1010
{
11-
$ci = IcingaCi::eventuallyLoadForIssue($db, $issue);
11+
$ci = IcingaCi::loadOptionalForIssue($db, $issue);
1212
if ($ci) {
1313
if ($ci->object_type === 'service') {
14-
if ($host = IcingaCi::eventuallyLoad($db, $issue->get('host_name'))) {
14+
if ($host = IcingaCi::loadOptional($db, $issue->get('host_name'))) {
1515
$ci->host = $host;
1616
}
1717
}

library/Eventtracker/Daemon/BackgroundDaemon.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class BackgroundDaemon implements EventEmitterInterface
4141
/** @var DaemonDb */
4242
protected $daemonDb;
4343

44-
/** @var DaemonProcessState */
44+
/** @var ?DaemonProcessState */
4545
protected $processState;
4646

4747
/** @var DaemonProcessDetails */
@@ -116,7 +116,11 @@ protected function initializeProcessDetails($systemd): DaemonProcessDetails
116116
try {
117117
$uuid = \bin2hex(Uuid::uuid4()->getBytes());
118118
} catch (Exception $e) {
119-
$uuid = 'deadc0de' . \substr(\md5(\getmypid()), 0, 24);
119+
$pid = getmypid();
120+
if ($pid === false) {
121+
$pid = '<unknown>';
122+
}
123+
$uuid = 'deadc0de' . \substr(\md5((string) $pid), 0, 24);
120124
}
121125
}
122126
$processDetails = new DaemonProcessDetails($uuid);

library/Eventtracker/Daemon/DaemonDb.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use gipfl\IcingaCliDaemon\RetryUnless;
1010
use gipfl\ZfDb\Adapter\Adapter as ZfDb;
1111
use gipfl\ZfDb\Adapter\Pdo\Mysql;
12+
use gipfl\ZfDb\Adapter\Pdo\PdoAdapter;
1213
use Icinga\Application\Icinga;
1314
use Icinga\Module\Eventtracker\Db\ZfDbConnectionFactory;
1415
use Icinga\Module\Eventtracker\Modifier\Settings;
@@ -36,9 +37,7 @@ class DaemonDb
3637
public const ON_SCHEMA_CHANGE = 'schemaChange';
3738
const TABLE_NAME = 'daemon_info';
3839
protected LoggerInterface $logger;
39-
40-
/** @var ZfDb|Mysql */
41-
protected $db;
40+
protected ?PdoAdapter $db = null;
4241

4342
protected DaemonProcessDetails $details;
4443

@@ -139,8 +138,9 @@ protected function establishConnection($config): PromiseInterface
139138
return $this->pendingReconnection->run(Loop::get());
140139
}
141140

142-
protected function getMigrations(Mysql $db): Migrations
141+
protected function getMigrations(PdoAdapter $db): Migrations
143142
{
143+
assert($db instanceof Mysql);
144144
return new Migrations($db, Icinga::app()->getModuleManager()->getModuleDir(
145145
'eventtracker',
146146
'/schema'
@@ -280,9 +280,7 @@ public function disconnect(): PromiseInterface
280280
$this->pendingDisconnect = $this->stopRegisteredComponents();
281281

282282
try {
283-
if ($this->db) {
284-
$this->db->closeConnection();
285-
}
283+
$this->db->closeConnection();
286284
} catch (Exception $e) {
287285
$this->logger->error('Failed to disconnect: ' . $e->getMessage());
288286
}

library/Eventtracker/Daemon/DaemonUtil.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
class DaemonUtil
66
{
7-
/**
8-
* @return int
9-
*/
10-
public static function timestampWithMilliseconds()
7+
public static function timestampWithMilliseconds(): int
118
{
129
$mTime = explode(' ', microtime());
1310

14-
return (int) round($mTime[0] * 1000) + (int) $mTime[1] * 1000;
11+
return (int) round((int) $mTime[0] * 1000) + (int) $mTime[1] * 1000;
1512
}
1613
}

library/Eventtracker/Daemon/IcingaCliRunner.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class IcingaCliRunner
1515
/** @var array|null */
1616
protected $env;
1717

18-
public function __construct($binary)
18+
final public function __construct($binary)
1919
{
2020
$this->binary = $binary;
2121
}
@@ -34,11 +34,7 @@ public static function forArgv(?array $argv = null)
3434
return new static($argv[0]);
3535
}
3636

37-
/**
38-
* @param mixed array|...$arguments
39-
* @return Process
40-
*/
41-
public function command($arguments = null)
37+
public function command(?array $arguments = null): Process
4238
{
4339
if (! is_array($arguments)) {
4440
$arguments = func_get_args();

library/Eventtracker/Daemon/IdoDb.php

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Icinga\Module\Eventtracker\Daemon;
44

5-
use gipfl\ZfDb\Adapter\Adapter as DbAdapter;
5+
use gipfl\ZfDb\Adapter\Pdo\PdoAdapter;
66
use gipfl\ZfDb\Select as DbSelect;
77
use Icinga\Application\Config;
88
use Icinga\Module\Eventtracker\Config\IcingaResource;
@@ -16,30 +16,22 @@
1616
*/
1717
class IdoDb
1818
{
19-
/** @var DbAdapter */
20-
protected $db;
19+
protected PdoAdapter $db;
2120

2221
/** @var string */
2322
protected $lastHostsChecksum;
2423

25-
/**
26-
* IdoDb constructor.
27-
* @param DbAdapter $db
28-
*/
29-
public function __construct(DbAdapter $db)
24+
public function __construct(PdoAdapter $db)
3025
{
3126
$this->db = $db;
3227
}
3328

34-
/**
35-
* @return DbAdapter
36-
*/
37-
public function getDb()
29+
public function getDb(): PdoAdapter
3830
{
3931
return $this->db;
4032
}
4133

42-
public function coreIsRunning()
34+
public function coreIsRunning(): bool
4335
{
4436
$query = $this->db
4537
->select()
@@ -284,17 +276,11 @@ protected function enrichRowsWithVars($rows)
284276
}
285277
}
286278

287-
/**
288-
* Instantiate with a given Icinga Web 2 resource name
289-
*
290-
* @param $name
291-
* @return static
292-
*/
293-
public static function fromResourceName($name)
279+
public static function fromResourceName(string $name): IdoDb
294280
{
295-
return new static(
296-
ZfDbConnectionFactory::connection(static::getResourceConfig($name))
297-
);
281+
/** @var PdoAdapter $db */
282+
$db = ZfDbConnectionFactory::connection(static::getResourceConfig($name));
283+
return new IdoDb($db);
298284
}
299285

300286
protected static function getResourceConfig($name)
@@ -309,10 +295,8 @@ protected static function getResourceConfig($name)
309295

310296
/**
311297
* Borrow the database connection from the monitoring module
312-
*
313-
* @return static
314298
*/
315-
public static function fromMonitoringModule()
299+
public static function fromMonitoringModule(): IdoDb
316300
{
317301
return static::fromResourceName(static::getIdoBackendResourceName());
318302
}

library/Eventtracker/Daemon/InputAndChannelRunner.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Icinga\Module\Eventtracker\Daemon;
44

5-
use gipfl\ZfDb\Adapter\Adapter as Db;
5+
use gipfl\ZfDb\Adapter\Pdo\PdoAdapter;
66
use Icinga\Module\Eventtracker\Db\ConfigStore;
77
use Icinga\Module\Eventtracker\Engine\Downtime\DowntimeRunner;
88
use Icinga\Module\Eventtracker\Engine\InputRunner;
@@ -23,17 +23,12 @@ public function __construct(DowntimeRunner $downtimeRunner, LoggerInterface $log
2323
$this->logger = $logger;
2424
}
2525

26-
/**
27-
* @return \React\Promise\ExtendedPromiseInterface
28-
*/
29-
public function initDb(Db $db)
26+
public function initDb(PdoAdapter $db): void
3027
{
3128
$store = new ConfigStore($db, $this->logger);
3229
$this->runner = new InputRunner($store, $this->downtimeRunner, $this->logger);
3330
$this->runner->setLogger($this->logger);
3431
$this->runner->start();
35-
36-
return resolve(null);
3732
}
3833

3934
public function getInputRunner(): ?InputRunner

library/Eventtracker/Daemon/JobRunner.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use gipfl\Process\ProcessList;
77
use gipfl\Protocol\JsonRpc\Handler\NamespacedPacketHandler;
88
use gipfl\Protocol\JsonRpc\JsonRpcConnection;
9-
use gipfl\ZfDb\Adapter\Adapter as Db;
9+
use gipfl\ZfDb\Adapter\Pdo\PdoAdapter;
1010
use Icinga\Application\Config;
1111
use Icinga\Application\Icinga;
1212
use Psr\Log\LoggerInterface;
@@ -22,7 +22,7 @@ class JobRunner implements DbBasedComponent
2222
{
2323
protected ProcessList $running;
2424
protected LoggerInterface $logger;
25-
protected ?Db $db = null;
25+
protected ?PdoAdapter $db = null;
2626
protected ?LogProxy $logProxy = null;
2727
protected ?TimerInterface $timer = null;
2828
protected ?TimerInterface $scheduleTimer = null;
@@ -62,11 +62,7 @@ protected function getTaskList(): array
6262
return $tasks;
6363
}
6464

65-
/**
66-
* @param Db $db
67-
* @return ExtendedPromiseInterface
68-
*/
69-
public function initDb(Db $db)
65+
public function initDb(PdoAdapter $db): void
7066
{
7167
$this->db = $db;
7268
$check = function () {

library/Eventtracker/Daemon/RemoteClient.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@
55
use gipfl\Protocol\JsonRpc\JsonRpcConnection;
66
use gipfl\Protocol\NetString\StreamWrapper;
77
use React\EventLoop\Loop;
8-
use React\EventLoop\LoopInterface;
8+
use React\Promise\PromiseInterface;
99
use React\Socket\ConnectionInterface;
1010
use React\Socket\UnixConnector;
11+
1112
use function React\Promise\resolve;
1213

1314
class RemoteClient
1415
{
15-
protected $path;
16-
17-
/** @var JsonRpcConnection */
18-
protected $connection;
19-
20-
21-
protected $pendingConnection;
16+
protected string $path;
17+
protected ?JsonRpcConnection$connection = null;
18+
protected ?PromiseInterface $pendingConnection = null;
2219

23-
public function __construct($path)
20+
public function __construct(string $path)
2421
{
2522
$this->path = $path;
2623
}

0 commit comments

Comments
 (0)