From 90fd5e271d739ea5cac9b3a49562e5fdc1843cb8 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Sat, 13 Apr 2024 16:34:59 +0300 Subject: [PATCH] docs: increase PhpStan documentation level --- ruleset-phpstan.neon | 2 +- src/Controllers/EnvController.php | 5 +++- src/Helpers/EntryObj.php | 5 +++- src/Helpers/EnvFileContentManager.php | 3 ++- src/Helpers/EnvKeysManager.php | 8 +++--- tests/Feature/ConfigurationTest.php | 3 ++- tests/TestCase.php | 6 +++-- tests/Unit/Helpers/EnvKeysManagerTest.php | 30 +++++++++++++---------- tests/Unit/Helpers/FilesManagerTest.php | 2 +- 9 files changed, 39 insertions(+), 25 deletions(-) diff --git a/ruleset-phpstan.neon b/ruleset-phpstan.neon index 0ba63e7..5971f0c 100644 --- a/ruleset-phpstan.neon +++ b/ruleset-phpstan.neon @@ -5,7 +5,7 @@ parameters: - src - tests - level: 6 + level: 7 reportStaticMethodSignatures: true parallel: jobSize: 20 diff --git a/src/Controllers/EnvController.php b/src/Controllers/EnvController.php index 4f9ec4f..dfb6aa7 100644 --- a/src/Controllers/EnvController.php +++ b/src/Controllers/EnvController.php @@ -6,6 +6,7 @@ use GeoSot\EnvEditor\ServiceProvider; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Http\UploadedFile; use Illuminate\Routing\Controller as BaseController; use Illuminate\Support\Facades\Artisan; use Illuminate\View\View; @@ -127,7 +128,9 @@ public function upload(Request $request): JsonResponse ]); $replaceCurrentEnv = filter_var($request->input('replace_current'), FILTER_VALIDATE_BOOLEAN); - $file = EnvEditor::upload($request->file('file'), $replaceCurrentEnv); + /** @var UploadedFile $uploadedFile */ + $uploadedFile = $request->file('file'); + $file = EnvEditor::upload($uploadedFile, $replaceCurrentEnv); $successMsg = ($replaceCurrentEnv) ? 'currentEnvWasReplacedByTheUploadedFile' : 'uploadedFileSavedAsBackup'; return $this->returnGenericResponse(true, [], $successMsg, $file->getFilename()); diff --git a/src/Helpers/EntryObj.php b/src/Helpers/EntryObj.php index 8e52f2e..c088015 100644 --- a/src/Helpers/EntryObj.php +++ b/src/Helpers/EntryObj.php @@ -75,7 +75,10 @@ public function setValue(mixed $value): void */ public function toArray(): array { - return get_object_vars($this); + /** @var array{key:string, value: int|string|null, group:int, index:int , isSeparator:bool} $result */ + $result = get_object_vars($this); + + return $result; } /** diff --git a/src/Helpers/EnvFileContentManager.php b/src/Helpers/EnvFileContentManager.php index 9640328..b462376 100644 --- a/src/Helpers/EnvFileContentManager.php +++ b/src/Helpers/EnvFileContentManager.php @@ -29,11 +29,12 @@ public function __construct(EnvEditor $envEditor, Filesystem $filesystem) */ public function getParsedFileContent(string $fileName = ''): Collection { + /** @var list $content */ $content = preg_split('/(\r\n|\r|\n)/', $this->getFileContents($fileName)); $groupIndex = 1; /** @var Collection $collection */ - $collection = collect([]); + $collection = new Collection(); foreach ($content as $index => $line) { $entryObj = EntryObj::parseEnvLine($line, $groupIndex, $index); $collection->push($entryObj); diff --git a/src/Helpers/EnvKeysManager.php b/src/Helpers/EnvKeysManager.php index 6ee04f0..f8caf72 100644 --- a/src/Helpers/EnvKeysManager.php +++ b/src/Helpers/EnvKeysManager.php @@ -38,7 +38,7 @@ public function get(string $key, mixed $default = null): float|bool|int|string|n /** * Add the Key on the Current Env. * - * @param array $options + * @param array{index?: int|string|null, group?: int|string|null} $options * * @throws EnvException */ @@ -57,14 +57,14 @@ public function add(string $key, mixed $value, array $options = []): bool $env->push($separator); } - $lastSameGroupIndex = $env->last(function (EntryObj $entry, $key) use ($givenGroup) { - return explode('_', $entry->key, 2)[0] == strtoupper($givenGroup) && null !== $entry->key; + $lastSameGroupEntry = $env->last(function (EntryObj $entry) use ($givenGroup) { + return explode('_', $entry->key, 2)[0] == strtoupper($givenGroup) && $entry->isSeparator(); }); $index = Arr::get( $options, 'index', - $env->search($lastSameGroupIndex) ? $env->search($lastSameGroupIndex) + 0.1 : $env->count() + 2 + $lastSameGroupEntry ? $lastSameGroupEntry->index + 0.1 : $env->count() + 2 ); $entryObj = new EntryObj($key, $value, $groupIndex, $index); diff --git a/tests/Feature/ConfigurationTest.php b/tests/Feature/ConfigurationTest.php index df80f29..4445610 100644 --- a/tests/Feature/ConfigurationTest.php +++ b/tests/Feature/ConfigurationTest.php @@ -3,6 +3,7 @@ namespace GeoSot\EnvEditor\Tests\Feature; use GeoSot\EnvEditor\Tests\TestCase; +use Illuminate\Config\Repository; use Illuminate\Support\Facades\Route; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\TestWith; @@ -14,7 +15,7 @@ class ConfigurationTest extends TestCase #[TestWith([true])] public function can_disable_routes(bool $enableRoutes): void { - $this->app['config']->set('env-editor.route.enable', $enableRoutes); + $this->app->make(Repository::class)->set('env-editor.route.enable', $enableRoutes); $routeNames = [ '.index', diff --git a/tests/TestCase.php b/tests/TestCase.php index fd1f28c..98d0132 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -47,13 +47,15 @@ protected function getPackageAliases($app): array protected static function getTestPath(): string { - return realpath(__DIR__.'/fixtures'); + return __DIR__.'/fixtures'; } protected static function getTestFile(bool $fullPath = false): string { $file = '.env.example'; - return $fullPath ? static::getTestPath().DIRECTORY_SEPARATOR.$file : $file; + return $fullPath + ? static::getTestPath().DIRECTORY_SEPARATOR.$file + : $file; } } diff --git a/tests/Unit/Helpers/EnvKeysManagerTest.php b/tests/Unit/Helpers/EnvKeysManagerTest.php index e037ef1..c68d0f3 100644 --- a/tests/Unit/Helpers/EnvKeysManagerTest.php +++ b/tests/Unit/Helpers/EnvKeysManagerTest.php @@ -15,7 +15,7 @@ class EnvKeysManagerTest extends TestCase { protected function setUp(): void { - parent::setUp(); // TODO: Change the autogenerated stub + parent::setUp(); $this->app['config']->set('env-editor.paths.env', self::getTestPath()); $this->app['config']->set('env-editor.envFileName', self::getTestFile()); } @@ -49,15 +49,17 @@ public function deletes_keys(): void $fullPath = $this->createNewDummyFile($fileName); $this->app['config']->set('env-editor.envFileName', $fileName); - self::assertStringContainsString('LOG_CHANNEL', file_get_contents($fullPath)); + $getContent = fn (): string => file_get_contents($fullPath) ?: throw new \RuntimeException("File {$fullPath}, not found"); + + self::assertStringContainsString('LOG_CHANNEL', $getContent()); self::assertTrue($this->getEnvKeysManager()->delete('LOG_CHANNEL')); - self::assertStringNotContainsString('LOG_CHANNEL=stack', file_get_contents($fullPath)); + self::assertStringNotContainsString('LOG_CHANNEL=stack', $getContent()); - self::assertStringContainsString('CACHE_DRIVER', file_get_contents($fullPath)); + self::assertStringContainsString('CACHE_DRIVER', $getContent()); self::assertTrue($this->getEnvKeysManager()->delete('CACHE_DRIVER')); - self::assertStringNotContainsString('CACHE_DRIVER="file"', file_get_contents($fullPath)); + self::assertStringNotContainsString('CACHE_DRIVER="file"', $getContent()); - self::assertStringNotContainsString('CACHE_DRIVER', file_get_contents($fullPath)); + self::assertStringNotContainsString('CACHE_DRIVER', $getContent()); try { $this->getEnvKeysManager()->delete('CACHE_DRIVER'); } catch (\Exception $e) { @@ -73,21 +75,23 @@ public function edits_keys(): void $fullPath = $this->createNewDummyFile($fileName); $this->app['config']->set('env-editor.envFileName', $fileName); - self::assertStringContainsString('LOG_CHANNEL=stack', file_get_contents($fullPath)); + $getContent = fn (): string => file_get_contents($fullPath) ?: throw new \RuntimeException("File {$fullPath}, not found"); + + self::assertStringContainsString('LOG_CHANNEL=stack', $getContent()); self::assertTrue($this->getEnvKeysManager()->edit('LOG_CHANNEL', 'foo')); - self::assertStringContainsString('LOG_CHANNEL=foo', file_get_contents($fullPath)); + self::assertStringContainsString('LOG_CHANNEL=foo', $getContent()); - self::assertStringContainsString('CACHE_DRIVER="file"', file_get_contents($fullPath)); + self::assertStringContainsString('CACHE_DRIVER="file"', $getContent()); self::assertTrue($this->getEnvKeysManager()->edit('CACHE_DRIVER', '"bar"')); - self::assertStringContainsString('CACHE_DRIVER="bar"', file_get_contents($fullPath)); + self::assertStringContainsString('CACHE_DRIVER="bar"', $getContent()); self::assertTrue($this->getEnvKeysManager()->edit('CACHE_DRIVER', '')); - self::assertStringContainsString('CACHE_DRIVER=', file_get_contents($fullPath)); + self::assertStringContainsString('CACHE_DRIVER=', $getContent()); self::assertTrue($this->getEnvKeysManager()->edit('CACHE_DRIVER', null)); - self::assertStringContainsString('CACHE_DRIVER=', file_get_contents($fullPath)); + self::assertStringContainsString('CACHE_DRIVER=', $getContent()); - self::assertStringNotContainsString('WRONG_KEY', file_get_contents($fullPath)); + self::assertStringNotContainsString('WRONG_KEY', $getContent()); try { $this->getEnvKeysManager()->edit('WRONG_KEY', 'fail'); } catch (\Exception $e) { diff --git a/tests/Unit/Helpers/FilesManagerTest.php b/tests/Unit/Helpers/FilesManagerTest.php index 7464f3d..3f33647 100644 --- a/tests/Unit/Helpers/FilesManagerTest.php +++ b/tests/Unit/Helpers/FilesManagerTest.php @@ -184,7 +184,7 @@ private function createAndTestPath(string $path): void { $path = realpath($path); $this->assertNotFalse($path); - $filename = tempnam($path, 'test'); + $filename = tempnam($path, 'test') ?: throw new \RuntimeException("Couldn't create file"); $this->assertEquals($filename, realpath($filename)); unlink($filename); }