Skip to content

Commit

Permalink
Merge pull request #329 from dkreuer/feature/php81-enums
Browse files Browse the repository at this point in the history
Supporting PHP 8.1 enums
  • Loading branch information
Ocramius authored Dec 6, 2021
2 parents 82347e1 + 1c96ac5 commit 42860ba
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ComposerRequireChecker/NodeVisitor/DefinedSymbolCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getDefinedSymbols(): array
public function enterNode(Node $node): Node
{
$this->recordClassDefinition($node);
$this->recordEnumDefinition($node);
$this->recordInterfaceDefinition($node);
$this->recordTraitDefinition($node);
$this->recordFunctionDefinition($node);
Expand All @@ -60,6 +61,15 @@ private function recordClassDefinition(Node $node): void
$this->recordDefinitionOf($node);
}

private function recordEnumDefinition(Node $node): void
{
if (! ($node instanceof Node\Stmt\Enum_)) {
return;
}

$this->recordDefinitionOf($node);
}

private function recordInterfaceDefinition(Node $node): void
{
if (! ($node instanceof Node\Stmt\Interface_)) {
Expand Down
16 changes: 16 additions & 0 deletions test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,20 @@ public function testNoUnknownComposerSymbolFound(): void
$this->commandTester->getDisplay()
);
}

/**
* @requires PHP >= 8.1.0
*/
public function testNoUnknownEnumSymbolsFound(): void
{
$this->commandTester->execute([
'composer-json' => dirname(__DIR__, 2) . '/fixtures/noUnknownEnumSymbols/composer.json',
]);

self::assertSame(Command::SUCCESS, $this->commandTester->getStatusCode());
self::assertStringContainsString(
'There were no unknown symbols found.',
$this->commandTester->getDisplay()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
Expand Down Expand Up @@ -74,6 +75,14 @@ public function testRecordClassDefinition(): void
$this->assertContains('Foo', $this->collector->getDefinedSymbols());
}

public function testRecordEnumDefinition(): void
{
$node = new Enum_(new Identifier('Foo'));
$this->traverser->traverse([$node]);

$this->assertContains('Foo', $this->collector->getDefinedSymbols());
}

public function testRecordInterfaceDefinition(): void
{
$node = new Interface_(new Identifier('Foo'));
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/noUnknownEnumSymbols/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "example/library",
"require": {
"php": "8.1.*"
},
"autoload": {
"psr-4": {
"Example\\Library\\": "src/"
}
}
}
10 changes: 10 additions & 0 deletions test/fixtures/noUnknownEnumSymbols/src/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Example\Library;

final class SomeClass
{
public readonly SomeEnum $enum = SomeEnum::A;
}
11 changes: 11 additions & 0 deletions test/fixtures/noUnknownEnumSymbols/src/SomeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Example\Library;

enum SomeEnum
{
case A;
case B;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit 42860ba

Please sign in to comment.