Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Fix code style #1837

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
86a9b17
rename datasources to datasource
Tiagospem Feb 2, 2025
41da80d
remove unnecessary cast confirm method already returns a boolean
Tiagospem Feb 2, 2025
06ee40d
declare $table name
Tiagospem Feb 2, 2025
7ad86fa
remove redundant static and fix code style
Tiagospem Feb 2, 2025
3ee03ed
remove duplicated braces
Tiagospem Feb 2, 2025
403c819
add missing import
Tiagospem Feb 2, 2025
75024f8
add missing import
Tiagospem Feb 2, 2025
6519325
add missing import
Tiagospem Feb 2, 2025
dbc7e14
add missing import
Tiagospem Feb 2, 2025
e9f0bab
add missing import
Tiagospem Feb 2, 2025
8d5a3ee
add missing import
Tiagospem Feb 2, 2025
2dcd49c
add missing import, fix code style and add @throws
Tiagospem Feb 2, 2025
8641985
rename DataSource as DataSourceCollection and add missing throws
Tiagospem Feb 2, 2025
54bdfb4
add missing import, fix code style
Tiagospem Feb 2, 2025
67c5706
fix code style
Tiagospem Feb 2, 2025
45bcf20
fix code style and add missing import
Tiagospem Feb 2, 2025
2dbf872
add missing import
Tiagospem Feb 2, 2025
458c68e
add missing import
Tiagospem Feb 2, 2025
014a8e6
add missing import and throws
Tiagospem Feb 2, 2025
d4b3844
add missing throws
Tiagospem Feb 2, 2025
88399fa
remove [] from collect and replace toArray() from all()
Tiagospem Feb 2, 2025
dc7b9b8
replace !empty by filled and added missing throws
Tiagospem Feb 2, 2025
e4612b7
added missing imports
Tiagospem Feb 2, 2025
71bf110
added missing imports
Tiagospem Feb 2, 2025
da8b080
fix code style
Tiagospem Feb 2, 2025
1144532
fix code style
Tiagospem Feb 2, 2025
967299f
fix code style and added missing imports
Tiagospem Feb 2, 2025
bc61c27
fix code style and replace toArray() by all()
Tiagospem Feb 2, 2025
5a1f1ae
fix code style and added missing imports
Tiagospem Feb 2, 2025
4fbca74
fix code style and remove unnecessary parameters
Tiagospem Feb 2, 2025
fcc6f6a
added missing imports
Tiagospem Feb 2, 2025
0f82db5
added missing imports and replace toArray() by all()
Tiagospem Feb 2, 2025
93560cc
added missing imports and throws
Tiagospem Feb 2, 2025
f323619
added missing imports and throws
Tiagospem Feb 2, 2025
7e09582
added missing import
Tiagospem Feb 2, 2025
8611eb6
added missing imports
Tiagospem Feb 2, 2025
4636aeb
added missing imports
Tiagospem Feb 2, 2025
0b7d99d
added missing imports
Tiagospem Feb 2, 2025
17f6168
added missing import
Tiagospem Feb 2, 2025
091e6dc
added missing imports
Tiagospem Feb 2, 2025
2689a22
added missing imports and replace toArray() by all()
Tiagospem Feb 2, 2025
1b9db23
replace boolval by filled
Tiagospem Feb 2, 2025
1ec1eba
added missing throws and fix ode style
Tiagospem Feb 2, 2025
d7dbd61
fix code style and added missing import
Tiagospem Feb 2, 2025
dd830cb
fix code style and added missing import
Tiagospem Feb 2, 2025
00966cb
fix code style
Tiagospem Feb 2, 2025
b7a9a3e
fix code style and added missing import
Tiagospem Feb 2, 2025
2404902
fix code style
Tiagospem Feb 2, 2025
44e37a2
added missing import
Tiagospem Feb 2, 2025
bc227b6
added missing throws
Tiagospem Feb 2, 2025
f4ad1aa
added missing import and throws
Tiagospem Feb 2, 2025
2052b94
added missing import
Tiagospem Feb 2, 2025
9478822
added missing import and throws
Tiagospem Feb 2, 2025
e94e898
fix code style
Tiagospem Feb 2, 2025
fff4d24
replace by filled
Tiagospem Feb 2, 2025
0595387
added missing import
Tiagospem Feb 2, 2025
af2c53e
added missing import
Tiagospem Feb 2, 2025
29697b2
fix code style
Tiagospem Feb 2, 2025
51ef242
added missing import
Tiagospem Feb 2, 2025
606c794
added missing import
Tiagospem Feb 2, 2025
b515555
fix code style
Tiagospem Feb 2, 2025
2a2b9a4
rollback changes
Tiagospem Feb 2, 2025
99bfd03
use only blank
Tiagospem Feb 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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