Skip to content

Commit 6c41190

Browse files
committed
Add runtime exceptions for invalid data.
1 parent 7642b20 commit 6c41190

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

src/Doc.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ public function getParams(): array {
5555
* @phpstan-param DocArray $data
5656
*/
5757
private function setData( array $data ): self {
58+
$required = [ 'description', 'long_description', 'long_description_html', 'tags' ];
59+
60+
foreach ( $required as $key ) {
61+
if ( ! array_key_exists( $key, $data ) ) {
62+
throw new \InvalidArgumentException(
63+
sprintf(
64+
'Missing required key "%s" in data array',
65+
$key,
66+
)
67+
);
68+
}
69+
}
70+
5871
$this->description = $data['description'];
5972
$this->long_description = $data['long_description'];
6073
$this->long_description_html = $data['long_description_html'];

src/Hook.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ public function getParams(): array {
8383
* @phpstan-param HookArray $data
8484
*/
8585
private function setData( array $data ): self {
86+
$required = [ 'name', 'file', 'type', 'doc', 'args' ];
87+
88+
foreach ( $required as $key ) {
89+
if ( ! array_key_exists( $key, $data ) ) {
90+
throw new \InvalidArgumentException(
91+
sprintf(
92+
'Missing required key "%s" in data array',
93+
$key,
94+
)
95+
);
96+
}
97+
}
98+
8699
$this->name = $data['name'];
87100
$this->aliases = $data['aliases'] ?? null;
88101
$this->file = $data['file'];

src/Hooks.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public static function fromVendor( string $directory, string $file ): self {
1919
}
2020

2121
/**
22-
* @throws \Exception
22+
* @throws \InvalidArgumentException
2323
*/
2424
public static function fromFile( string $file ): self {
2525
if ( ! file_exists( $file ) ) {
26-
throw new \Exception( sprintf(
26+
throw new \InvalidArgumentException( sprintf(
2727
'File does not exist: %s',
2828
$file
2929
) );
@@ -99,13 +99,13 @@ public function includes( string $name ): bool {
9999
}
100100

101101
/**
102-
* @throws \Exception
102+
* @throws \ErrorException
103103
*/
104104
private static function fromKnownFile( string $file ): self {
105105
$contents = file_get_contents( $file );
106106

107107
if ( $contents === false ) {
108-
throw new \Exception( sprintf(
108+
throw new \ErrorException( sprintf(
109109
'Could not open hook file: %s',
110110
$file
111111
) );
@@ -114,7 +114,7 @@ private static function fromKnownFile( string $file ): self {
114114
$decoded = json_decode( $contents, true );
115115

116116
if ( ! is_array( $decoded ) || ! isset( $decoded['hooks'] ) || ! is_array( $decoded['hooks'] ) ) {
117-
throw new \Exception( sprintf(
117+
throw new \ErrorException( sprintf(
118118
'Unexpected data format in file: %s',
119119
$file
120120
) );
@@ -126,7 +126,7 @@ private static function fromKnownFile( string $file ): self {
126126
}
127127

128128
/**
129-
* @throws \Exception
129+
* @throws \InvalidArgumentException
130130
*/
131131
private static function findFileFromVendor( string $directory, string $path ): string {
132132
$library_dependency = $directory . '/vendor/' . $path;
@@ -141,7 +141,7 @@ private static function findFileFromVendor( string $directory, string $path ): s
141141
return $project_dependency;
142142
}
143143

144-
throw new \Exception( sprintf(
144+
throw new \InvalidArgumentException( sprintf(
145145
'Vendor directory not found for file: %s',
146146
$path
147147
) );

src/Tag.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ public function getDescription(): ?string {
7474
* @phpstan-param TagArray $data
7575
*/
7676
private function setData( array $data ): self {
77+
$required = [ 'name', 'content' ];
78+
foreach ( $required as $key ) {
79+
if ( ! array_key_exists( $key, $data ) ) {
80+
throw new \InvalidArgumentException(
81+
sprintf(
82+
'Missing required key "%s" in data array',
83+
$key,
84+
)
85+
);
86+
}
87+
}
88+
7789
$this->name = $data['name'];
7890
$this->content = $data['content'];
7991
$this->types = $data['types'] ?? null;

tests/phpunit/HooksTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testCanBeCreatedFromFile( string $file ): void {
2525
}
2626

2727
public function testErrorThrownWhenFileDoesNotExist(): void {
28-
self::expectException( \Exception::class );
28+
self::expectException( \InvalidArgumentException::class );
2929
Hooks::fromFile( __DIR__ . '/missing.json' );
3030
}
3131

0 commit comments

Comments
 (0)