Skip to content

Commit af198b0

Browse files
committed
Fix static analysis warnings
1 parent aa17940 commit af198b0

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/GlobalsAttributeReader.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33

44
namespace Zalas\PHPUnit\Globals;
55

6+
use PHPUnit\Event\Code\Test;
67
use PHPUnit\Event\Code\TestMethod;
78
use PHPUnit\Event\Test\PreparationStarted;
89
use PHPUnit\Event\Test\PreparationStartedSubscriber;
10+
use ReflectionAttribute;
11+
use ReflectionClass;
12+
use ReflectionException;
13+
use ReflectionMethod;
14+
use RuntimeException;
915
use Zalas\PHPUnit\Globals\Attribute\Env;
1016
use Zalas\PHPUnit\Globals\Attribute\Putenv;
1117
use Zalas\PHPUnit\Globals\Attribute\Server;
@@ -17,7 +23,7 @@ public function notify(PreparationStarted $event): void
1723
$this->readGlobalAttributes($event->test());
1824
}
1925

20-
private function readGlobalAttributes(TestMethod $method): void
26+
private function readGlobalAttributes(Test $method): void
2127
{
2228
$attributes = $this->parseTestMethodAttributes($method);
2329
$setVars = $this->findSetVarAttributes($attributes);
@@ -84,27 +90,32 @@ private function findUnsetVarAttributes(array $attributes): array
8490
/**
8591
* @return array<Env|Putenv|Server>
8692
*/
87-
private function parseTestMethodAttributes(TestMethod $method): array
93+
private function parseTestMethodAttributes(Test $method): array
8894
{
95+
if (!$method instanceof TestMethod) {
96+
return [];
97+
}
98+
8999
$className = $method->className();
90100
$methodName = $method->methodName();
91101

92-
$methodAttributes = null;
93-
94-
if (null !== $methodName) {
102+
try {
95103
$methodAttributes = $this->collectGlobalsFromAttributes(
96-
(new \ReflectionMethod($className, $methodName))->getAttributes()
104+
(new ReflectionMethod($className, $methodName))->getAttributes()
97105
);
98-
}
99106

100-
return \array_merge(
101-
$methodAttributes,
102-
$this->collectGlobalsFromAttributes((new \ReflectionClass($className))->getAttributes())
103-
);
107+
return \array_merge(
108+
$methodAttributes,
109+
$this->collectGlobalsFromAttributes((new ReflectionClass($className))->getAttributes())
110+
);
111+
} catch (ReflectionException $e) {
112+
// There would need to be a bug in PHPUnit for the ReflectionException to be thrown.
113+
throw new RuntimeException("Failed to parse test method $className::$methodName", 0, $e);
114+
}
104115
}
105116

106117
/**
107-
* @param array<\ReflectionAttribute> $attributes
118+
* @param array<ReflectionAttribute> $attributes
108119
* @return array<Env|Putenv|Server>
109120
*/
110121
private function collectGlobalsFromAttributes(array $attributes): array

src/GlobalsBackup.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
final class GlobalsBackup implements PreparationStartedSubscriber
99
{
10-
private $server;
11-
private $env;
12-
private $getenv;
10+
private array $server;
11+
private array $env;
12+
private array $getenv;
1313

1414
public function notify(PreparationStarted $event): void
1515
{

0 commit comments

Comments
 (0)