From 731c2da078b7b59190130ba35209319d87451f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=CC=88nther=20Debrauwer?= Date: Thu, 18 Jan 2024 15:15:32 +0100 Subject: [PATCH 1/3] Remove docblocks and add return typehints --- src/Commands/DownloadCommand.php | 27 +---- src/Commands/UploadCommand.php | 37 ++----- src/Poeditor/Poeditor.php | 50 ++-------- src/Poeditor/UploadResponse.php | 42 +------- src/PoeditorSyncServiceProvider.php | 10 +- src/Translations/TranslationManager.php | 126 +++--------------------- tests/DownloadCommandTest.php | 25 +---- tests/PoeditorTest.php | 19 +--- tests/TestCase.php | 21 +--- tests/UploadCommandTest.php | 49 +-------- 10 files changed, 52 insertions(+), 354 deletions(-) diff --git a/src/Commands/DownloadCommand.php b/src/Commands/DownloadCommand.php index d74c657..538c7d2 100644 --- a/src/Commands/DownloadCommand.php +++ b/src/Commands/DownloadCommand.php @@ -3,31 +3,17 @@ namespace NextApps\PoeditorSync\Commands; use Illuminate\Console\Command; +use Illuminate\Support\Collection; use NextApps\PoeditorSync\Poeditor\Poeditor; use NextApps\PoeditorSync\Translations\TranslationManager; class DownloadCommand extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ protected $signature = 'poeditor:download'; - /** - * The console command description. - * - * @var string - */ protected $description = 'Download translations from POEditor'; - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() + public function handle() : int { $this->getLocales()->each(function ($locale, $key) { $translations = app(Poeditor::class)->download(is_string($key) ? $key : $locale); @@ -38,14 +24,11 @@ public function handle() }); $this->info('All translations have been downloaded!'); + + return Command::SUCCESS; } - /** - * Get project locales. - * - * @return \Illuminate\Support\Collection - */ - protected function getLocales() + protected function getLocales() : Collection { return collect(config('poeditor-sync.locales')); } diff --git a/src/Commands/UploadCommand.php b/src/Commands/UploadCommand.php index f0fbc33..f7585a0 100644 --- a/src/Commands/UploadCommand.php +++ b/src/Commands/UploadCommand.php @@ -9,33 +9,18 @@ class UploadCommand extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ protected $signature = 'poeditor:upload {locale? : The language to upload translations from} {--force : Overwrite the existing POEditor translations}'; - /** - * The console command description. - * - * @var string - */ protected $description = 'Upload translations to POEditor'; - /** - * Execute the console command. - * - * @return mixed - */ - public function handle() + public function handle() : int { if ($this->getLocale() === null) { $this->error('Invalid locale provided!'); - return 1; + return Command::FAILURE; } $translations = app(TranslationManager::class)->getTranslations($this->getLocale()); @@ -52,30 +37,22 @@ public function handle() $this->line("{$response->getDeletedTermsCount()} terms deleted"); $this->line("{$response->getAddedTranslationsCount()} translations added"); $this->line("{$response->getUpdatedTranslationsCount()} translations updated"); + + return COMMAND::SUCCESS; } - /** - * Get locale that needs to be used to upload translations. - * - * @return string|null - */ - protected function getLocale() + protected function getLocale() : ?string { $locale = $this->argument('locale') ?? app()->getLocale(); if (! collect(config('poeditor-sync.locales'))->flatten()->contains($locale)) { - return; + return null; } return $locale; } - /** - * Get POEditor locale. - * - * @return string - */ - protected function getPoeditorLocale() + protected function getPoeditorLocale() : string { $locales = config('poeditor-sync.locales'); diff --git a/src/Poeditor/Poeditor.php b/src/Poeditor/Poeditor.php index 36d2289..6027882 100644 --- a/src/Poeditor/Poeditor.php +++ b/src/Poeditor/Poeditor.php @@ -7,30 +7,12 @@ class Poeditor { - /** - * @var \GuzzleHttp\Client - */ - protected $client; - - /** - * @var string - */ - protected $apiKey; - - /** - * @var string - */ - protected $projectId; - - /** - * Create a new manager instance. - * - * @param \GuzzleHttp\Client $client - * @param string $apiKey - * @param string $projectId - * - * @return void - */ + protected Client $client; + + protected string $apiKey; + + protected string $projectId; + public function __construct(Client $client, $apiKey, $projectId) { if (! is_string($apiKey) || ! $apiKey) { @@ -46,14 +28,7 @@ public function __construct(Client $client, $apiKey, $projectId) $this->projectId = $projectId; } - /** - * Download translations in the language. - * - * @param string $language - * - * @return array - */ - public function download(string $language) + public function download(string $language) : array { $projectResponse = $this->client ->post( @@ -80,16 +55,7 @@ public function download(string $language) return json_decode($exportResponse, true); } - /** - * Upload translations in the language. - * - * @param string $language - * @param array $translations - * @param bool $overwrite - * - * @return \NextApps\PoeditorSync\Poeditor\UploadResponse - */ - public function upload(string $language, array $translations, bool $overwrite = false) + public function upload(string $language, array $translations, bool $overwrite = false) : UploadResponse { $filename = stream_get_meta_data($file = tmpfile())['uri'] . '.json'; diff --git a/src/Poeditor/UploadResponse.php b/src/Poeditor/UploadResponse.php index a38117e..3b964d9 100644 --- a/src/Poeditor/UploadResponse.php +++ b/src/Poeditor/UploadResponse.php @@ -4,61 +4,29 @@ class UploadResponse { - /** - * The upload response content. - * - * @var array - */ - protected $content; + protected array $content; - /** - * Create a new POEditor upload response instance. - * - * @param array $content - * - * @return void - */ public function __construct(array $content) { $this->content = $content; } - /** - * Get the amount of terms that have been added. - * - * @return int - */ - public function getAddedTermsCount() + public function getAddedTermsCount() : int { return (int) $this->content['result']['terms']['added']; } - /** - * Get the amount of terms that have been deleted. - * - * @return int - */ - public function getDeletedTermsCount() + public function getDeletedTermsCount() : int { return (int) $this->content['result']['terms']['deleted']; } - /** - * Get the amount of translations that have been added. - * - * @return int - */ - public function getAddedTranslationsCount() + public function getAddedTranslationsCount() : int { return (int) $this->content['result']['translations']['added']; } - /** - * Get the amount of translations that have been updated. - * - * @return int - */ - public function getUpdatedTranslationsCount() + public function getUpdatedTranslationsCount() : int { return (int) $this->content['result']['translations']['updated']; } diff --git a/src/PoeditorSyncServiceProvider.php b/src/PoeditorSyncServiceProvider.php index 17787ab..c1e0fa9 100644 --- a/src/PoeditorSyncServiceProvider.php +++ b/src/PoeditorSyncServiceProvider.php @@ -10,10 +10,7 @@ class PoeditorSyncServiceProvider extends ServiceProvider { - /** - * Bootstrap the application services. - */ - public function boot() + public function boot() : void { if ($this->app->runningInConsole()) { $this->publishes([ @@ -27,10 +24,7 @@ public function boot() } } - /** - * Register the application services. - */ - public function register() + public function register() : void { $this->mergeConfigFrom(__DIR__ . '/../config/poeditor-sync.php', 'poeditor-sync'); diff --git a/src/Translations/TranslationManager.php b/src/Translations/TranslationManager.php index d1017a8..22d4cc7 100644 --- a/src/Translations/TranslationManager.php +++ b/src/Translations/TranslationManager.php @@ -9,31 +9,14 @@ class TranslationManager { - /** - * @var \Illuminate\Filesystem\Filesystem - */ - protected $filesystem; - - /** - * Create a new manager instance. - * - * @param \Illuminate\Filesystem\Filesystem $filesystem - * - * @return void - */ + protected Filesystem $filesystem; + public function __construct(Filesystem $filesystem) { $this->filesystem = $filesystem; } - /** - * Get translations of PHP and JSON translation files in the specified locale. - * - * @param string $locale - * - * @return array - */ - public function getTranslations(string $locale) + public function getTranslations(string $locale) : array { $translations = array_merge( $this->getPhpTranslations($this->getLangPath("/{$locale}")), @@ -47,15 +30,7 @@ public function getTranslations(string $locale) return $translations; } - /** - * Create translation files based on the provided array. - * - * @param array $translations - * @param string $locale - * - * @return void - */ - public function createTranslationFiles(array $translations, string $locale) + public function createTranslationFiles(array $translations, string $locale) : void { $this->createEmptyLocaleFolder($locale); @@ -67,14 +42,7 @@ public function createTranslationFiles(array $translations, string $locale) } } - /** - * Get translations of vendor translation files in the specified locale. - * - * @param string $locale - * - * @return array - */ - protected function getVendorTranslations(string $locale) + protected function getVendorTranslations(string $locale) : array { if (! $this->filesystem->exists($this->getLangPath('vendor'))) { return []; @@ -92,14 +60,7 @@ protected function getVendorTranslations(string $locale) return ['vendor' => $translations]; } - /** - * Get PHP translations from files in folder. - * - * @param string $folder - * - * @return array - */ - protected function getPhpTranslations(string $folder) + protected function getPhpTranslations(string $folder) : array { $files = collect($this->filesystem->files($folder)); @@ -123,14 +84,7 @@ protected function getPhpTranslations(string $folder) ->toArray(); } - /** - * Get JSON translations from file. - * - * @param string $filename - * - * @return array - */ - protected function getJsonTranslations(string $filename) + protected function getJsonTranslations(string $filename) : array { if (! $this->filesystem->exists($filename)) { return []; @@ -139,41 +93,17 @@ protected function getJsonTranslations(string $filename) return json_decode($this->filesystem->get($filename), true); } - /** - * Create PHP translation files containing arrays. - * - * @param array $translations - * @param string $locale - * - * @return void - */ - protected function createPhpTranslationFiles(array $translations, string $locale) + protected function createPhpTranslationFiles(array $translations, string $locale) : void { $this->createPhpFiles($this->getLangPath($locale), $translations); } - /** - * Create JSON translation file containing arrays. - * - * @param array $translations - * @param string $locale - * - * @return void - */ - protected function createJsonTranslationFile(array $translations, string $locale) + protected function createJsonTranslationFile(array $translations, string $locale) : void { $this->createJsonFile($this->getLangPath("/{$locale}.json"), $translations); } - /** - * Create vendor translation files. - * - * @param array $translations - * @param string $locale - * - * @return void - */ - protected function createVendorTranslationFiles(array $translations, string $locale) + protected function createVendorTranslationFiles(array $translations, string $locale) : void { if (! Arr::has($translations, 'vendor')) { return; @@ -193,15 +123,7 @@ protected function createVendorTranslationFiles(array $translations, string $loc } } - /** - * Create PHP translation files in folder. - * - * @param string $filename - * @param array $translations - * - * @return void - */ - protected function createPhpFiles(string $folder, array $translations) + protected function createPhpFiles(string $folder, array $translations) : void { $translations = Arr::where($translations, function ($translation) { return is_array($translation); @@ -221,15 +143,7 @@ protected function createPhpFiles(string $folder, array $translations) } } - /** - * Create JSON translation file on filename. - * - * @param string $filename - * @param array $translations - * - * @return void - */ - protected function createJsonFile(string $filename, array $translations) + protected function createJsonFile(string $filename, array $translations) : void { $translations = Arr::where($translations, function ($translation) { return is_string($translation); @@ -242,14 +156,7 @@ protected function createJsonFile(string $filename, array $translations) $this->filesystem->put($filename, json_encode($translations, JSON_PRETTY_PRINT)); } - /** - * Create empty folder for locale in "lang" resources folder (if folder does not exist yet). - * - * @param string $locale - * - * @return void - */ - protected function createEmptyLocaleFolder(string $locale) + protected function createEmptyLocaleFolder(string $locale) : void { if (! $this->filesystem->exists($this->getLangPath())) { $this->filesystem->makeDirectory($this->getLangPath()); @@ -270,13 +177,6 @@ protected function createEmptyLocaleFolder(string $locale) $this->filesystem->makeDirectory($path); } - /** - * Get language path. - * - * @param string|null $path - * - * @return string - */ protected function getLangPath(string $path = null) : string { if (function_exists('lang_path')) { diff --git a/tests/DownloadCommandTest.php b/tests/DownloadCommandTest.php index 2009ca9..b939ad7 100644 --- a/tests/DownloadCommandTest.php +++ b/tests/DownloadCommandTest.php @@ -8,11 +8,6 @@ class DownloadCommandTest extends TestCase { - /** - * Setup the test environment. - * - * @return void - */ protected function setUp() : void { parent::setUp(); @@ -409,15 +404,7 @@ public function mockPoeditorDownload(string $language, array $data) }); } - /** - * Assert that PHP translation file exists and contains data. - * - * @param string $filename - * @param array $data - * - * @return void - */ - public function assertPhpTranslationFile(string $filename, array $data) + public function assertPhpTranslationFile(string $filename, array $data) : void { $this->assertTrue(file_exists($filename)); $this->assertEquals( @@ -426,15 +413,7 @@ public function assertPhpTranslationFile(string $filename, array $data) ); } - /** - * Assert that JSON translation file exists and contains data. - * - * @param string $filename - * @param array $data - * - * @return void - */ - public function assertJsonTranslationFile(string $filename, array $data) + public function assertJsonTranslationFile(string $filename, array $data) : void { $this->assertTrue(file_exists($filename)); $this->assertEquals(json_encode($data, JSON_PRETTY_PRINT), file_get_contents($filename)); diff --git a/tests/PoeditorTest.php b/tests/PoeditorTest.php index 43f8a0e..2c9b616 100644 --- a/tests/PoeditorTest.php +++ b/tests/PoeditorTest.php @@ -13,21 +13,10 @@ class PoeditorTest extends TestCase { - /** - * @var \GuzzleHttp\Handler\MockHandler - */ - public $requestMockHandler; - - /** - * @var array - */ - public $requestHistoryContainer = []; - - /** - * Setup the test environment. - * - * @return void - */ + public MockHandler $requestMockHandler; + + public array $requestHistoryContainer = []; + protected function setUp() : void { parent::setUp(); diff --git a/tests/TestCase.php b/tests/TestCase.php index be0abf6..e0c4e6f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,11 +11,6 @@ class TestCase extends BaseTestCase { use WithFaker; - /** - * Setup the test environment. - * - * @return void - */ protected function setUp() : void { parent::setUp(); @@ -26,27 +21,13 @@ protected function setUp() : void config()->set('poeditor-sync.include_vendor', true); } - /** - * Register package providers. - * - * @param mixed $app - * - * @return array - */ - protected function getPackageProviders($app) + protected function getPackageProviders($app) : array { return [ PoeditorSyncServiceProvider::class, ]; } - /** - * Get language path. - * - * @param string|null $path - * - * @return string - */ protected function getLangPath(string $path = null) : string { if (function_exists('lang_path')) { diff --git a/tests/UploadCommandTest.php b/tests/UploadCommandTest.php index 4c8c070..fbba281 100644 --- a/tests/UploadCommandTest.php +++ b/tests/UploadCommandTest.php @@ -9,11 +9,6 @@ class UploadCommandTest extends TestCase { - /** - * Setup the test environment. - * - * @return void - */ protected function setUp() : void { parent::setUp(); @@ -292,16 +287,6 @@ public function it_throws_error_if_default_locale_is_not_present_in_config_local ->expectsOutput('Invalid locale provided!'); } - /** - * Mock the POEditor "upload" method. - * - * @param string $language - * @param array $translations - * @param bool $overwrite - * @param \NextApps\PoeditorSync\Poeditor\UploadResponse $response - * - * @return void - */ public function mockPoeditorUpload(string $language, array $translations, bool $overwrite = false, UploadResponse $response = null) { $this->mock(Poeditor::class, function ($mock) use ($language, $translations, $overwrite, $response) { @@ -311,15 +296,7 @@ public function mockPoeditorUpload(string $language, array $translations, bool $ }); } - /** - * Create PHP translation file. - * - * @param string $filename - * @param array $data - * - * @return void - */ - public function createPhpTranslationFile(string $filename, array $data) + public function createPhpTranslationFile(string $filename, array $data) : void { if (! app(Filesystem::class)->exists(dirname($filename))) { app(Filesystem::class)->makeDirectory(dirname($filename), 0755, true); @@ -331,15 +308,7 @@ public function createPhpTranslationFile(string $filename, array $data) ); } - /** - * Create JSON translation file exists and contains data. - * - * @param string $filename - * @param array $data - * - * @return void - */ - public function createJsonTranslationFile(string $filename, array $data) + public function createJsonTranslationFile(string $filename, array $data) : void { if (! app(Filesystem::class)->exists(dirname($filename))) { app(Filesystem::class)->makeDirectory(dirname($filename), 0755, true); @@ -348,20 +317,12 @@ public function createJsonTranslationFile(string $filename, array $data) file_put_contents($filename, json_encode($data, JSON_PRETTY_PRINT)); } - /** - * Get upload response. - * - * @param string $filename - * @param array $data - * - * @return \NextApps\PoeditorSync\Poeditor\UploadResponse - */ - public function getUploadResponse() + public function getUploadResponse() : UploadResponse { return new UploadResponse([ 'result' => [ - 'terms' => ['added' => $this->faker->randomNumber, 'deleted' => $this->faker->randomNumber], - 'translations' => ['added' => $this->faker->randomNumber, 'updated' => $this->faker->randomNumber], + 'terms' => ['added' => $this->faker->randomNumber(), 'deleted' => $this->faker->randomNumber()], + 'translations' => ['added' => $this->faker->randomNumber(), 'updated' => $this->faker->randomNumber()], ], ]); } From d31b51515af73844a7416f878c080df81c8174c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=CC=88nther=20Debrauwer?= Date: Thu, 18 Jan 2024 15:15:44 +0100 Subject: [PATCH 2/3] Fix faker deprecation warnings --- tests/PoeditorTest.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/PoeditorTest.php b/tests/PoeditorTest.php index 2c9b616..93c3841 100644 --- a/tests/PoeditorTest.php +++ b/tests/PoeditorTest.php @@ -38,7 +38,7 @@ public function it_requests_export_url_and_downloads_it_content() 'message' => 'OK', ], 'result' => [ - 'url' => $url = $this->faker->url, + 'url' => $url = $this->faker->url(), ], ])), new Response(200, [], json_encode([ @@ -46,7 +46,7 @@ public function it_requests_export_url_and_downloads_it_content() ])), ); - $translations = app(Poeditor::class)->download($locale = $this->faker->locale); + $translations = app(Poeditor::class)->download($locale = $this->faker->locale()); $this->assertEquals(['key' => 'value'], $translations); @@ -78,7 +78,7 @@ public function it_throws_an_error_if_api_key_is_empty() $this->expectExceptionMessage('Invalid API key'); config()->set('poeditor-sync.api_key', ''); - app(Poeditor::class)->download($this->faker->locale); + app(Poeditor::class)->download($this->faker->locale()); } /** @test */ @@ -88,7 +88,7 @@ public function it_throws_an_error_if_api_key_is_null() $this->expectExceptionMessage('Invalid API key'); config()->set('poeditor-sync.api_key', null); - app(Poeditor::class)->download($this->faker->locale); + app(Poeditor::class)->download($this->faker->locale()); } /** @test */ @@ -98,7 +98,7 @@ public function it_throws_an_error_if_project_id_is_empty() $this->expectExceptionMessage('Invalid project id'); config()->set('poeditor-sync.project_id', ''); - app(Poeditor::class)->download($this->faker->locale); + app(Poeditor::class)->download($this->faker->locale()); } /** @test */ @@ -108,7 +108,7 @@ public function it_throws_an_error_if_project_id_is_null() $this->expectExceptionMessage('Invalid project id'); config()->set('poeditor-sync.project_id', null); - app(Poeditor::class)->download($this->faker->locale); + app(Poeditor::class)->download($this->faker->locale()); } /** @test */ @@ -123,20 +123,20 @@ public function it_uploads_translations() ], 'result' => [ 'terms' => [ - 'parsed' => $this->faker->randomNumber, - 'added' => $this->faker->randomNumber, - 'deleted' => $this->faker->randomNumber, + 'parsed' => $this->faker->randomNumber(), + 'added' => $this->faker->randomNumber(), + 'deleted' => $this->faker->randomNumber(), ], 'translations' => [ - 'parsed' => $this->faker->randomNumber, - 'added' => $this->faker->randomNumber, - 'updated' => $this->faker->randomNumber, + 'parsed' => $this->faker->randomNumber(), + 'added' => $this->faker->randomNumber(), + 'updated' => $this->faker->randomNumber(), ], ], ])), ); - $response = app(Poeditor::class)->upload($this->faker->locale, ['key' => 'value']); + $response = app(Poeditor::class)->upload($this->faker->locale(), ['key' => 'value']); $this->assertInstanceOf(UploadResponse::class, $response); From ea41a2f3142d1e24631a143fccad1113a0f44b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=CC=88nther=20Debrauwer?= Date: Thu, 18 Jan 2024 15:21:50 +0100 Subject: [PATCH 3/3] Drop support for PHP 7.4 - 8.0 and Laravel 7 - 8 --- .github/workflows/run-tests.yml | 33 ++------------------------------- composer.json | 8 ++++---- 2 files changed, 6 insertions(+), 35 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e0f071a..3cec988 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -10,45 +10,16 @@ jobs: strategy: fail-fast: false matrix: - php: [7.3, 7.4, 8.0, 8.1, 8.2, 8.3] - laravel: [7.*, 8.*, 9.*, 10.*] + php: [8.1, 8.2, 8.3] + laravel: [9.*, 10.*] dependency-version: [prefer-lowest, prefer-stable] exclude: - php: 8.3 laravel: 9.* dependency-version: prefer-lowest - - php: 8.3 - laravel: 8.* - - php: 8.3 - laravel: 7.* - php: 8.2 laravel: 9.* dependency-version: prefer-lowest - - php: 8.2 - laravel: 8.* - - php: 8.2 - laravel: 7.* - - php: 8.1 - laravel: 8.* - dependency-version: prefer-lowest - - php: 8.1 - laravel: 7.* - - php: 8.0 - laravel: 8.* - dependency-version: prefer-lowest - - php: 8.0 - laravel: 7.* - dependency-version: prefer-lowest - - php: 8.0 - laravel: 10.* - - php: 7.4 - laravel: 10.* - - php: 7.4 - laravel: 9.* - - php: 7.3 - laravel: 10.* - - php: 7.3 - laravel: 9.* name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - ${{ matrix.dependency-version }} diff --git a/composer.json b/composer.json index fe3461e..ec0c44b 100644 --- a/composer.json +++ b/composer.json @@ -17,16 +17,16 @@ } ], "require": { - "php": "^7.3|^7.4|^8.0|^8.1|^8.2|^8.3", + "php": "^8.1|^8.2|^8.3", "guzzlehttp/guzzle": "^6.5|^7.0", - "illuminate/filesystem": "^7.0|^8.0|^9.0|^10.0", - "illuminate/support": "^7.0|^8.0|^9.0|^10.0", + "illuminate/filesystem": "^9.0|^10.0", + "illuminate/support": "^9.0|^10.0", "symfony/var-exporter": "^5.0|^6.0" }, "require-dev": { "adamwojs/php-cs-fixer-phpdoc-force-fqcn": "^2.0", "friendsofphp/php-cs-fixer": "^3.0", - "orchestra/testbench": "^5.0|^6.0|^7.0|^8.0", + "orchestra/testbench": "^7.0|^8.0", "phpunit/phpunit": "^9.1", "squizlabs/php_codesniffer": "^3.6" },