From da72b72658c7ae04ea59bbae9f27cf6c29aa38ba Mon Sep 17 00:00:00 2001 From: Abdullah Al-Faqeir Date: Sun, 10 Oct 2021 13:39:25 +0300 Subject: [PATCH 1/3] - Code refactor. - Fixed default body column value. - Adopt to php8. --- config/model-stats.php | 21 ++++++-- database/factories/DashboardFactory.php | 20 +++++++ database/factories/ModelFactory.php | 19 ------- .../migrations/create_model-stats_table.php | 19 ++++--- src/AuthorizesRequests.php | 20 +++---- src/Console/InstallModelStatsPackage.php | 4 +- src/Console/PublishCommand.php | 3 +- src/Http/Controllers/Controller.php | 6 +-- src/Http/Controllers/CustomCodeController.php | 52 ++++++++----------- src/Http/Controllers/DashboardController.php | 16 +++--- src/Http/Controllers/HomeController.php | 29 ++++++----- src/Http/Controllers/StatController.php | 24 +++------ src/Http/Middleware/Authorize.php | 11 ++-- src/Http/Middleware/CustomCodeEnabled.php | 7 +-- src/Http/Requests/Dashboard/StoreRequest.php | 5 +- src/Http/Requests/Dashboard/UpdateRequest.php | 9 ++-- src/Http/Requests/Widgets/DataRequest.php | 43 +++++++-------- src/LaravelModelStatsFacade.php | 2 +- src/LaravelModelStatsServiceProvider.php | 13 ++--- src/ModelStatsServiceProvider.php | 11 ++-- src/Models/Dashboard.php | 22 ++++++-- src/Services/ModelStats.php | 8 +-- src/Services/Tinker.php | 28 +++++----- stubs/ModelStatsServiceProvider.stub | 2 +- 24 files changed, 208 insertions(+), 186 deletions(-) create mode 100644 database/factories/DashboardFactory.php delete mode 100644 database/factories/ModelFactory.php diff --git a/config/model-stats.php b/config/model-stats.php index 046c437..3b7a912 100644 --- a/config/model-stats.php +++ b/config/model-stats.php @@ -16,8 +16,8 @@ | */ - 'enabled' => env('MODEL_STATS_ENABLED', true), - 'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', true), + 'enabled' => env('MODEL_STATS_ENABLED', true), + 'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', true), /* |-------------------------------------------------------------------------- @@ -29,20 +29,31 @@ | the existing middleware. Or, you can simply stick with this list. | */ - 'middleware' => [ + 'middleware' => [ 'web', \Jhumanj\LaravelModelStats\Http\Middleware\Authorize::class, ], + /* + |-------------------------------------------------------------------------- + | ModelStats table name + |-------------------------------------------------------------------------- + | + | As PostgreSQL table names seems to use dashes instead of underscore + | this configures the table name based on your connection. + | + */ + 'table_name' => 'model_stats_dashboards', + /* |-------------------------------------------------------------------------- | Route Prefixes |-------------------------------------------------------------------------- | - | You can change the route where your dashboards are. By default routes will + | You can change the route where your dashboards are. By default, routes will | be starting the '/stats' prefix, and names will start with 'stats.'. | */ - 'routes_prefix' => 'stats', + 'routes_prefix' => 'stats', 'route_names_prefix' => 'stats.', ]; diff --git a/database/factories/DashboardFactory.php b/database/factories/DashboardFactory.php new file mode 100644 index 0000000..bbfb803 --- /dev/null +++ b/database/factories/DashboardFactory.php @@ -0,0 +1,20 @@ + $this->faker->name, + 'description' => $this->faker->sentence, + 'body' => '{"widgets":[]}', + ]; + } +} diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100644 index 0f35887..0000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1,19 +0,0 @@ -id(); $table->string('name'); $table->text('description'); - $table->jsonb('body')->default('{"widgets":[]}'); + if (Config::get('database.defaults') === 'pgsql') { + $table->jsonb('body')->default('{"widgets":[]}'); + } else { + $table->json('body'); + } $table->timestamps(); }); } - public function down() + public function down(): void { - Schema::dropIfExists('model-stats-dashboards'); + Schema::dropIfExists(Config::get('model-stats.table_name')); } }; diff --git a/src/AuthorizesRequests.php b/src/AuthorizesRequests.php index 6818c3e..1d3a2a1 100644 --- a/src/AuthorizesRequests.php +++ b/src/AuthorizesRequests.php @@ -1,8 +1,10 @@ environment('local'); - })($request); + return (static::$authUsing ?: fn () => app()->environment('local'))($request); } } diff --git a/src/Console/InstallModelStatsPackage.php b/src/Console/InstallModelStatsPackage.php index ba4106e..e0c8633 100644 --- a/src/Console/InstallModelStatsPackage.php +++ b/src/Console/InstallModelStatsPackage.php @@ -12,7 +12,7 @@ class InstallModelStatsPackage extends Command protected $description = 'Install the ModelStatsPackage'; - public function handle() + public function handle(): void { $this->info('Installing ModelStats package...'); @@ -35,7 +35,7 @@ public function handle() * * @return void */ - private function registerModelStatsServiceProvider() + private function registerModelStatsServiceProvider(): void { $namespace = Str::replaceLast('\\', '', $this->laravel->getNamespace()); diff --git a/src/Console/PublishCommand.php b/src/Console/PublishCommand.php index fe9bdfa..fd1693e 100644 --- a/src/Console/PublishCommand.php +++ b/src/Console/PublishCommand.php @@ -1,6 +1,5 @@ call('vendor:publish', [ '--tag' => 'model-stats-config', diff --git a/src/Http/Controllers/Controller.php b/src/Http/Controllers/Controller.php index 9d25bc7..af95461 100644 --- a/src/Http/Controllers/Controller.php +++ b/src/Http/Controllers/Controller.php @@ -1,8 +1,8 @@ json(array_merge([ 'type' => 'success', ], $data)); } - public function error($data = [], $statusCode = 400) + public function error($data = [], $statusCode = 400): JsonResponse { return response()->json(array_merge([ 'type' => 'error', diff --git a/src/Http/Controllers/CustomCodeController.php b/src/Http/Controllers/CustomCodeController.php index 43001d8..7b73002 100644 --- a/src/Http/Controllers/CustomCodeController.php +++ b/src/Http/Controllers/CustomCodeController.php @@ -5,12 +5,13 @@ use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Validation\Rule; +use Illuminate\Http\JsonResponse; use Jhumanj\LaravelModelStats\Http\Middleware\CustomCodeEnabled; use Jhumanj\LaravelModelStats\Services\Tinker; class CustomCodeController extends Controller { - const CHART_TYPES = ['line_chart', 'bar_chart']; + public const CHART_TYPES = ['line_chart', 'bar_chart']; public function __construct() { @@ -20,7 +21,7 @@ public function __construct() /** * Endpoint used to test customCode when creating widgets. */ - public function executeCustomCode(Request $request, Tinker $tinker) + public function executeCustomCode(Request $request, Tinker $tinker): JsonResponse { $validated = $request->validate([ 'code' => 'required', @@ -35,10 +36,8 @@ public function executeCustomCode(Request $request, Tinker $tinker) return $this->success([ 'output' => $result, 'code_executed' => $codeExecuted, - 'valid_output' => $codeExecuted ? $this->isValidOutput( - $request->chart_type, - $tinker->getCustomCodeResult() - ) : false, + 'valid_output' => $codeExecuted + && $this->isValidOutput($request->get('chart_type'), $tinker->getCustomCodeResult()), ]); } @@ -51,8 +50,8 @@ public function widgetData(Request $request, Tinker $tinker) 'date_to' => 'required|date_format:Y-m-d|after:date_from', ]); - $dateFrom = Carbon::createFromFormat('Y-m-d', $request->date_from); - $dateTo = Carbon::createFromFormat('Y-m-d', $request->date_to); + $dateFrom = Carbon::createFromFormat('Y-m-d', $request->get('date_from')); + $dateTo = Carbon::createFromFormat('Y-m-d', $request->get('date_to')); $result = $tinker->injectDates($dateFrom, $dateTo) ->readonly() @@ -61,33 +60,28 @@ public function widgetData(Request $request, Tinker $tinker) $codeExecuted = $tinker->lastExecSuccess(); $dataResult = $tinker->getCustomCodeResult(); - if ($codeExecuted && $this->isValidOutput($request->chart_type, $dataResult)) { + if ($codeExecuted && $this->isValidOutput($request->get('chart_type'), $dataResult)) { return $tinker->getCustomCodeResult(); - } else { - return $this->error([ - 'output' => $result, - 'code_executed' => $codeExecuted, - 'valid_output' => $codeExecuted ? $this->isValidOutput( - $request->chart_type, - $tinker->getCustomCodeResult() - ) : false, - ]); } + + return $this->error([ + 'output' => $result, + 'code_executed' => $codeExecuted, + 'valid_output' => $codeExecuted + && $this->isValidOutput($request->get('chart_type'), $tinker->getCustomCodeResult()), + ]); } - private function isValidOutput(string $chartType, $data) + private function isValidOutput(string $chartType, $data): bool { - switch ($chartType) { - case 'bar_chart': - return $this->validateBarChartData($data); - case 'line_chart': - return $this->validateLineChartData($data); - } - - return false; + return match ($chartType) { + 'bar_chart' => $this->validateBarChartData($data), + 'line_chart' => $this->validateLineChartData($data), + default => false, + }; } - private function validateBarChartData($data) + private function validateBarChartData($data): bool { if (! is_array($data)) { return false; @@ -101,7 +95,7 @@ private function validateBarChartData($data) return true; } - private function validateLineChartData($data) + private function validateLineChartData($data): bool { if (! is_array($data)) { return false; diff --git a/src/Http/Controllers/DashboardController.php b/src/Http/Controllers/DashboardController.php index dbfb0b4..ff1e6f9 100644 --- a/src/Http/Controllers/DashboardController.php +++ b/src/Http/Controllers/DashboardController.php @@ -2,35 +2,39 @@ namespace Jhumanj\LaravelModelStats\Http\Controllers; +use Illuminate\Http\JsonResponse; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Jhumanj\LaravelModelStats\Http\Requests\Dashboard\StoreRequest; use Jhumanj\LaravelModelStats\Http\Requests\Dashboard\UpdateRequest; use Jhumanj\LaravelModelStats\Models\Dashboard; class DashboardController extends Controller { - public function index() + public function index(): Collection|array { return Dashboard::all(); } - public function show(Dashboard $dashboard) + public function show(Dashboard $dashboard): Dashboard { return $dashboard; } - public function store(StoreRequest $request) + public function store(StoreRequest $request): Model|Builder { - return Dashboard::create($request->validated()); + return Dashboard::query()->create($request->validated()); } - public function update(Dashboard $dashboard, UpdateRequest $request) + public function update(Dashboard $dashboard, UpdateRequest $request): Dashboard { $dashboard->update($request->validated()); return $dashboard; } - public function destroy(Dashboard $dashboard) + public function destroy(Dashboard $dashboard): JsonResponse { $dashboard->delete(); diff --git a/src/Http/Controllers/HomeController.php b/src/Http/Controllers/HomeController.php index ee03af5..3857732 100644 --- a/src/Http/Controllers/HomeController.php +++ b/src/Http/Controllers/HomeController.php @@ -2,16 +2,21 @@ namespace Jhumanj\LaravelModelStats\Http\Controllers; +use Schema; +use ReflectionClass; use Illuminate\Container\Container; +use Illuminate\Contracts\View\View; +use Illuminate\Contracts\View\Factory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; +use Illuminate\Contracts\Foundation\Application; class HomeController extends Controller { - const FRONT_END_VERSION = 6; + public const FRONT_END_VERSION = 6; - public function home() + public function home(): Factory|View|Application { return view('model-stats::dashboard', [ 'config' => $this->modelStatsConfig(), @@ -19,7 +24,7 @@ public function home() ]); } - private function modelStatsConfig() + private function modelStatsConfig(): array { return [ 'appName' => config('app.name'), @@ -33,19 +38,17 @@ private function getModels(): Collection $models = collect(File::allFiles(app_path())) ->map(function ($item) { $path = $item->getRelativePathName(); - $class = sprintf( + return sprintf( '\%s%s', Container::getInstance()->getNamespace(), strtr(substr($path, 0, strrpos($path, '.')), '/', '\\') ); - - return $class; }) ->filter(function ($class) { $valid = false; if (class_exists($class)) { - $reflection = new \ReflectionClass($class); + $reflection = new ReflectionClass($class); $valid = $reflection->isSubclassOf(Model::class) && ! $reflection->isAbstract(); } @@ -54,16 +57,14 @@ private function getModels(): Collection }); - return $models->map(function (string $class) { - return [ - 'class' => $class, - 'fields' => $this->getClassFields($class), - ]; - })->sortByDesc('class')->values(); + return $models->map(fn(string $class) => [ + 'class' => $class, + 'fields' => $this->getClassFields($class), + ])->sortByDesc('class')->values(); } private function getClassFields(string $class) { - return \Schema::getColumnListing((new $class)->getTable()); + return Schema::getColumnListing((new $class)->getTable()); } } diff --git a/src/Http/Controllers/StatController.php b/src/Http/Controllers/StatController.php index c649b2a..f09fe65 100644 --- a/src/Http/Controllers/StatController.php +++ b/src/Http/Controllers/StatController.php @@ -14,22 +14,12 @@ public function widgetData(DataRequest $request) $dateFrom = Carbon::createFromFormat('Y-m-d', $request->date_from); $dateTo = Carbon::createFromFormat('Y-m-d', $request->date_to); - switch ($request->aggregate_type) { - case 'daily_count': - return $modelStats->getDailyHistogram($dateFrom, $dateTo, $request->date_column); - case 'cumulated_daily_count': - return $modelStats->getDailyHistogram($dateFrom, $dateTo, $request->date_column, null, true); - case 'period_total': - return $modelStats->getPeriodTotal($dateFrom, $dateTo, $request->date_column); - case 'group_by_count': - return $modelStats->getGroupByCount( - $dateFrom, - $dateTo, - $request->date_column, - $request->aggregate_column - ); - default: - throw new \Exception('Wigdet aggregate type not supported.'); - } + return match ($request->get('aggregate_type')) { + 'daily_count' => $modelStats->getDailyHistogram($dateFrom, $dateTo, $request->date_column), + 'cumulated_daily_count' => $modelStats->getDailyHistogram($dateFrom, $dateTo, $request->date_column, null, true), + 'period_total' => $modelStats->getPeriodTotal($dateFrom, $dateTo, $request->date_column), + 'group_by_count' => $modelStats->getGroupByCount($dateFrom, $dateTo, $request->date_column, $request->aggregate_column), + default => throw new \Exception('Widget aggregate type not supported.'), + }; } } diff --git a/src/Http/Middleware/Authorize.php b/src/Http/Middleware/Authorize.php index 1fb7d96..d9b70f9 100644 --- a/src/Http/Middleware/Authorize.php +++ b/src/Http/Middleware/Authorize.php @@ -1,6 +1,5 @@ 'Custom code not enabled.', ], 403); diff --git a/src/Http/Requests/Dashboard/StoreRequest.php b/src/Http/Requests/Dashboard/StoreRequest.php index ab2ebba..b2e23fa 100644 --- a/src/Http/Requests/Dashboard/StoreRequest.php +++ b/src/Http/Requests/Dashboard/StoreRequest.php @@ -1,6 +1,5 @@ 'required|string|max:60', + 'name' => 'required|string|max:60', 'description' => 'required', ]; } diff --git a/src/Http/Requests/Dashboard/UpdateRequest.php b/src/Http/Requests/Dashboard/UpdateRequest.php index 3bb45e0..331698f 100644 --- a/src/Http/Requests/Dashboard/UpdateRequest.php +++ b/src/Http/Requests/Dashboard/UpdateRequest.php @@ -1,6 +1,5 @@ 'required|string|max:60', - 'description' => 'required', - 'body' => 'sometimes|required', + 'name' => 'required|string|max:60', + 'description' => 'required', + 'body' => 'sometimes|required', 'body.widgets' => 'sometimes|array', ]; } diff --git a/src/Http/Requests/Widgets/DataRequest.php b/src/Http/Requests/Widgets/DataRequest.php index c895c0c..d9eb983 100644 --- a/src/Http/Requests/Widgets/DataRequest.php +++ b/src/Http/Requests/Widgets/DataRequest.php @@ -1,6 +1,5 @@ ['required',Rule::in($this->getModels())], - 'aggregate_type' => ['required', Rule::in(static::ALLOWED_AGGREGATES_TYPES)], - 'date_column' => 'required', - 'date_from' => 'required|date_format:Y-m-d|before:date_to', - 'date_to' => 'required|date_format:Y-m-d|after:date_from', - 'aggregate_column' => [Rule::requiredIf(in_array(request()->aggregate_type, self::AGGREGATES_TYPES_WITH_AGGREGATE_COLUMN))], + 'model' => ['required', Rule::in($this->getModels())], + 'aggregate_type' => ['required', Rule::in(static::ALLOWED_AGGREGATES_TYPES)], + 'date_column' => 'required', + 'date_from' => 'required|date_format:Y-m-d|before:date_to', + 'date_to' => 'required|date_format:Y-m-d|after:date_from', + 'aggregate_column' => [Rule::requiredIf(in_array($this->aggregate_type, self::AGGREGATES_TYPES_WITH_AGGREGATE_COLUMN, true))], ]; } @@ -45,21 +52,15 @@ private function getModels(): Collection $models = collect(File::allFiles(app_path())) ->map(function ($item) { $path = $item->getRelativePathName(); - $class = sprintf( - '\%s%s', - Container::getInstance()->getNamespace(), - strtr(substr($path, 0, strrpos($path, '.')), '/', '\\') - ); - - return $class; - }) - ->filter(function ($class) { + return sprintf('\%s%s', Container::getInstance() + ->getNamespace(), strtr(substr($path, 0, strrpos($path, '.')), '/', '\\')); + })->filter(function ($class) { $valid = false; if (class_exists($class)) { $reflection = new \ReflectionClass($class); - $valid = $reflection->isSubclassOf(Model::class) && - ! $reflection->isAbstract(); + $valid = $reflection->isSubclassOf(Model::class) + && !$reflection->isAbstract(); } return $valid; diff --git a/src/LaravelModelStatsFacade.php b/src/LaravelModelStatsFacade.php index 02315e3..2eacdd6 100644 --- a/src/LaravelModelStatsFacade.php +++ b/src/LaravelModelStatsFacade.php @@ -9,7 +9,7 @@ */ class LaravelModelStatsFacade extends Facade { - protected static function getFacadeAccessor() + protected static function getFacadeAccessor(): string { return 'laravel-model-stats'; } diff --git a/src/LaravelModelStatsServiceProvider.php b/src/LaravelModelStatsServiceProvider.php index 29f15d2..b86b557 100644 --- a/src/LaravelModelStatsServiceProvider.php +++ b/src/LaravelModelStatsServiceProvider.php @@ -10,11 +10,10 @@ class LaravelModelStatsServiceProvider extends PackageServiceProvider { - public function boot() + public function boot(): LaravelModelStatsServiceProvider { $this->registerPublishing(); $this->registerCommands(); - return parent::boot(); } @@ -25,9 +24,7 @@ public function configurePackage(Package $package): void * * More info: https://github.com/spatie/laravel-package-tools */ - $package - ->name('laravel-model-stats') - ->hasConfigFile(); + $package->name('laravel-model-stats')->hasConfigFile(); if (! config('model-stats.enabled')) { return; @@ -47,7 +44,7 @@ public function configurePackage(Package $package): void * * @return void */ - private function registerPublishing() + private function registerPublishing(): void { if ($this->app->runningInConsole()) { $this->publishes([ @@ -65,7 +62,7 @@ private function registerPublishing() * * @return void */ - protected function registerCommands() + protected function registerCommands(): void { if ($this->app->runningInConsole()) { $this->commands([ @@ -78,7 +75,7 @@ protected function registerCommands() /** * Load the package's migrations */ - protected function loadMigrations() + protected function loadMigrations(): void { $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); } diff --git a/src/ModelStatsServiceProvider.php b/src/ModelStatsServiceProvider.php index 42f2141..ee18425 100644 --- a/src/ModelStatsServiceProvider.php +++ b/src/ModelStatsServiceProvider.php @@ -13,7 +13,7 @@ class ModelStatsServiceProvider extends ServiceProvider * * @return void */ - public function boot() + public function boot(): void { $this->authorization(); } @@ -23,7 +23,7 @@ public function boot() * * @return void */ - protected function authorization() + protected function authorization(): void { $this->gate(); @@ -40,12 +40,11 @@ protected function authorization() * * @return void */ - protected function gate() + protected function gate(): void { Gate::define('viewModelStats', function ($user) { - return in_array($user->email, [ - // - ]); + return in_array($user->email, [// + ], true); }); } } diff --git a/src/Models/Dashboard.php b/src/Models/Dashboard.php index 77372f3..77fabfd 100644 --- a/src/Models/Dashboard.php +++ b/src/Models/Dashboard.php @@ -1,16 +1,21 @@ table = Config::get('model-stats.table_name'); + parent::__construct($attributes); + } protected $fillable = [ 'name', @@ -19,6 +24,15 @@ class Dashboard extends Model ]; protected $casts = [ - 'body' => 'array', + 'body' => 'json', + ]; + + protected $attributes = [ + 'body' => '{"widgets":[]}', ]; + + protected static function newFactory(): DashboardFactory + { + return DashboardFactory::new(); + } } diff --git a/src/Services/ModelStats.php b/src/Services/ModelStats.php index 3f4f3ba..d06712d 100644 --- a/src/Services/ModelStats.php +++ b/src/Services/ModelStats.php @@ -7,9 +7,9 @@ class ModelStats { - public $class; + public string $class; - public function __construct($class) + public function __construct(string $class) { $this->class = $class; } @@ -57,7 +57,7 @@ public function getPeriodTotal( Carbon $from, Carbon $to, string $dateFieldName = 'created_at' - ) { + ): array { $diff = $to->diffInDays($from); $periodCount = $this->class::where($dateFieldName, '>=', $from->startOfDay()) @@ -79,7 +79,7 @@ public function getGroupByCount( Carbon $to, string $dateFieldName, string $aggregateColumn - ) { + ): array { $tableName = (new $this->class)->getTable(); $mapping = []; diff --git a/src/Services/Tinker.php b/src/Services/Tinker.php index 9d97b3b..abfc21f 100644 --- a/src/Services/Tinker.php +++ b/src/Services/Tinker.php @@ -3,6 +3,7 @@ namespace Jhumanj\LaravelModelStats\Services; +use Exception; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Application; @@ -10,6 +11,7 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; +use Illuminate\Database\QueryException; use Laravel\Tinker\ClassAliasAutoloader; use Psy\Configuration; use Psy\ExecutionLoopClosure; @@ -24,13 +26,13 @@ */ class Tinker { - const FAKE_WRITE_HOST = 'database_write_not_allowed_with_model_stats'; + public const FAKE_WRITE_HOST = 'database_write_not_allowed_with_model_stats'; /** @var \Symfony\Component\Console\Output\BufferedOutput */ - protected $output; + protected BufferedOutput $output; /** @var \Psy\Shell */ - protected $shell; + protected Shell $shell; public function __construct() { @@ -54,11 +56,11 @@ public function execute(string $phpCode): string // Detect db write exception if (! $this->lastExecSuccess() && isset($resultVars['_e'])) { $lastException = $resultVars['_e']; - if (get_class($lastException) === 'Illuminate\Database\QueryException') { - if (Str::of($lastException->getMessage())->contains(self::FAKE_WRITE_HOST)) { - return "For safety reasons, you can only query data with ModelStats. Write operations are forbidden."; - } - } + if (($lastException instanceof QueryException) + && Str::of($lastException->getMessage()) + ->contains(self::FAKE_WRITE_HOST)) { + return "For safety reasons, you can only query data with ModelStats. Write operations are forbidden."; + } } // Make sure we have a result var @@ -80,7 +82,7 @@ public function getCustomCodeResult() try { $result = $this->shell->getScopeVariable('result'); - } catch (\Exception $exception) { + } catch (Exception $exception) { ray($exception); return null; @@ -95,7 +97,7 @@ public function getCustomCodeResult() /** * Check if last execution worked without exceptions */ - public function lastExecSuccess() + public function lastExecSuccess(): bool { return $this->shell->getLastExecSuccess(); } @@ -103,7 +105,7 @@ public function lastExecSuccess() /** * Prevents unwanted database modifications by enabling creating and using a readonly connection. */ - public function readonly() + public function readonly(): static { $defaultConnection = config('database.default'); $databaseConnection = Config::get('database.connections.'.$defaultConnection); @@ -124,7 +126,7 @@ public function readonly() /** * Inject chart dates (if needed) in the shell. */ - public function injectDates(Carbon $dateFrom, Carbon $dateTo) + public function injectDates(Carbon $dateFrom, Carbon $dateTo): static { $this->shell->setScopeVariables([ 'dateFrom' => $dateFrom, @@ -199,7 +201,7 @@ protected function ignoreCommentsAndPhpTags(array $token) protected function cleanOutput(string $output): string { - $output = preg_replace('/(?s)()|Exit: Ctrl\+D/ms', '$2', $output); + $output = preg_replace('/(?s)()|Exit: {2}Ctrl\+D/ms', '$2', $output); return trim($output); } diff --git a/stubs/ModelStatsServiceProvider.stub b/stubs/ModelStatsServiceProvider.stub index 88b6041..b8c2128 100644 --- a/stubs/ModelStatsServiceProvider.stub +++ b/stubs/ModelStatsServiceProvider.stub @@ -15,7 +15,7 @@ class ModelStatsServiceProvider extends Provider * * @return void */ - protected function gate() + protected function gate(): void { Gate::define('viewModelStats', function ($user) { return in_array($user->email, [ From c9c4b1d6199b4f34bc7a473483bb3cd65530776a Mon Sep 17 00:00:00 2001 From: JhumanJ Date: Tue, 12 Oct 2021 10:59:44 +0000 Subject: [PATCH 2/3] Fix styling --- src/Http/Controllers/Controller.php | 2 +- src/Http/Controllers/CustomCodeController.php | 2 +- src/Http/Controllers/DashboardController.php | 4 ++-- src/Http/Controllers/HomeController.php | 11 ++++++----- src/Http/Middleware/CustomCodeEnabled.php | 2 +- src/Http/Requests/Dashboard/StoreRequest.php | 2 +- src/Http/Requests/Dashboard/UpdateRequest.php | 6 +++--- src/Http/Requests/Widgets/DataRequest.php | 13 +++++++------ src/LaravelModelStatsServiceProvider.php | 1 + src/Models/Dashboard.php | 2 +- src/Services/Tinker.php | 8 ++++---- 11 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/Http/Controllers/Controller.php b/src/Http/Controllers/Controller.php index af95461..18567ff 100644 --- a/src/Http/Controllers/Controller.php +++ b/src/Http/Controllers/Controller.php @@ -2,9 +2,9 @@ namespace Jhumanj\LaravelModelStats\Http\Controllers; -use Illuminate\Http\JsonResponse; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Validation\ValidatesRequests; +use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller as BaseController; class Controller extends BaseController diff --git a/src/Http/Controllers/CustomCodeController.php b/src/Http/Controllers/CustomCodeController.php index 7b73002..9764429 100644 --- a/src/Http/Controllers/CustomCodeController.php +++ b/src/Http/Controllers/CustomCodeController.php @@ -3,9 +3,9 @@ namespace Jhumanj\LaravelModelStats\Http\Controllers; use Carbon\Carbon; +use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Validation\Rule; -use Illuminate\Http\JsonResponse; use Jhumanj\LaravelModelStats\Http\Middleware\CustomCodeEnabled; use Jhumanj\LaravelModelStats\Services\Tinker; diff --git a/src/Http/Controllers/DashboardController.php b/src/Http/Controllers/DashboardController.php index ff1e6f9..dc9baa9 100644 --- a/src/Http/Controllers/DashboardController.php +++ b/src/Http/Controllers/DashboardController.php @@ -2,10 +2,10 @@ namespace Jhumanj\LaravelModelStats\Http\Controllers; -use Illuminate\Http\JsonResponse; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Http\JsonResponse; use Jhumanj\LaravelModelStats\Http\Requests\Dashboard\StoreRequest; use Jhumanj\LaravelModelStats\Http\Requests\Dashboard\UpdateRequest; use Jhumanj\LaravelModelStats\Models\Dashboard; diff --git a/src/Http/Controllers/HomeController.php b/src/Http/Controllers/HomeController.php index 3857732..714296b 100644 --- a/src/Http/Controllers/HomeController.php +++ b/src/Http/Controllers/HomeController.php @@ -2,15 +2,15 @@ namespace Jhumanj\LaravelModelStats\Http\Controllers; -use Schema; -use ReflectionClass; use Illuminate\Container\Container; -use Illuminate\Contracts\View\View; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; +use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; -use Illuminate\Contracts\Foundation\Application; +use ReflectionClass; +use Schema; class HomeController extends Controller { @@ -38,6 +38,7 @@ private function getModels(): Collection $models = collect(File::allFiles(app_path())) ->map(function ($item) { $path = $item->getRelativePathName(); + return sprintf( '\%s%s', Container::getInstance()->getNamespace(), @@ -57,7 +58,7 @@ private function getModels(): Collection }); - return $models->map(fn(string $class) => [ + return $models->map(fn (string $class) => [ 'class' => $class, 'fields' => $this->getClassFields($class), ])->sortByDesc('class')->values(); diff --git a/src/Http/Middleware/CustomCodeEnabled.php b/src/Http/Middleware/CustomCodeEnabled.php index a249275..b9cb646 100644 --- a/src/Http/Middleware/CustomCodeEnabled.php +++ b/src/Http/Middleware/CustomCodeEnabled.php @@ -15,7 +15,7 @@ class CustomCodeEnabled */ public function handle($request, $next) { - if (!config('model-stats.allow_custom_code')) { + if (! config('model-stats.allow_custom_code')) { return response([ 'message' => 'Custom code not enabled.', ], 403); diff --git a/src/Http/Requests/Dashboard/StoreRequest.php b/src/Http/Requests/Dashboard/StoreRequest.php index b2e23fa..7925bab 100644 --- a/src/Http/Requests/Dashboard/StoreRequest.php +++ b/src/Http/Requests/Dashboard/StoreRequest.php @@ -14,7 +14,7 @@ class StoreRequest extends FormRequest public function rules(): array { return [ - 'name' => 'required|string|max:60', + 'name' => 'required|string|max:60', 'description' => 'required', ]; } diff --git a/src/Http/Requests/Dashboard/UpdateRequest.php b/src/Http/Requests/Dashboard/UpdateRequest.php index 331698f..634f0b5 100644 --- a/src/Http/Requests/Dashboard/UpdateRequest.php +++ b/src/Http/Requests/Dashboard/UpdateRequest.php @@ -14,9 +14,9 @@ class UpdateRequest extends FormRequest public function rules(): array { return [ - 'name' => 'required|string|max:60', - 'description' => 'required', - 'body' => 'sometimes|required', + 'name' => 'required|string|max:60', + 'description' => 'required', + 'body' => 'sometimes|required', 'body.widgets' => 'sometimes|array', ]; } diff --git a/src/Http/Requests/Widgets/DataRequest.php b/src/Http/Requests/Widgets/DataRequest.php index d9eb983..142cae0 100644 --- a/src/Http/Requests/Widgets/DataRequest.php +++ b/src/Http/Requests/Widgets/DataRequest.php @@ -38,11 +38,11 @@ class DataRequest extends FormRequest public function rules(): array { return [ - 'model' => ['required', Rule::in($this->getModels())], - 'aggregate_type' => ['required', Rule::in(static::ALLOWED_AGGREGATES_TYPES)], - 'date_column' => 'required', - 'date_from' => 'required|date_format:Y-m-d|before:date_to', - 'date_to' => 'required|date_format:Y-m-d|after:date_from', + 'model' => ['required', Rule::in($this->getModels())], + 'aggregate_type' => ['required', Rule::in(static::ALLOWED_AGGREGATES_TYPES)], + 'date_column' => 'required', + 'date_from' => 'required|date_format:Y-m-d|before:date_to', + 'date_to' => 'required|date_format:Y-m-d|after:date_from', 'aggregate_column' => [Rule::requiredIf(in_array($this->aggregate_type, self::AGGREGATES_TYPES_WITH_AGGREGATE_COLUMN, true))], ]; } @@ -52,6 +52,7 @@ private function getModels(): Collection $models = collect(File::allFiles(app_path())) ->map(function ($item) { $path = $item->getRelativePathName(); + return sprintf('\%s%s', Container::getInstance() ->getNamespace(), strtr(substr($path, 0, strrpos($path, '.')), '/', '\\')); })->filter(function ($class) { @@ -60,7 +61,7 @@ private function getModels(): Collection if (class_exists($class)) { $reflection = new \ReflectionClass($class); $valid = $reflection->isSubclassOf(Model::class) - && !$reflection->isAbstract(); + && ! $reflection->isAbstract(); } return $valid; diff --git a/src/LaravelModelStatsServiceProvider.php b/src/LaravelModelStatsServiceProvider.php index b86b557..50fc484 100644 --- a/src/LaravelModelStatsServiceProvider.php +++ b/src/LaravelModelStatsServiceProvider.php @@ -14,6 +14,7 @@ public function boot(): LaravelModelStatsServiceProvider { $this->registerPublishing(); $this->registerCommands(); + return parent::boot(); } diff --git a/src/Models/Dashboard.php b/src/Models/Dashboard.php index 77fabfd..82c0ec5 100644 --- a/src/Models/Dashboard.php +++ b/src/Models/Dashboard.php @@ -2,9 +2,9 @@ namespace Jhumanj\LaravelModelStats\Models; -use Illuminate\Support\Facades\Config; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Config; use Jhumanj\LaravelModelStats\Database\Factories\DashboardFactory; class Dashboard extends Model diff --git a/src/Services/Tinker.php b/src/Services/Tinker.php index abfc21f..2b7bce7 100644 --- a/src/Services/Tinker.php +++ b/src/Services/Tinker.php @@ -3,15 +3,15 @@ namespace Jhumanj\LaravelModelStats\Services; -use Exception; use Carbon\Carbon; +use Exception; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\QueryException; use Illuminate\Foundation\Application; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; -use Illuminate\Database\QueryException; use Laravel\Tinker\ClassAliasAutoloader; use Psy\Configuration; use Psy\ExecutionLoopClosure; @@ -59,8 +59,8 @@ public function execute(string $phpCode): string if (($lastException instanceof QueryException) && Str::of($lastException->getMessage()) ->contains(self::FAKE_WRITE_HOST)) { - return "For safety reasons, you can only query data with ModelStats. Write operations are forbidden."; - } + return "For safety reasons, you can only query data with ModelStats. Write operations are forbidden."; + } } // Make sure we have a result var From 14b64e79036163b5ff6e815eaa51f83af97af561 Mon Sep 17 00:00:00 2001 From: Julien Nahum Date: Tue, 8 Mar 2022 11:05:15 +0100 Subject: [PATCH 3/3] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 200a751..500df92 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "php": "^8.0", "spatie/laravel-package-tools": "^1.4.3", "illuminate/contracts": "^8.37", - "illuminate/support": "^5.8|^6.0|^7.0|^8.0", + "illuminate/support": "^5.8|^6.0|^7.0|^8.0|^9.0", "laravel/tinker": "^1.0|^2.0" }, "require-dev": {