Skip to content

Commit 6f613b9

Browse files
authored
Updating to PHP 8 (#13)
General update brings the plugin inline with PHP 8
1 parent 9834346 commit 6f613b9

10 files changed

Lines changed: 172 additions & 162 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,5 @@ In Ubuntu 16.04, install it like so :
200200

201201
In Ubuntu 18.04, install it like so :
202202

203-
sudo apt install php7.2-curl
203+
sudo apt install php8.3-curl
204204

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
"homepage": "https://github.com/adilbaig"
1010
}],
1111
"require": {
12-
"php": ">=5.4.0",
12+
"php": ">=8.0",
1313
"ext-curl": "*"
1414
},
1515
"require-dev": {
16-
"phpunit/phpunit": ">=5.6"
16+
"phpunit/phpunit": ">=12.0"
1717
},
1818
"autoload": {
1919
"psr-0": {

src/PagerDuty/AcknowledgeEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class AcknowledgeEvent extends Event
1111
{
1212

13-
public function __construct($routingKey, $dedupKey)
13+
public function __construct(string $routingKey, string $dedupKey)
1414
{
1515
parent::__construct($routingKey, 'acknowledge');
1616

src/PagerDuty/Event.php

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

33
namespace PagerDuty;
44

5+
use ArrayAccess;
6+
use Exception;
7+
use JsonSerializable;
58
use PagerDuty\Exceptions\PagerDutyException;
69

710
/**
811
* An abstract Event
912
*
1013
* @author adil
1114
*/
12-
abstract class Event implements \ArrayAccess, \JsonSerializable
15+
abstract class Event implements ArrayAccess, JsonSerializable
1316
{
1417

15-
protected $dict;
18+
protected array $dict = [];
1619

1720
/**
1821
* ctor
1922
*
20-
* @param string $routingKey
21-
* @param string $type - One of 'trigger', 'acknowledge' or 'resolve'
23+
* @param string $routingKey
24+
* @param string $type - One of 'trigger', 'acknowledge' or 'resolve'
2225
*/
23-
protected function __construct($routingKey, $type)
26+
protected function __construct(string $routingKey, string $type)
2427
{
25-
$this->dict['routing_key'] = (string) $routingKey;
26-
$this->dict['event_action'] = (string) $type;
28+
$this->dict['routing_key'] = $routingKey;
29+
$this->dict['event_action'] = $type;
2730
}
2831

2932
/**
@@ -36,40 +39,41 @@ protected function __construct($routingKey, $type)
3639
*
3740
* @link https://v2.developer.pagerduty.com/docs/events-api-v2#alert-de-duplication
3841
*
39-
* @param string $key
42+
* @param string $key
4043
*
4144
* @return self
4245
*/
43-
public function setDeDupKey($key)
46+
public function setDeDupKey(string $key): static
4447
{
45-
$this->dict['dedup_key'] = substr((string) $key, 0, 255);
48+
$this->dict['dedup_key'] = substr($key, 0, 255);
4649
return $this;
4750
}
4851

4952
/**
50-
* Get the request json as an array
53+
* Get the request JSON as an array
5154
* Useful for debugging or logging.
5255
*
5356
* @return array
5457
*/
55-
public function toArray()
58+
public function toArray(): array
5659
{
57-
return $this->dict;
60+
return $this->dict ?? [];
5861
}
5962

6063
/**
6164
* Send the event to PagerDuty
6265
*
63-
* @param array $result (Opt)(Pass by reference) - If this parameter is given the result of the CURL call will be filled here. The response is an associative array.
64-
*
65-
* @throws PagerDutyException - If status code == 400
66+
* @param array|null $result (Opt) (Pass by reference) - If this parameter is given, the result of the CURL call will be filled here. The response is an associative array.
6667
*
6768
* @return int - HTTP response code
6869
* 202 - Event Processed
6970
* 400 - Invalid Event. Throws a PagerDutyException
7071
* 403 - Rate Limited. Slow down and try again later.
72+
*
73+
* @throws PagerDutyException - If status code == 400
74+
* @noinspection PhpUnused
7175
*/
72-
public function send(&$result = null)
76+
public function send(?array &$result = null): int
7377
{
7478
$jsonStr = json_encode($this);
7579

@@ -93,40 +97,44 @@ public function send(&$result = null)
9397

9498
return $responseCode;
9599
}
100+
96101
/* -------- ArrayAccess -------- */
97102

98-
public function offsetExists($key)
103+
public function offsetExists(mixed $offset): bool
99104
{
100-
return array_key_exists($key, $this->dict);
105+
return array_key_exists($offset, $this->dict);
101106
}
102107

103-
public function offsetGet($key)
108+
public function offsetGet(mixed $offset): mixed
104109
{
105-
if (!$this->offsetExists($key)) {
106-
return;
110+
if (!$this->offsetExists($offset)) {
111+
return null;
107112
}
108113

109-
return $this->dict[$key];
114+
return $this->dict[$offset];
110115
}
111116

112-
public function offsetSet($key, $value)
117+
/**
118+
* @throws Exception
119+
*/
120+
public function offsetSet(mixed $offset, mixed $value): void
113121
{
114-
if (empty($key) || is_string($key)) {
115-
throw new \Exception("Key must be a non-empty string. It is `" . var_export($key, true) . "`");
122+
if (empty($offset) || is_string($offset)) {
123+
throw new Exception("Offset must be a non-empty string. It is `" . var_export($offset, true) . "`");
116124
}
117125

118-
$this->dict[$key] = $value;
126+
$this->dict[$offset] = $value;
119127
}
120128

121-
public function offsetUnset($key)
129+
public function offsetUnset(mixed $offset): void
122130
{
123-
if ($this->offsetExists($key)) {
124-
unset($this->dict[$key]);
131+
if ($this->offsetExists($offset)) {
132+
unset($this->dict[$offset]);
125133
}
126134
}
127135
/* -------- JsonSerializable -------- */
128136

129-
public function jsonSerialize()
137+
public function jsonSerialize(): array
130138
{
131139
return $this->toArray();
132140
}

src/PagerDuty/Exceptions/PagerDutyConfigurationException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace PagerDuty\Exceptions;
44

5+
use Exception;
6+
57
/**
68
* Class PagerDutyConfigurationException
79
* @package PagerDuty\Exceptions
810
*/
9-
class PagerDutyConfigurationException extends \Exception
11+
class PagerDutyConfigurationException extends Exception
1012
{
1113

1214
}

src/PagerDuty/Exceptions/PagerDutyException.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace PagerDuty\Exceptions;
44

5+
use Exception;
6+
57
/**
68
* PagerDutyException
79
*
810
* @author adil
911
*/
10-
class PagerDutyException extends \Exception
12+
class PagerDutyException extends Exception
1113
{
1214

13-
protected $errors;
15+
protected array $errors;
1416

1517
public function __construct($message, array $errors)
1618
{
@@ -21,8 +23,9 @@ public function __construct($message, array $errors)
2123
/**
2224
*
2325
* @return array
26+
* @noinspection PhpUnused
2427
*/
25-
public function getErrors()
28+
public function getErrors(): array
2629
{
2730
return $this->errors;
2831
}

0 commit comments

Comments
 (0)