diff --git a/composer.json b/composer.json index 7528fd5..458c8af 100644 --- a/composer.json +++ b/composer.json @@ -16,19 +16,19 @@ } ], "require": { - "php": "^8.0", - "spatie/laravel-package-tools": "^1.4.3", - "illuminate/contracts": "^8.37|^v9.0.0", - "illuminate/support": "^5.8|^6.0|^7.0|^8.0|^9.0", + "php": "^8.0|^8.1|^8.2", + "spatie/laravel-package-tools": "^1.4", + "illuminate/contracts": "^8.0|^v9.0|^v10.0", + "illuminate/support": "^8.0|^9.0|^10.0", "laravel/tinker": "^1.0|^2.0" }, "require-dev": { - "brianium/paratest": "^6.2", - "nunomaduro/collision": "^5.3", - "orchestra/testbench": "^6.15", - "phpunit/phpunit": "^9.3", - "spatie/laravel-ray": "^1.20", - "vimeo/psalm": "^4.4" + "brianium/paratest": "^6.2|^7.0", + "nunomaduro/collision": "^5.3|^6.0|^7.0", + "orchestra/testbench": "^6.15|^7.0|^8.0", + "phpunit/phpunit": "^9.3|^10.0", + "spatie/laravel-ray": "^1.2", + "vimeo/psalm": "^4.4|^5.0" }, "autoload": { "psr-4": { diff --git a/config/model-stats.php b/config/model-stats.php index 501bdd6..31d9ebf 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', false), + 'enabled' => env('MODEL_STATS_ENABLED', true), + 'allow_custom_code' => env('MODEL_STATS_CUSTOM_CODE', false), /* |-------------------------------------------------------------------------- @@ -29,7 +29,7 @@ | the existing middleware. Or, you can simply stick with this list. | */ - 'middleware' => [ + 'middleware' => [ 'web', \Jhumanj\LaravelModelStats\Http\Middleware\Authorize::class, ], @@ -43,7 +43,7 @@ | this configures the table name based on your connection. | */ - 'table_name' => 'model_stats_dashboards', + 'table_name' => 'model_stats_dashboards', /* |-------------------------------------------------------------------------- @@ -54,7 +54,7 @@ | This can be used to ensure a read-only connection, by using a custom connection with a read-only user. | */ - 'query_database_connection' => env('MODEL_STATS_DB_CONNECTION', env('DB_CONNECTION')), + 'query_database_connection' => env('MODEL_STATS_DB_CONNECTION', env('DB_CONNECTION')), /* |-------------------------------------------------------------------------- @@ -65,6 +65,6 @@ | 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 index bbfb803..a1ddcf5 100644 --- a/database/factories/DashboardFactory.php +++ b/database/factories/DashboardFactory.php @@ -2,8 +2,8 @@ namespace Jhumanj\LaravelModelStats\Database\Factories; -use Jhumanj\LaravelModelStats\Models\Dashboard; use Illuminate\Database\Eloquent\Factories\Factory; +use Jhumanj\LaravelModelStats\Models\Dashboard; class DashboardFactory extends Factory { @@ -12,9 +12,9 @@ class DashboardFactory extends Factory public function definition(): array { return [ - 'name' => $this->faker->name, + 'name' => $this->faker->name, 'description' => $this->faker->sentence, - 'body' => '{"widgets":[]}', + 'body' => '{"widgets":[]}', ]; } } diff --git a/database/migrations/create_model-stats_table.php b/database/migrations/create_model-stats_table.php index b3f0060..9c832b1 100644 --- a/database/migrations/create_model-stats_table.php +++ b/database/migrations/create_model-stats_table.php @@ -1,12 +1,12 @@ - - - - tests - - - - - ./src - - - - - - - - - - + + + + tests + + + + + + + + + + + + + + + ./src + + diff --git a/routes/web.php b/routes/web.php index 9f75310..4b77ce2 100644 --- a/routes/web.php +++ b/routes/web.php @@ -8,13 +8,19 @@ Route::prefix('api')->name('api.')->group(function () { Route::apiResource('dashboards', \Jhumanj\LaravelModelStats\Http\Controllers\DashboardController::class); - Route::post('widgets/data', - [\Jhumanj\LaravelModelStats\Http\Controllers\StatController::class, 'widgetData']); - Route::post('widgets/custom-code/data', - [\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'widgetData']); + Route::post( + 'widgets/data', + [\Jhumanj\LaravelModelStats\Http\Controllers\StatController::class, 'widgetData'] + ); + Route::post( + 'widgets/custom-code/data', + [\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'widgetData'] + ); - Route::post('widgets/custom-code/execute', - [\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'executeCustomCode']); + Route::post( + 'widgets/custom-code/execute', + [\Jhumanj\LaravelModelStats\Http\Controllers\CustomCodeController::class, 'executeCustomCode'] + ); }); Route::get('/{view?}', [\Jhumanj\LaravelModelStats\Http\Controllers\HomeController::class, 'home']) diff --git a/src/AuthorizesRequests.php b/src/AuthorizesRequests.php index 1d3a2a1..cab5216 100644 --- a/src/AuthorizesRequests.php +++ b/src/AuthorizesRequests.php @@ -9,34 +9,24 @@ trait AuthorizesRequests { /** * The callback that should be used to authenticate ModelStats users. - * - * @var \Closure */ public static Closure $authUsing; /** * Register the ModelStats authentication callback. - * - * @param \Closure $callback - * - * @return static */ public static function auth(Closure $callback): static { static::$authUsing = $callback; - return new static; + return new static(); } /** * Determine if the given request can access the ModelStats dashboard. - * - * @param \Illuminate\Http\Request $request - * - * @return bool */ public static function check(Request $request): bool { - return (static::$authUsing ?: fn () => app()->environment('local'))($request); + return (static::$authUsing)($request); } } diff --git a/src/Console/InstallModelStatsPackage.php b/src/Console/InstallModelStatsPackage.php index e0c8633..29d876a 100644 --- a/src/Console/InstallModelStatsPackage.php +++ b/src/Console/InstallModelStatsPackage.php @@ -1,6 +1,5 @@ delete(); return $this->success([ - 'message' => 'Dashboard deleted', + 'message' => 'Dashboard deleted', ]); } } diff --git a/src/Http/Controllers/HomeController.php b/src/Http/Controllers/HomeController.php index 6688c59..451d698 100644 --- a/src/Http/Controllers/HomeController.php +++ b/src/Http/Controllers/HomeController.php @@ -2,15 +2,14 @@ namespace Jhumanj\LaravelModelStats\Http\Controllers; -use Illuminate\Container\Container; 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\Support\Facades\Schema; use ReflectionClass; -use Schema; class HomeController extends Controller { @@ -41,7 +40,7 @@ private function getModels(): Collection return sprintf( '\%s%s', - Container::getInstance()->getNamespace(), + app()->getNamespace(), strtr(substr($path, 0, strrpos($path, '.')), '/', '\\') ); }) @@ -57,7 +56,6 @@ private function getModels(): Collection return $valid; }); - return $models->map(fn (string $class) => [ 'class' => $class, 'fields' => $this->getClassFields($class), @@ -66,6 +64,6 @@ private function getModels(): Collection private function getClassFields(string $class) { - return Schema::getColumnListing((new $class)->getTable()); + return Schema::getColumnListing((new $class())->getTable()); } } diff --git a/src/Http/Middleware/Authorize.php b/src/Http/Middleware/Authorize.php index d9b70f9..ba3f6d7 100644 --- a/src/Http/Middleware/Authorize.php +++ b/src/Http/Middleware/Authorize.php @@ -9,9 +9,8 @@ class Authorize /** * Handle the incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next - * + * @param \Illuminate\Http\Request $request + * @param \Closure $next * @return \Illuminate\Http\Response */ public function handle($request, $next) diff --git a/src/Http/Middleware/CustomCodeEnabled.php b/src/Http/Middleware/CustomCodeEnabled.php index b9cb646..e7967c2 100644 --- a/src/Http/Middleware/CustomCodeEnabled.php +++ b/src/Http/Middleware/CustomCodeEnabled.php @@ -1,6 +1,5 @@ map(function ($item) { $path = $item->getRelativePathName(); - return sprintf('\%s%s', Container::getInstance() - ->getNamespace(), strtr(substr($path, 0, strrpos($path, '.')), '/', '\\')); + return sprintf( + '\%s%s', + app()->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(); + && ! $reflection->isAbstract(); } return $valid; diff --git a/src/LaravelModelStatsServiceProvider.php b/src/LaravelModelStatsServiceProvider.php index 50fc484..93490ee 100644 --- a/src/LaravelModelStatsServiceProvider.php +++ b/src/LaravelModelStatsServiceProvider.php @@ -42,14 +42,12 @@ public function configurePackage(Package $package): void /** * Register the package's publishable resources. - * - * @return void */ private function registerPublishing(): void { if ($this->app->runningInConsole()) { $this->publishes([ - __DIR__ . '/../stubs/ModelStatsServiceProvider.stub' => app_path('Providers/ModelStatsServiceProvider.php'), + __DIR__.'/../stubs/ModelStatsServiceProvider.stub' => app_path('Providers/ModelStatsServiceProvider.php'), ], 'model-stats-provider'); $this->publishes([ @@ -60,8 +58,6 @@ private function registerPublishing(): void /** * Register the package's commands. - * - * @return void */ protected function registerCommands(): void { @@ -78,6 +74,6 @@ protected function registerCommands(): void */ protected function loadMigrations(): void { - $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); } } diff --git a/src/ModelStatsServiceProvider.php b/src/ModelStatsServiceProvider.php index ee18425..824f276 100644 --- a/src/ModelStatsServiceProvider.php +++ b/src/ModelStatsServiceProvider.php @@ -10,8 +10,6 @@ class ModelStatsServiceProvider extends ServiceProvider { /** * Bootstrap any application services. - * - * @return void */ public function boot(): void { @@ -20,8 +18,6 @@ public function boot(): void /** * Configure the ModelStats authorization services. - * - * @return void */ protected function authorization(): void { @@ -37,8 +33,6 @@ protected function authorization(): void * Register the ModelStats gate. * * This gate determines who can access ModelStats in non-local environments. - * - * @return void */ protected function gate(): void { diff --git a/src/Services/ModelStats.php b/src/Services/ModelStats.php index d06712d..a528adf 100644 --- a/src/Services/ModelStats.php +++ b/src/Services/ModelStats.php @@ -39,7 +39,7 @@ public function getDailyHistogram( $data = $dataQuery->select([ DB::raw('DATE('.$dateFieldName.') as date'), DB::raw('COUNT(*) as "count"'), - ])->get()->pluck("count", "date"); + ])->get()->pluck('count', 'date'); $cumulatedSum = null; if ($cumulated) { @@ -80,13 +80,13 @@ public function getGroupByCount( string $dateFieldName, string $aggregateColumn ): array { - $tableName = (new $this->class)->getTable(); + $tableName = (new $this->class())->getTable(); $mapping = []; DB::table($tableName)->where($dateFieldName, '>=', $from->startOfDay()) ->where($dateFieldName, '<=', $to->endOfDay()) ->groupBy($aggregateColumn) - ->select($aggregateColumn, DB::raw('count(*) as count')) + ->select([$aggregateColumn, DB::raw('count(*) as count')]) ->get() ->each(function ($row) use (&$mapping, $aggregateColumn) { $mapping[$row->{$aggregateColumn}] = $row->count; diff --git a/src/Services/Tinker.php b/src/Services/Tinker.php index 4e565fa..eb2dbc9 100644 --- a/src/Services/Tinker.php +++ b/src/Services/Tinker.php @@ -1,6 +1,5 @@ getMessage()) - ->contains("SQLSTATE[42501]: Insufficient privilege")) { - return "For safety reasons, you can only query data with ModelStats. Write operations are forbidden."; + ->contains('SQLSTATE[42501]: Insufficient privilege')) { + return 'For safety reasons, you can only query data with ModelStats. Write operations are forbidden.'; } }