Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: increase PhpStan documentation level #46

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ruleset-phpstan.neon
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ parameters:
- src
- tests

level: 6
level: 7
reportStaticMethodSignatures: true
parallel:
jobSize: 20
5 changes: 4 additions & 1 deletion src/Controllers/EnvController.php
Original file line number Diff line number Diff line change
@@ -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());
5 changes: 4 additions & 1 deletion src/Helpers/EntryObj.php
Original file line number Diff line number Diff line change
@@ -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;
}

/**
3 changes: 2 additions & 1 deletion src/Helpers/EnvFileContentManager.php
Original file line number Diff line number Diff line change
@@ -29,11 +29,12 @@ public function __construct(EnvEditor $envEditor, Filesystem $filesystem)
*/
public function getParsedFileContent(string $fileName = ''): Collection
{
/** @var list<string> $content */
$content = preg_split('/(\r\n|\r|\n)/', $this->getFileContents($fileName));

$groupIndex = 1;
/** @var Collection<int, EntryObj> $collection */
$collection = collect([]);
$collection = new Collection();
foreach ($content as $index => $line) {
$entryObj = EntryObj::parseEnvLine($line, $groupIndex, $index);
$collection->push($entryObj);
8 changes: 4 additions & 4 deletions src/Helpers/EnvKeysManager.php
Original file line number Diff line number Diff line change
@@ -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<string, int|string> $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);
3 changes: 2 additions & 1 deletion tests/Feature/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -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',
6 changes: 4 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
30 changes: 17 additions & 13 deletions tests/Unit/Helpers/EnvKeysManagerTest.php
Original file line number Diff line number Diff line change
@@ -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) {
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/FilesManagerTest.php
Original file line number Diff line number Diff line change
@@ -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);
}