Skip to content

Commit

Permalink
Refactor code for clarity and readability
Browse files Browse the repository at this point in the history
This commit performs a series of refactorings across multiple files with the goal of improving readability. Changes mainly consist of adjustments to alignment, line break placement, and usage of boolean conditions to ensure code consistency and enhance comprehension, with no changes to the underlying logic. The removal of redundant whitespace and comments also helps to minimize visual clutter. Update also ensures adherence to coding style guidelines.
  • Loading branch information
Radiergummi committed Aug 22, 2023
1 parent 67bd4a7 commit 6b505ac
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 288 deletions.
11 changes: 11 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@
<directory name="vendor"/>
<directory name="src/pagination"/>
<directory name="src/Commands"/>
<file name="src/ScoutEngine.php"/>
<file name="src/Request.php"/>
<file name="src/Exceptions/DocumentNotFoundException.php"/>
</ignoreFiles>
</projectFiles>

<issueHandlers>
<DeprecatedMethod errorLevel="suppress"/>
<DeprecatedProperty errorLevel="suppress"/>

<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<file name="src/Model.php"/>
</errorLevel>
</PropertyNotSetInConstructor>

<!--
Thanks to the ridiculous PSR-6 spec which refuses to just use proper exceptions for bogus reasons, we'll have
Expand Down
27 changes: 15 additions & 12 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,32 @@
use Illuminate\Support\Collection as BaseCollection;
use JsonException;
use stdClass;

use function array_map;
use function is_array;
use function json_encode;

/**
* Collection
*
* @template-covariant T of Model
* @extends BaseCollection<array-key, T>
* @psalm-suppress TooManyTemplateParams
* @template TKey of array-key
* @template-covariant TValue of Model
* @extends BaseCollection<TKey, TValue>
* @package Matchory\Elasticsearch
*/
class Collection extends BaseCollection
{
/**
* Collection constructor.
*
* @param iterable $items
* @param int|null $total
* @param float|null $maxScore
* @param float|null $duration
* @param bool|null $timedOut
* @param string|null $scrollId
* @param iterable $items
* @param int|null $total
* @param float|null $maxScore
* @param float|null $duration
* @param bool|null $timedOut
* @param string|null $scrollId
* @param stdClass|null $shards
* @param array|null $suggestions
* @param array|null $aggregations
* @param array|null $suggestions
* @param array|null $aggregations
*/
public function __construct(
iterable $items = [],
Expand Down Expand Up @@ -129,6 +128,10 @@ public function isTimedOut(): bool|null
return $this->timedOut;
}

/**
* @inheritDoc
* @psalm-suppress DocblockTypeContradiction
*/
public function toArray(): array
{
return array_map(static function ($item) {
Expand Down
26 changes: 12 additions & 14 deletions src/Concerns/AppliesScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Support\Arr;
use Matchory\Elasticsearch\Interfaces\ScopeInterface;
use Matchory\Elasticsearch\Query;

use function array_keys;
use function array_unshift;
use function array_values;
Expand Down Expand Up @@ -41,14 +40,14 @@ trait AppliesScopes
*/
public function applyScopes(): static
{
if ( ! $this->scopes) {
if (!$this->scopes) {
return $this;
}

$query = clone $this;

foreach ($this->scopes as $identifier => $scope) {
if ( ! isset($query->scopes[$identifier])) {
if (!isset($query->scopes[$identifier])) {
continue;
}

Expand Down Expand Up @@ -130,7 +129,7 @@ public function scopes(array|string $scopes): static
/**
* Register a new global scope.
*
* @param string $identifier
* @param string $identifier
* @param Closure|ScopeInterface $scope
*
* @return $this
Expand All @@ -157,7 +156,7 @@ public function withGlobalScope(
*/
public function withoutGlobalScope(ScopeInterface|string $scope): static
{
if ( ! is_string($scope)) {
if (!is_string($scope)) {
$scope = get_class($scope);
}

Expand All @@ -177,7 +176,7 @@ public function withoutGlobalScope(ScopeInterface|string $scope): static
*/
public function withoutGlobalScopes(array $scopes = null): static
{
if ( ! is_array($scopes)) {
if (!is_array($scopes)) {
$scopes = array_keys($this->scopes);
}

Expand All @@ -192,35 +191,34 @@ public function withoutGlobalScopes(array $scopes = null): static
* Apply the given named scope on the current query instance.
*
* @param string $scope
* @param array $parameters
* @param array $parameters
*
* @return $this
*/
protected function callNamedScope(
string $scope,
array $parameters = []
): static {
return $this->callScope(function (...$parameters) use ($scope) {
return $this->getModel()->callNamedScope(
return $this->callScope(fn(mixed ...$parameters): mixed => $this
->getModel()
->callNamedScope(
$scope,
$parameters
);
}, $parameters);
), $parameters);
}

/**
* Apply the given scope on the current builder instance.
*
* @param callable $scope
* @param array $parameters
* @param array $parameters
*
* @return $this
*/
protected function callScope(callable $scope, array $parameters = []): static
{
array_unshift($parameters, $this);

$scope(...array_values($parameters)) ?? $this;
$scope(...array_values($parameters));

return $this;
}
Expand Down
Loading

0 comments on commit 6b505ac

Please sign in to comment.