Skip to content

Commit

Permalink
Merge branch 'main' into fix/sqid-attribute-return-type
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred authored May 18, 2024
2 parents 5310a20 + d68d86e commit 32f5492
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 34 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ parameters:
ignoreErrors:

excludePaths:

checkMissingIterableValueType: false
1 change: 0 additions & 1 deletion src/Concerns/HasSqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function getRouteKeyName(): string
* @param Model|Relation $query
* @param mixed $value
* @param null $field
* @return Builder|Relation
*/
public function resolveRouteBindingQuery($query, $value, $field = null): Builder|Relation
{
Expand Down
1 change: 0 additions & 1 deletion src/Contracts/Prefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ interface Prefix
{
/**
* @param class-string<Model> $model
* @return string
*/
public function prefix(string $model): string;
}
2 changes: 1 addition & 1 deletion src/Mixins/FindBySqidMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class FindBySqidMixin
public function findBySqid(): Closure
{
/** @phpstan-ignore-next-line */
return fn(string $sqid, array $columns = ['*']) => $this->find(id: $this->getModel()->keyFromSqid(sqid: $sqid), columns: $columns);
return fn (string $sqid, array $columns = ['*']) => $this->find(id: $this->getModel()->keyFromSqid(sqid: $sqid), columns: $columns);
}
}
2 changes: 1 addition & 1 deletion src/Mixins/FindBySqidOrFailMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class FindBySqidOrFailMixin
public function findBySqidOrFail(): Closure
{
/** @phpstan-ignore-next-line */
return fn(string $sqid, array $columns = ['*']) => $this->findOrFail(id: $this->getModel()->keyFromSqid(sqid: $sqid), columns: $columns);
return fn (string $sqid, array $columns = ['*']) => $this->findOrFail(id: $this->getModel()->keyFromSqid(sqid: $sqid), columns: $columns);
}
}
2 changes: 1 addition & 1 deletion src/Mixins/WhereSqidInMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function whereSqidIn(): Closure
$model = $this->getModel();

/** @phpstan-ignore-next-line */
$values = array_map(callback: fn(string $sqid) => $this->getModel()->keyFromSqid(sqid: $sqid), array: $sqids);
$values = array_map(callback: fn (string $sqid) => $this->getModel()->keyFromSqid(sqid: $sqid), array: $sqids);

return $this->whereIn(column: $column, values: $values, boolean: $boolean, not: $not);
};
Expand Down
2 changes: 1 addition & 1 deletion src/Mixins/WhereSqidMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class WhereSqidMixin
public function whereSqid(): Closure
{
/** @phpstan-ignore-next-line */
return fn(string $sqid) => $this->whereKey(id: $this->getModel()->keyFromSqid(sqid: $sqid));
return fn (string $sqid) => $this->whereKey(id: $this->getModel()->keyFromSqid(sqid: $sqid));
}
}
17 changes: 10 additions & 7 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function find(string $sqid): ?EloquentModel
/** @var class-string<EloquentModel>|null $model */
$model = $models[$prefix] ?? null;

