Skip to content

Commit

Permalink
[Enhancement] Fix code style (#1837)
Browse files Browse the repository at this point in the history
* fix code style
  • Loading branch information
Tiagospem authored Feb 6, 2025
1 parent 7e361e3 commit 7e6682a
Show file tree
Hide file tree
Showing 58 changed files with 314 additions and 187 deletions.
5 changes: 3 additions & 2 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PowerComponents\LivewirePowerGrid;

use Closure;
use Illuminate\Support\Traits\Macroable;
use Livewire\Wireable;

Expand All @@ -17,7 +18,7 @@
* @method static route(string $route, array $params, string $target)
* @method static method(string $method)
* @method static target(string $target) _blank, _self, _top, _parent, null
* @method static can(bool|\Closure $allowed = true)
* @method static can(bool|Closure $allowed = true)
* @method static id(string $id = null)
* @method static confirm(string $message = 'Are you sure you want to perform this action?')
* @method static confirmPrompt(string $message = 'Are you sure you want to perform this action?', string $confirmValue = 'Confirm')
Expand All @@ -38,7 +39,7 @@ final class Button implements Wireable

public array $iconAttributes = [];

public bool | \Closure $can = true;
public bool|Closure $can = true;

public function __construct(public string $action)
{
Expand Down
7 changes: 4 additions & 3 deletions src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace PowerComponents\LivewirePowerGrid;

use Illuminate\Support\Traits\Macroable;
use Livewire\Wireable;

/**
* Macros
* @method static naturalSort()
* @method static searchableRaw(string $sql)
* @method static searchableJson(string $tableName) // sqlite, mysql
*/
final class Column implements \Livewire\Wireable
final class Column implements Wireable
{
use Macroable;

Expand Down Expand Up @@ -81,7 +82,7 @@ public static function add(): self
*/
public static function make(string $title, string $field, string $dataField = ''): self
{
return (new static())
return (new Column())
->title($title)
->field($field, $dataField);
}
Expand All @@ -91,7 +92,7 @@ public static function make(string $title, string $field, string $dataField = ''
*/
public static function action(string $title): self
{
return (new static())
return (new Column())
->title($title)
->isAction()
->visibleInExport(false);
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Actions/AskComponentDatasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public static function handle(): string
{
// Must pass options as array<int, "label"> to
// improve users experience when Laravel prompt falls back.
$datasources = Datasource::asOptions();
$datasource = Datasource::asOptions();

$choice = strval(select(
label: 'Select your preferred Data source:',
options: $datasources->values()->toArray(), // @phpstan-ignore-line
options: $datasource->values()->toArray(), // @phpstan-ignore-line
default: 0
));

// Find and return they key based on user's choice.
return (string) $datasources->filter(function ($item) use ($choice) {
return (string) $datasource->filter(function ($item) use ($choice) {
return $item === $choice;
})->keys()[0];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Actions/AskComponentName.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static function handle(): string
private static function checkIfComponentAlreadyExists(): void
{
if (File::exists(powergrid_components_path(self::$componentName . '.php'))) {
$confirmation = (bool) confirm(
"Component [" . self::$componentName . "] already exists. Overwrite it?",
$confirmation = confirm(
'Component [' . self::$componentName . '] already exists. Overwrite it?',
default: false,
hint: '❗ WARNING ❗'
);
Expand Down
1 change: 1 addition & 0 deletions src/Commands/Actions/AskDatabaseTableName.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class AskDatabaseTableName
public static function handle(): string
{
$tableExists = false;
$tableName = '';

while (!$tableExists) {
$tableName = suggest(
Expand Down
18 changes: 8 additions & 10 deletions src/Commands/Actions/AskModelName.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ final class AskModelName
*/
public static function handle(): array
{
{
while (self::$model === '') {
self::setModel(suggest(
label: 'Select a Model or enter its Fully qualified name.',
options: ListModels::handle(),
required: true,
));
}

return ['model' => self::$model, 'fqn' => self::$fqn];
while (self::$model === '') {
self::setModel(suggest(
label: 'Select a Model or enter its Fully qualified name.',
options: ListModels::handle(),
required: true,
));
}

return ['model' => self::$model, 'fqn' => self::$fqn];
}

private static function setModel(string $model): void
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/Actions/CheckIfDatabaseHasTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace PowerComponents\LivewirePowerGrid\Commands\Actions;

use Exception;
use Illuminate\Support\Facades\Schema;

final class CheckIfDatabaseHasTables
{
public static function handle(): bool
{
try {
return count(Schema::getTables()) > 0 ? true : false;
} catch (\Exception $e) {
return count(Schema::getTables()) > 0;
} catch (Exception) {
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Actions/GetStubVarsFromDbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public static function handle(PowerGridComponentMaker $component): array
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '\')' . "\n" . ' ->sortable()' . "\n" . ' ->searchable(),' . "\n\n";
}

$columns .= " ];";
$filters .= " ];";
$columns .= ' ];';
$filters .= ' ];';

return [
'PowerGridFields' => $datasource,
Expand Down
7 changes: 4 additions & 3 deletions src/Commands/Actions/GetStubVarsFromFromModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PowerComponents\LivewirePowerGrid\Commands\Actions;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use PowerComponents\LivewirePowerGrid\Commands\Support\PowerGridComponentMaker;
Expand All @@ -15,7 +16,7 @@ class GetStubVarsFromFromModel
*/
public static function handle(PowerGridComponentMaker $component): array
{
/** @var \Illuminate\Database\Eloquent\Model $model*/
/** @var Model $model */
$model = new $component->modelFqn();

$getFillable = $model->getFillable();
Expand Down Expand Up @@ -106,8 +107,8 @@ public static function handle(PowerGridComponentMaker $component): array

$columns .= ' Column::action(\'Action\')' . "\n";

$columns .= " ];";
$filters .= " ];";
$columns .= ' ];';
$filters .= ' ];';

return [
'PowerGridFields' => $datasource,
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/Actions/ListDatabaseTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PowerComponents\LivewirePowerGrid\Commands\Actions;

use Exception;
use Illuminate\Support\Facades\Schema;

final class ListDatabaseTables
Expand All @@ -18,8 +19,8 @@ public static function handle(): array
return array_values(collect(Schema::getTables())
->pluck('name')
->diff(self::HIDDEN_TABLES)
->toArray());
} catch (\Exception $e) {
->all());
} catch (Exception) {
return [];
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/Commands/Actions/ListModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
namespace PowerComponents\LivewirePowerGrid\Commands\Actions;

use Illuminate\Support\Facades\File;
use ReflectionClass;
use ReflectionException;
use Symfony\Component\Finder\SplFileInfo;

final class ListModels
{
/**
* List files in Models
*
*/
public static function handle(): array
{
$directories = config('livewire-powergrid.auto_discover_models_paths', [app_path('Models')]);

/** @var Array<int,string> $directories */
/** @var array<int,string> $directories */
return collect($directories)
->filter(fn (string $directory) => File::exists($directory))
->map(fn (string $directory) => File::allFiles($directory))
Expand All @@ -33,8 +34,10 @@ public static function handle(): array
->filter()

// Remove classes that do not extend an Eloquent Model
/** @phpstan-ignore-next-line */
->reject(fn (string $fqnClass) => rescue(fn () => (new \ReflectionClass($fqnClass))->isSubclassOf(\Illuminate\Database\Eloquent\Model::class), false) === false)
->toArray();
/** @phpstan-ignore-next-line
* @throws ReflectionException
*/
->reject(fn (string $fqnClass) => rescue(fn () => (new ReflectionClass($fqnClass))->isSubclassOf(\Illuminate\Database\Eloquent\Model::class), false) === false)
->all();
}
}
6 changes: 4 additions & 2 deletions src/Commands/Actions/ParseFqnClassInCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace PowerComponents\LivewirePowerGrid\Commands\Actions;

use Exception;

final class ParseFqnClassInCode
{
/**
* Parse namespace from PHP source code
* Inspired by: https://gist.github.com/ludofleury/1886076
* @throws \Exception
* @throws Exception
*/
public static function handle(string $sourceCode): string
{
if (preg_match('#^namespace\s+(.+?);.*class\s+(\w+).+;$#sm', $sourceCode, $matches)) {
return $matches[1] . '\\' . $matches[2];
}

throw new \Exception('could not find a FQN Class is source-code');
throw new Exception('could not find a FQN Class is source-code');
}
}
6 changes: 3 additions & 3 deletions src/Commands/Actions/SanitizeComponentName.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public static function handle(string $componentName): string
//Convert multiple spaces into forward slashes
->replaceMatches('/\s+/', '//')
//multiple back slashes into forward slashes
->replaceMatches('/\\\{2,}/', "\\")
->replaceMatches('/\\\{2,}/', '\\')
//Multiple forward slashes
->replaceMatches('/\/{2,}/', "\\")
->replaceMatches('/\/{2,}/', '\\')
//Multile dots
->replaceMatches('/\.{2,}/', ".")
->replaceMatches('/\.{2,}/', '.')
->replace('.', '\\')
//Left over backslahes into forward slashes
->replace('/', '\\')
Expand Down
21 changes: 11 additions & 10 deletions src/Commands/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PowerComponents\LivewirePowerGrid\Commands;

use Exception;
use Illuminate\Console\Command;

use function Laravel\Prompts\{error, info, note};
Expand Down Expand Up @@ -34,17 +35,17 @@ public function handle(): int

try {
$this
->step1()
->step2()
->step3()
->step4()
->step5()
->step6()
->save()
->feedback();
->step1()
->step2()
->step3()
->step4()
->step5()
->step6()
->save()
->feedback();

return self::SUCCESS;
} catch (\Exception $e) {
} catch (Exception $e) {
error($e->getMessage());

return self::FAILURE;
Expand Down Expand Up @@ -130,7 +131,7 @@ public function feedback(): void

note("💡 include the <comment>{$this->component?->name}</comment> component using the tag: <comment>{$this->component?->htmlTag}</comment>");

info("👍 Please consider <comment>⭐ starring ⭐</comment> <info>our repository. Visit: </info><comment>https://github.com/Power-Components/livewire-powergrid</comment>" . PHP_EOL);
info('👍 Please consider <comment>⭐ starring ⭐</comment> <info>our repository. Visit: </info><comment>https://github.com/Power-Components/livewire-powergrid</comment>' . PHP_EOL);
}

private function AutoImportLabel(): string
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Enums/Datasource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public static function from(string $datasource): mixed
public function label(): string
{
return match ($this) {
Datasource::ELOQUENT_BUILDER => "Eloquent Builder",
Datasource::QUERY_BUILDER => "Query Builder",
Datasource::COLLECTION => "Collection"
Datasource::ELOQUENT_BUILDER => 'Eloquent Builder',
Datasource::QUERY_BUILDER => 'Query Builder',
Datasource::COLLECTION => 'Collection'
};
}

Expand Down Expand Up @@ -70,7 +70,7 @@ public function stubTemplate(): string
}

/**
* Datasource with labels for dropdown select
* Datasource with labels for dropdown select
*
* @return Collection<string,string>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/InteractsWithVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class InteractsWithVersions
*/
public function ensureLatestVersion(): array
{
$composer = Factory::create(new NullIo(), null, false);
$composer = Factory::create(new NullIo());
$localRepo = $composer->getRepositoryManager()->getLocalRepository();

return $this->searchPackage($localRepo);
Expand Down Expand Up @@ -73,7 +73,7 @@ public function getLatestVersion(): string

/** @phpstan-ignore-next-line */
$version = collect($package['packages']['power-components/livewire-powergrid'])
->first()['version'];
->first()['version'];

if (!is_string($version)) {
throw new Exception('Error: could find PowerGrid version.');
Expand Down
Loading

0 comments on commit 7e6682a

Please sign in to comment.