diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..144d20b --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @apfelbox \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5cb8ec5 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: CI + +on: [ push ] + +jobs: + build-test: + runs-on: ubuntu-latest + + strategy: + matrix: + php: ['8.4'] + + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + coverage: "none" + ini-values: "memory_limit=-1" + php-version: "${{ matrix.php }}" + tools: "composer" + + - name: Display versions + run: | + php --version + php -r 'foreach (get_loaded_extensions() as $extension) echo $extension . " " . phpversion($extension) . PHP_EOL;' + php -i + + - name: Install Composer + run: composer install --optimize-autoloader --classmap-authoritative --no-interaction + + - name: Run Linters + run: composer run-script lint + env: + PHP_CS_FIXER_IGNORE_ENV: 1 + + - name: Run Tests + run: composer run-script test diff --git a/phpstan.neon b/phpstan.neon index cd0577a..c97c69f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,6 +3,6 @@ includes: # If you use simple-phpunit, you need to uncomment the following line. # Always make sure to first run simple-phpunit and then PHPStan. -# parameters: -# bootstrapFiles: -# - vendor/bin/.phpunit/phpunit/vendor/autoload.php +parameters: + bootstrapFiles: + - vendor/bin/.phpunit/phpunit/vendor/autoload.php diff --git a/src/Snail/SnailHelper.php b/src/Snail/Snailer.php similarity index 93% rename from src/Snail/SnailHelper.php rename to src/Snail/Snailer.php index a457799..1fa1842 100644 --- a/src/Snail/SnailHelper.php +++ b/src/Snail/Snailer.php @@ -3,14 +3,16 @@ namespace Torr\Snail\Snail; use Symfony\Component\String\Slugger\AsciiSlugger; -use Symfony\Component\String\UnicodeString; use Torr\Snail\Exception\SnailGenerationFailedException; + use function Symfony\Component\String\u; /** + * Main class that helps generating and validation snails + * * @final */ -class SnailHelper +class Snailer { private const LOCALE_TO_TRANSLITERATOR_ID = [ 'am' => 'Amharic-Latin', @@ -45,7 +47,7 @@ class SnailHelper /** * Cache of transliterators per locale. * - * @var \Transliterator[] + * @var array */ private array $transliterators = []; @@ -69,6 +71,7 @@ public function generateSnail (string $text, ?string $locale = null) : string : $string->lower(); $transliterator = []; + if ($locale && ('de' === $locale || str_starts_with($locale, 'de_'))) { // Use the shortcut for German in UnicodeString::ascii() if possible (faster and no requirement on intl) @@ -89,7 +92,7 @@ public function generateSnail (string $text, ?string $locale = null) : string if ("" === $transformed) { throw new SnailGenerationFailedException( - sprintf( + \sprintf( "Could not generate snail from text '%s', as the result would be empty.", $text, ), @@ -114,8 +117,8 @@ private function createTransliterator (string $locale) : ?\Transliterator // Exact locale supported, cache and return if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$locale] ?? null) { - return $this->transliterators[$locale] = - \Transliterator::create($id . '/BGN') ?? \Transliterator::create($id); + return $this->transliterators[$locale] + = \Transliterator::create($id . '/BGN') ?? \Transliterator::create($id); } // Locale is not supported and there is no parent, fallback to any-latin diff --git a/tests/Snail/SnailHelperTest.php b/tests/Snail/SnailerTest.php similarity index 86% rename from tests/Snail/SnailHelperTest.php rename to tests/Snail/SnailerTest.php index e805e86..a945f68 100644 --- a/tests/Snail/SnailHelperTest.php +++ b/tests/Snail/SnailerTest.php @@ -2,17 +2,20 @@ namespace Tests\Torr\Snail\Snail; -use Torr\Snail\Exception\SnailGenerationFailedException; -use Torr\Snail\Snail\SnailHelper; use PHPUnit\Framework\TestCase; +use Torr\Snail\Exception\SnailGenerationFailedException; +use Torr\Snail\Snail\Snailer; -class SnailHelperTest extends TestCase +/** + * @internal + */ +final class SnailerTest extends TestCase { /** * */ public static function provideIsValid () : iterable - { + { yield "plain" => ["test"]; yield "with dash" => ["a-b"]; yield "with underscore" => ["a-b_c"]; @@ -26,14 +29,14 @@ public static function provideIsValid () : iterable */ public function testIsValid (string $input) : void { - self::assertTrue(SnailHelper::isValidSnail($input)); + self::assertTrue(Snailer::isValidSnail($input)); } /** * */ public static function provideIsInvalid () : iterable - { + { yield "empty" => [""]; yield "dash at the end" => ["test-"]; yield "dot at the end" => ["test."]; @@ -51,11 +54,9 @@ public static function provideIsInvalid () : iterable */ public function testIsInvalid (string $input) : void { - self::assertFalse(SnailHelper::isValidSnail($input)); + self::assertFalse(Snailer::isValidSnail($input)); } - - /** * */ @@ -65,13 +66,12 @@ public static function provideGenerateValid () : iterable yield "umlauts" => ["äöü", "aou"]; } - /** * @dataProvider provideGenerateValid */ public function testGenerateValid (string $input, string $expected) : void { - $helper = new SnailHelper(); + $helper = new Snailer(); self::assertSame( $expected, @@ -88,19 +88,18 @@ public static function provideGenerateInvalid () : iterable yield "invalid-chars" => ["@"]; } - /** * @dataProvider provideGenerateInvalid */ public function testGenerateInvalid (string $input) : void { $this->expectException(SnailGenerationFailedException::class); - $this->expectExceptionMessage(sprintf( + $this->expectExceptionMessage(\sprintf( "Could not generate snail from text '%s', as the result would be empty.", $input, )); - $helper = new SnailHelper(); + $helper = new Snailer(); $helper->generateSnail($input); } }