Skip to content

Commit 68403fa

Browse files
Treat enumerations as classes
1 parent 359a2d4 commit 68403fa

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

src/StaticAnalysis/CodeUnitFindingVisitor.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpParser\Node\NullableType;
2222
use PhpParser\Node\Stmt\Class_;
2323
use PhpParser\Node\Stmt\ClassMethod;
24+
use PhpParser\Node\Stmt\Enum_;
2425
use PhpParser\Node\Stmt\Function_;
2526
use PhpParser\Node\Stmt\Interface_;
2627
use PhpParser\Node\Stmt\Trait_;
@@ -81,6 +82,10 @@ public function enterNode(Node $node): null
8182
$this->processClass($node);
8283
}
8384

85+
if ($node instanceof Enum_) {
86+
$this->processClass($node);
87+
}
88+
8489
if ($node instanceof Trait_) {
8590
$this->processTrait($node);
8691
}
@@ -255,19 +260,21 @@ private function processInterface(Interface_ $node): void
255260
);
256261
}
257262

258-
private function processClass(Class_ $node): void
263+
private function processClass(Class_|Enum_ $node): void
259264
{
260265
$name = $node->name->toString();
261266
$namespacedName = $node->namespacedName->toString();
262267
$parentClass = null;
263268
$interfaces = [];
264269

265-
if ($node->extends instanceof Name) {
266-
$parentClass = $node->extends->toString();
267-
}
270+
if (!$node instanceof Enum_) {
271+
if ($node->extends instanceof Name) {
272+
$parentClass = $node->extends->toString();
273+
}
268274

269-
foreach ($node->implements as $interface) {
270-
$interfaces[] = $interface->toString();
275+
foreach ($node->implements as $interface) {
276+
$interfaces[] = $interface->toString();
277+
}
271278
}
272279

273280
$this->classes[$namespacedName] = new \SebastianBergmann\CodeCoverage\StaticAnalysis\Class_(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php declare(strict_types=1);
2+
namespace SebastianBergmann\CodeCoverage\TestFixture\Target;
3+
4+
enum TargetEnumeration
5+
{
6+
case Foo;
7+
case Bar;
8+
}

tests/tests/Target/MapBuilderTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use SebastianBergmann\CodeCoverage\StaticAnalysis\ParsingFileAnalyser;
2121
use SebastianBergmann\CodeCoverage\TestFixture\Target\T1;
2222
use SebastianBergmann\CodeCoverage\TestFixture\Target\T2;
23+
use SebastianBergmann\CodeCoverage\TestFixture\Target\TargetEnumeration;
2324
use SebastianBergmann\CodeCoverage\TestFixture\Target\TraitOne;
2425
use SebastianBergmann\CodeCoverage\TestFixture\Target\TraitTwo;
2526

@@ -39,6 +40,7 @@ public static function provider(): array
3940
$traitOne = realpath(__DIR__ . '/../../_files/Target/TraitOne.php');
4041
$traitTwo = realpath(__DIR__ . '/../../_files/Target/TraitTwo.php');
4142
$twoTraits = realpath(__DIR__ . '/../../_files/Target/two_traits.php');
43+
$enum = realpath(__DIR__ . '/../../_files/Target/TargetEnumeration.php');
4244

4345
return [
4446
'generic' => [
@@ -279,6 +281,44 @@ public static function provider(): array
279281
$twoTraits,
280282
],
281283
],
284+
'enumeration' => [
285+
[
286+
'namespaces' => [
287+
'SebastianBergmann' => [
288+
$enum => range(4, 8),
289+
],
290+
'SebastianBergmann\\CodeCoverage' => [
291+
$enum => range(4, 8),
292+
],
293+
'SebastianBergmann\\CodeCoverage\\TestFixture' => [
294+
$enum => range(4, 8),
295+
],
296+
'SebastianBergmann\\CodeCoverage\\TestFixture\\Target' => [
297+
$enum => range(4, 8),
298+
],
299+
],
300+
'traits' => [
301+
],
302+
'classes' => [
303+
TargetEnumeration::class => [
304+
$enum => range(4, 8),
305+
],
306+
],
307+
'classesThatExtendClass' => [
308+
],
309+
'classesThatImplementInterface' => [
310+
],
311+
'methods' => [
312+
],
313+
'functions' => [
314+
],
315+
'reverseLookup' => [
316+
],
317+
],
318+
[
319+
$enum,
320+
],
321+
],
282322
];
283323
}
284324

tests/tests/Target/MapperTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPUnit\Framework\TestCase;
2121
use SebastianBergmann\CodeCoverage\Filter;
2222
use SebastianBergmann\CodeCoverage\StaticAnalysis\ParsingFileAnalyser;
23+
use SebastianBergmann\CodeCoverage\TestFixture\Target\TargetEnumeration;
2324
use SebastianBergmann\CodeCoverage\TestFixture\Target\TraitOne;
2425

2526
/**
@@ -157,6 +158,17 @@ public static function provider(): array
157158
],
158159
),
159160
],
161+
162+
'enumeration' => [
163+
[
164+
realpath(__DIR__ . '/../../_files/Target/TargetEnumeration.php') => range(4, 8),
165+
],
166+
TargetCollection::fromArray(
167+
[
168+
Target::forClass(TargetEnumeration::class),
169+
],
170+
),
171+
],
160172
];
161173
}
162174

0 commit comments

Comments
 (0)