if (!$model) {
if (! $model) {
return null;
}

Expand All @@ -45,7 +45,7 @@ public static function findOrFail(string $sqid): EloquentModel
/** @var class-string<EloquentModel>|null $model */
$model = $models[$prefix] ?? null;

if (!$model) {
if (! $model) {
throw new ModelNotFoundException();
}

Expand All @@ -60,7 +60,7 @@ protected static function models(): array
{
/** @var array<string, class-string<EloquentModel>> $models */
$models = collect(static::getFilesRecursively())
->map(fn(SplFileInfo $file) => self::fullQualifiedClassNameFromFile(file: $file))
->map(fn (SplFileInfo $file) => self::fullQualifiedClassNameFromFile(file: $file))
->map(function (string $class): ?ReflectionClass {
try {
/** @phpstan-ignore-next-line */
Expand All @@ -71,11 +71,11 @@ protected static function models(): array
})
->filter()
/** @phpstan-ignore-next-line */
->filter(fn(ReflectionClass $class): bool => $class->isSubclassOf(class: EloquentModel::class))
->filter(fn (ReflectionClass $class): bool => $class->isSubclassOf(class: EloquentModel::class))
/** @phpstan-ignore-next-line */
->filter(fn(ReflectionClass $class) => !$class->isAbstract())
->filter(fn (ReflectionClass $class) => ! $class->isAbstract())
/** @phpstan-ignore-next-line */
->filter(fn(ReflectionClass $class) => in_array(needle: HasSqids::class, haystack: $class->getTraitNames()))
->filter(fn (ReflectionClass $class) => in_array(needle: HasSqids::class, haystack: $class->getTraitNames()))
/** @phpstan-ignore-next-line */
->mapWithKeys(function (ReflectionClass $reflectionClass): array {
/** @var class-string<EloquentModel> $model */
Expand All @@ -90,11 +90,14 @@ protected static function models(): array
return $models;
}

/**
* @return array<string, string>
*/
protected static function namespaces(): array
{
$composer = File::json(path: base_path(path: 'composer.json'));

/** @var array $namespaces */
/** @var array<string, string> $namespaces */
$namespaces = Arr::get(array: $composer, key: 'autoload.psr-4', default: []);

return array_flip($namespaces);
Expand Down
1 change: 0 additions & 1 deletion src/Prefixes/ConsonantPrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class ConsonantPrefix implements Prefix
* Use the first 3 consonants as the model prefix.
*
* @param class-string<Model> $model
* @return string
*/
public function prefix(string $model): string
{
Expand Down
1 change: 0 additions & 1 deletion src/Prefixes/SimplePrefix.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class SimplePrefix implements Prefix
* Use the first 3 characters as the model prefix.
*
* @param class-string<Model> $model
* @return string
*/
public function prefix(string $model): string
{
Expand Down
11 changes: 8 additions & 3 deletions src/Sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static function forModel(Model $model): string

/**
* @param class-string<Model> $model
* @return string
*/
public static function prefixForModel(string $model): ?string
{
Expand All @@ -38,7 +37,7 @@ public static function prefixForModel(string $model): ?string
return $modelPrefix;
}

if (!$prefixClass) {
if (! $prefixClass) {
return null;
}

Expand All @@ -50,6 +49,9 @@ public static function encodeId(string $model, int $id): string
return static::encoder(model: $model)->encode(numbers: [$id]);
}

/**
* @return array<int, int>
*/
public static function decodeId(string $model, string $id): array
{
return static::encoder(model: $model)->decode(id: $id);
Expand All @@ -72,7 +74,7 @@ public static function alphabetForModel(string $model): string
$shuffle = $model . Config::shuffleKey();
$shuffleLength = mb_strlen(string: $shuffle);

if (!$shuffleLength) {
if (! $shuffleLength) {
return Config::alphabet();
}

Expand All @@ -92,6 +94,9 @@ public static function alphabetForModel(string $model): string
return implode(separator: '', array: $alphabetArray);
}

/**
* @return array<int, string>
*/
protected static function multiByteSplit(string $string): array
{
return preg_split(pattern: '/(?!^)(?=.)/u', subject: $string) ?: [];
Expand Down
2 changes: 1 addition & 1 deletion src/SqidsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Illuminate\Support\ServiceProvider;
use RedExplosion\Sqids\Mixins\FindBySqidMixin;
use RedExplosion\Sqids\Mixins\FindBySqidOrFailMixin;
use RedExplosion\Sqids\Mixins\WhereSqidMixin;
use RedExplosion\Sqids\Mixins\WhereSqidInMixin;
use RedExplosion\Sqids\Mixins\WhereSqidMixin;
use RedExplosion\Sqids\Mixins\WhereSqidNotInMixin;

class SqidsServiceProvider extends ServiceProvider
Expand Down
20 changes: 13 additions & 7 deletions src/Support/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Config

protected static int $defaultMinLength = 10;

/**
* @var array<int, string>
*/
protected static array $defaultBlacklist = [];

protected static string $defaultSeparator = '_';
Expand All @@ -26,7 +29,7 @@ public static function shuffleKey(): ?string
{
$shuffleKey = config(key: 'sqids.shuffle_key');

if (!is_string($shuffleKey)) {
if (! is_string($shuffleKey)) {
return null;
}

Expand All @@ -37,7 +40,7 @@ public static function alphabet(): string
{
$alphabet = config(key: 'sqids.alphabet');

if (!$alphabet || !is_string($alphabet)) {
if (! $alphabet || ! is_string($alphabet)) {
return static::$defaultAlphabet;
}

Expand All @@ -49,18 +52,21 @@ public static function minLength(): int
/** @var int|null $minLength */
$minLength = config(key: 'sqids.min_length', default: static::$defaultMinLength);

if (!$minLength || !is_int($minLength)) {
if (! $minLength || ! is_int($minLength)) {
return static::$defaultMinLength;
}

return $minLength;
}

/**
* @return array<int, string>
*/
public static function blacklist(): array
{
$blacklist = config(key: 'sqids.blacklist', default: static::$defaultBlacklist);

if (!is_array($blacklist)) {
if (! is_array($blacklist)) {
return static::$defaultBlacklist;
}

Expand All @@ -71,7 +77,7 @@ public static function separator(): string
{
$separator = config(key: 'sqids.separator', default: static::$defaultSeparator);

if (!$separator || !is_string(value: $separator)) {
if (! $separator || ! is_string(value: $separator)) {
return static::$defaultSeparator;
}

Expand All @@ -82,7 +88,7 @@ public static function prefixClass(): ?Prefix
{
$prefix = config(key: 'sqids.prefix_class');

if (!$prefix) {
if (! $prefix) {
return null;
}

Expand All @@ -92,7 +98,7 @@ public static function prefixClass(): ?Prefix
return new SimplePrefix();
}

if (!$prefix instanceof Prefix) {
if (! $prefix instanceof Prefix) {
return new SimplePrefix();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
->toBe(expected: 'cst')
->prefix(model: Charge::class)
->toBe(expected: 'chr');
;
});
1 change: 0 additions & 1 deletion tests/Prefixes/DefaultPrefixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
->toBe(expected: 'cus')
->prefix(model: Charge::class)
->toBe(expected: 'cha');
;
});
2 changes: 1 addition & 1 deletion tests/RouteModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

it('returns a 404 if the sqid is invalid', function (): void {
$this
->get(uri: "/customers/invalid-sqid")
->get(uri: '/customers/invalid-sqid')
->assertNotFound();
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
return new class() extends Migration
{
/**
* Run the migrations.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
return new class() extends Migration
{
/**
* Run the migrations.
*/
Expand Down
2 changes: 1 addition & 1 deletion workbench/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
use Illuminate\Support\Facades\Route;
use Workbench\App\Models\Customer;

Route::get(uri: 'customers/{customer}', action: fn(Customer $customer) => $customer->name);
Route::get(uri: 'customers/{customer}', action: fn (Customer $customer) => $customer->name);

0 comments on commit 32f5492

Please sign in to comment.