Skip to content

Commit

Permalink
Merge pull request #7 from philbates35/rector
Browse files Browse the repository at this point in the history
Composer: Install & configure Rector and fix all errors
  • Loading branch information
philbates35 authored Jan 24, 2024
2 parents d700d89 + f0bfafd commit c48ccfd
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 10 deletions.
20 changes: 13 additions & 7 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Run phpstan
run: composer run phpstan

php-cs-fixer:
rector:
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php@v2
Expand All @@ -65,6 +65,18 @@ jobs:
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Run rector
run: composer run rector -- --dry-run

php-cs-fixer:
runs-on: ubuntu-latest
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
- uses: actions/checkout@v4
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Run php-cs-fixer
run: composer run php-cs-fixer -- --dry-run

Expand All @@ -75,13 +87,7 @@ jobs:
with:
php-version: '8.3'
- uses: actions/checkout@v4
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Run phpcs
run: composer run phpcs
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Back end:
* Laravel 10
* PHPStan (& Larastan) set to the highest level, as well as phpstan/phpstan-strict-rules for extra strictness
* PHPUnit 10
* Rector
* PHP CS Fixer configured to use the latest PER coding standards
* PHP_CodeSniffer configured to use the latest PSR12 coding standards

Expand Down
3 changes: 3 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Override;

class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
#[Override]
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
Expand All @@ -20,6 +22,7 @@ protected function schedule(Schedule $schedule): void
/**
* Register the commands for the application.
*/
#[Override]
protected function commands(): void
{
$this->load(__DIR__ . '/Commands');
Expand Down
2 changes: 2 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Override;
use Throwable;

class Handler extends ExceptionHandler
Expand All @@ -23,6 +24,7 @@ class Handler extends ExceptionHandler
/**
* Register the exception handling callbacks for the application.
*/
#[Override]
public function register(): void
{
$this->reportable(static function (Throwable $e): void {
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
use Override;

class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*/
#[Override]
protected function redirectTo(Request $request): ?string
{
return $request->expectsJson() ? null : \route('login');
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Middleware/TrustHosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Middleware;

use Illuminate\Http\Middleware\TrustHosts as Middleware;
use Override;

class TrustHosts extends Middleware
{
Expand All @@ -13,6 +14,7 @@ class TrustHosts extends Middleware
*
* @return array<int, string|null>
*/
#[Override]
public function hosts(): array
{
return [
Expand Down
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
use Illuminate\Support\ServiceProvider;
use Illuminate\Translation\Translator;
use OutOfBoundsException;
use Override;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
#[Override]
public function register(): void
{
$this->app->resolving('translator', function (Translator $translator): void {
Expand Down
3 changes: 3 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use Override;

class EventServiceProvider extends ServiceProvider
{
Expand All @@ -25,6 +26,7 @@ class EventServiceProvider extends ServiceProvider
/**
* Register any events for your application.
*/
#[Override]
public function boot(): void
{
//
Expand All @@ -33,6 +35,7 @@ public function boot(): void
/**
* Determine if events and listeners should be automatically discovered.
*/
#[Override]
public function shouldDiscoverEvents(): bool
{
return false;
Expand Down
4 changes: 3 additions & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Override;

class RouteServiceProvider extends ServiceProvider
{
Expand All @@ -19,11 +20,12 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
public const HOME = '/home';
final public const HOME = '/home';

/**
* Define your route model bindings, pattern filters, and other route configuration.
*/
#[Override]
public function boot(): void
{
RateLimiter::for(
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"laravel/tinker": "^2.8"
},
"require-dev": {
"driftingly/rector-laravel": "^0.30.0",
"fakerphp/faker": "^1.9.1",
"friendsofphp/php-cs-fixer": "^3.47",
"larastan/larastan": "^2.8",
Expand All @@ -23,6 +24,7 @@
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-strict-rules": "^1.5",
"phpunit/phpunit": "^10.1",
"rector/rector": "^0.19.2",
"spatie/laravel-ignition": "^2.0",
"squizlabs/php_codesniffer": "^3.8"
},
Expand Down Expand Up @@ -74,6 +76,9 @@
"phpstan": [
"phpstan --ansi"
],
"rector": [
"rector process --ansi"
],
"php-cs-fixer": [
"php-cs-fixer fix --ansi"
],
Expand Down
93 changes: 92 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use RectorLaravel\Set\LaravelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/app',
__DIR__ . '/bootstrap',
__DIR__ . '/config',
__DIR__ . '/public',
__DIR__ . '/resources',
__DIR__ . '/routes',
__DIR__ . '/tests',
]);

$rectorConfig->skip([
__DIR__ . '/bootstrap/cache',
]);

// Larastan's bootstrap file doesn't run when Rector boots PHPStan, so we need to include it manually. See:
// * https://github.com/rectorphp/rector/issues/8006
// * https://github.com/larastan/larastan/issues/1664#issuecomment-1637152828
$rectorConfig->bootstrapFiles([__DIR__ . '/vendor/larastan/larastan/bootstrap.php']);
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon.dist');

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_83,

SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::STRICT_BOOLEANS,
SetList::PRIVATIZATION,
SetList::TYPE_DECLARATION,
SetList::EARLY_RETURN,
SetList::INSTANCEOF,

LaravelSetList::LARAVEL_100,
LaravelSetList::LARAVEL_CODE_QUALITY,
LaravelSetList::LARAVEL_FACADE_ALIASES_TO_FULL_NAMES,
LaravelSetList::LARAVEL_ELOQUENT_MAGIC_METHOD_TO_QUERY_BUILDER,

PHPUnitSetList::PHPUNIT_100,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES,
]);

$rectorConfig->skip([
// Enforcing final on everything doesn't really offer much value, and stops up from
// mocking a concrete class, in the rare occasion we need to do so.
FinalizeClassesWithoutChildrenRector::class,

// TODO Stop skipping this rule as soon as phpcs supports typed const
// See: https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/106
AddTypeToConstRector::class,
]);
};
2 changes: 1 addition & 1 deletion routes/channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
|
*/

Broadcast::channel('App.Models.User.{id}', static fn($user, $id) => (int) $user->id === (int) $id);
Broadcast::channel('App.Models.User.{id}', static fn($user, $id): bool => (int) $user->id === (int) $id);
Loading

0 comments on commit c48ccfd

Please sign in to comment.