44
55use gipfl \Cli \Screen ;
66use InvalidArgumentException ;
7+
78use function array_key_exists ;
89use function ctype_digit ;
910use function is_int ;
1213
1314class 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 }
0 commit comments