Skip to content

Commit

Permalink
fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Oct 23, 2023
1 parent bd1e1db commit d2148c6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Enums/GetsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static function getDescription(self $enum): string

/**
* Get enum as select array using descriptions (enum values as array keys)
*
*
* @return array<string, string>
*/
public static function asSelectArray(): array
Expand Down
30 changes: 19 additions & 11 deletions src/Utils/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* Parse HTTP query parameters to be in an associative array.
*
*
* This method does require the package "symfony/http-foundation".
*/
function parse_http_query(string $query = null): array
Expand All @@ -17,32 +17,32 @@ function parse_http_query(string $query = null): array

foreach ($queryParameters as $key => $value) {
$parameterKey = array_key_first($value);

$parameterGroup = $key;

if (substr($parameterGroup, 0, 1) === '?') {
$parameterGroup = substr($parameterGroup, 1, strlen($parameterGroup));
$parameterGroup = substr($parameterGroup, 1, strlen($parameterGroup));
}

if (! isset($processedQueryParameters[$parameterGroup])) {
$processedQueryParameters[$parameterGroup] = [];
$processedQueryParameters[$parameterGroup] = [];
}

if (isset($processedQueryParameters[$parameterGroup][$parameterKey]) && is_array($value[$parameterKey])) {
$processedQueryParameters[$parameterGroup][$parameterKey] = array_merge_recursive(
$value[$parameterKey],
$processedQueryParameters[$parameterGroup][$parameterKey] ?? []
);

continue;
}

if (isset($processedQueryParameters[$parameterGroup][$parameterKey]) && is_string($value[$parameterKey])) {
$processedQueryParameters[$parameterGroup][$parameterKey] = implode(',', array_merge(
explode(',', $value[$parameterKey]),
explode(',', $processedQueryParameters[$parameterGroup][$parameterKey])
));

continue;
}

Expand All @@ -62,18 +62,26 @@ function build_http_query(array $query): string
$iterator = function ($value, $key, $aggregator) use (&$resultQuery, &$iterator) {
$paremeterKey = $aggregator ?? $key;

if ($aggregator && ! is_integer($key)) {
if ($aggregator && ! is_int($key)) {
$paremeterKey = "{$aggregator}[{$key}]";
}

if (is_array($value) && (is_string(array_key_first($value)) || (! is_string(array_key_first($value)) && is_array($value[0]) && is_string(array_key_first($value[0]))))) {
if (
is_array($value)
&& (is_string(array_key_first($value))
|| (! is_string(array_key_first($value))

Check failure on line 72 in src/Utils/functions.php

View workflow job for this annotation

GitHub Actions / PHPStan

Negated boolean expression is always true.

Check failure on line 72 in src/Utils/functions.php

View workflow job for this annotation

GitHub Actions / PHPStan

Negated boolean expression is always true.
&& is_array($value[0])
&& is_string(array_key_first($value[0]))
)
)
) {
array_walk($value, $iterator, $paremeterKey);

return;
}

$value = (array) $value;

array_walk_recursive($value, function ($value) use (&$resultQuery, $paremeterKey) {
$resultQuery[] = urlencode($paremeterKey).'='.urlencode($value);
});
Expand Down
10 changes: 5 additions & 5 deletions tests/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use Illuminate\Foundation\Testing\Concerns\InteractsWithDeprecationHandling;
use Illuminate\Foundation\Testing\Concerns\InteractsWithExceptionHandling;
use Throwable;
use function OpenSoutheners\LaravelHelpers\Strings\get_email_domain;
use function OpenSoutheners\LaravelHelpers\Strings\is_json;
use function OpenSoutheners\LaravelHelpers\Strings\is_json_structure;
use PHPUnit\Framework\TestCase;
use Throwable;

class StringsTest extends TestCase
{
Expand All @@ -22,8 +22,8 @@ public function test_is_json(): void
$this->assertTrue(is_json('[{}]'));
$this->assertTrue(is_json('{"foo": "bar"}'));
$this->assertTrue(is_json('[{"foo": "bar"}]'));
$this->assertTrue(is_json("0"));
$this->assertTrue(is_json("\"hello\""));
$this->assertTrue(is_json('0'));
$this->assertTrue(is_json('"hello"'));
$this->assertFalse(is_json("{\u0022foo\u0022: \u0022bar\u0022}"));
$this->assertFalse(is_json([]));
$this->assertFalse(is_json(new \stdClass()));
Expand Down Expand Up @@ -53,8 +53,8 @@ public function test_is_json_structure()
$this->assertTrue(is_json_structure('[{"foo": "bar"}]'));
$this->assertFalse(is_json_structure('{"foo": "bar"]'));
$this->assertFalse(is_json_structure('[{"foo": "bar"]]'));
$this->assertFalse(is_json_structure("0"));
$this->assertFalse(is_json_structure("\"hello\""));
$this->assertFalse(is_json_structure('0'));
$this->assertFalse(is_json_structure('"hello"'));
$this->assertFalse(is_json_structure("{\u0022foo\u0022: \u0022bar\u0022}"));
$this->assertFalse(is_json_structure([]));
$this->assertFalse(is_json_structure(new \stdClass()));
Expand Down
4 changes: 2 additions & 2 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace OpenSoutheners\LaravelHelpers\Tests;

use PHPUnit\Framework\TestCase;
use function OpenSoutheners\LaravelHelpers\Utils\parse_http_query;
use function OpenSoutheners\LaravelHelpers\Utils\build_http_query;
use function OpenSoutheners\LaravelHelpers\Utils\parse_http_query;
use PHPUnit\Framework\TestCase;

class UtilsTest extends TestCase
{
Expand Down

0 comments on commit d2148c6

Please sign in to comment.