Skip to content

Commit

Permalink
Added support for generating Override attributes in generated enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Dec 16, 2023
1 parent 888a993 commit e2d86d3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/MimeMappingGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,29 @@ public function generateJson(bool $minify = true): string
}

/**
* @param non-empty-string $classname
* @param non-empty-string $namespace
* @param non-empty-string $classname The name of the generated enum (defaults to: "MimeType")
* @param non-empty-string $namespace The namespace to put the enum into (defaults to this namespace)
* @param bool|null $withOverrideAttribute Whether to add the #[Override] attribute to the generated enum (requires PHP 8.3, defaults to true if PHP >= 8.3, false otherwise)
* @return non-empty-string
*/
public function generatePhpEnum(string $classname = "MimeType", string $namespace = __NAMESPACE__): string
public function generatePhpEnum(string $classname = "MimeType", string $namespace = __NAMESPACE__, ?bool $withOverrideAttribute = null): string
{
$usages = [];
if ($namespace !== __NAMESPACE__) {
$usages[] = "use " . MimeTypeInterface::class . ";";
}

$overrideAttribute = '';
if (($withOverrideAttribute === null && PHP_VERSION_ID >= 80300) || $withOverrideAttribute) {
$usages[] = "use Override;";
$overrideAttribute = "\n\t#[Override]";
}

$values = [
'namespace' => $namespace,
'classname' => $classname,
'interface_usage' => $namespace !== __NAMESPACE__ ? ("use " . MimeTypeInterface::class . ";\n") : '',
'usages' => implode("\n", $usages),
'overrideAttribute' => $overrideAttribute,
'cases' => "",
'type2ext' => "",
'ext2type' => "",
Expand Down
7 changes: 4 additions & 3 deletions stubs/mimeType.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ namespace %namespace%;

use RuntimeException;
use InvalidArgumentException;
%interface_usage%
%usages%

enum %classname%: string implements MimeTypeInterface
{
%cases%
%cases%%overrideAttribute%
public function getExtension(): string
{
return match($this) {
%type2ext%
default => throw new RuntimeException("Unknown extension for type: " . $this->value),
};
}

%overrideAttribute%
public function getValue(): string
{
return $this->value;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/MimeMappingGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testGeneratePhpEnum(): void
EOF
);

$phpEnum = $generator->generatePhpEnum("TestMimeClass", "TestMimeNamespace");
$phpEnum = $generator->generatePhpEnum("TestMimeClass", "TestMimeNamespace", withOverrideAttribute: false);

self::assertEquals(<<<EOF
<?php
Expand Down

0 comments on commit e2d86d3

Please sign in to comment.