Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsizemore committed Apr 25, 2024
1 parent a949e6f commit 03023cf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 105 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Updated dev dependencies.
* `Generator::generatePhpEnum()` can now throw a `RuntimeException` if `generateMapping` returns an empty array.
* Refactored `Generator::generateMapping()` make use of a helper function to reduce function complexity.

### Fixed

Expand Down
53 changes: 27 additions & 26 deletions src/Mapping/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,7 @@ public function generateMapping(): array
static fn (string $value): bool => trim($value) !== ''
));

if (\count($parts) === 2) {
$mime = trim($parts[0]);
$extensions = explode(' ', $parts[1]);

foreach ($extensions as $extension) {
$extension = trim($extension);

if ($mime !== '' && $extension !== '') {
$this->mapCache['mimes'][$extension][] = $mime;
$this->mapCache['extensions'][$mime][] = $extension;
$this->mapCache['mimes'][$extension] = array_unique($this->mapCache['mimes'][$extension]);
$this->mapCache['extensions'][$mime] = array_unique($this->mapCache['extensions'][$mime]);

}
}
}
$this->generateMapCache($parts);
}

return $this->mapCache;
Expand All @@ -144,23 +129,15 @@ public function generateMapping(): array
/**
* Generates the PHP Enum found in `dist`.
*
* @param string $classname
* @param string $namespace
* @param non-empty-string $classname
* @param non-empty-string $namespace
*
* @throws RuntimeException
*
* @return string
*/
public function generatePhpEnum(string $classname = 'MimeType', string $namespace = 'Esi\Mimey'): string
{
if ($classname === '') {
$classname = 'MimeType';
}

if ($namespace === '') {
$namespace = 'Esi\Mimey';
}

$values = [
'namespace' => $namespace,
'classname' => $classname,
Expand Down Expand Up @@ -210,6 +187,30 @@ protected function convertMimeTypeToCaseName(string $mimeType): string
return $mimeType;
}

/**
* Helper function for self::generateMapping()
*
* @param list<string> $parts
*/
protected function generateMapCache(array $parts): void
{
if (\count($parts) === 2) {
$mime = trim($parts[0]);
$extensions = explode(' ', $parts[1]);

foreach ($extensions as $extension) {
$extension = trim($extension);

if ($mime !== '' && $extension !== '') {
$this->mapCache['mimes'][$extension][] = $mime;
$this->mapCache['extensions'][$mime][] = $extension;
$this->mapCache['mimes'][$extension] = array_unique($this->mapCache['mimes'][$extension]);
$this->mapCache['extensions'][$mime] = array_unique($this->mapCache['extensions'][$mime]);
}
}
}
}

/**
* Helper function for self::generatePhpEnum().
*/
Expand Down
79 changes: 0 additions & 79 deletions tests/src/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,85 +200,6 @@ public static function tryFromExtension(string \$extension): ?MimeType
);
}

/**
* Test generating the PHP Enum given empty classname and namespace.
*/
public function testGeneratePhpEnumEmptyParams(): void
{
$generator = new Generator(
<<<EOF
#ignore
application/json\tjson
image/jpeg\tjpeg jpg
EOF
);

$phpEnum = $generator->generatePhpEnum('', '');

self::assertSame(
<<<EOF
<?php
/**
* @generated enum generated using bin/generate.php, please DO NOT EDIT!
*
* @codeCoverageIgnore
*/
declare(strict_types=1);
namespace Esi\Mimey;
use InvalidArgumentException;
use Esi\Mimey\Interface\MimeTypeInterface;
enum MimeType: string implements MimeTypeInterface
{
case ApplicationJson = 'application/json';
case ImageJpeg = 'image/jpeg';
#[\Override]
public function getExtension(): string
{
return match(\$this) {
self::ApplicationJson => 'json',
self::ImageJpeg => 'jpeg',
};
}
#[\Override]
public function getValue(): string
{
return \$this->value;
}
public static function fromExtension(string \$extension): MimeType
{
\$type = self::tryFromExtension(\$extension);
if (\$type === null) {
throw new InvalidArgumentException('Unknown extension: ' . \$extension);
}
return \$type;
}
public static function tryFromExtension(string \$extension): ?MimeType
{
return match(\$extension) {
'json' => self::ApplicationJson,
'jpeg' => self::ImageJpeg,
'jpg' => self::ImageJpeg,
default => null,
};
}
}
EOF,
$phpEnum
);
}

/**
* Test generating the PHP Enum when given invalid mime.types text.
*/
Expand Down

0 comments on commit 03023cf

Please sign in to comment.