Skip to content

Commit

Permalink
docs: increase PhpStan documentation level (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot authored Apr 13, 2024
1 parent 0b9f121 commit 5558764
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion ruleset-phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ parameters:
- src
- tests

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

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Helpers/EnvFileContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/Helpers/EnvKeysManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/FilesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 5558764

Please sign in to comment.