Skip to content

Commit

Permalink
update to php-cs-fixer and NoCompromises standard
Browse files Browse the repository at this point in the history
  • Loading branch information
joelclermont committed Dec 15, 2023
1 parent 4f0174f commit bf2fd26
Show file tree
Hide file tree
Showing 52 changed files with 437 additions and 355 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.env.backup
.phpstan.cache
.phpunit.cache
.php-cs-fixer.cache
auth.json
npm-debug.log
/.idea
Expand Down
5 changes: 5 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

return NoCompromises\PhpCsFixer\Config\Factory::create(__DIR__);
21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ If you want to use this project standard, here is a list of files you need to co
* `docker` folder
* `.gitgnore`
* `.github` folder
* `phpcs.xml`
* `.php-cs-fixer.php`
* `phpstan.neon`
* `phpunit.xml` and `phpunit-external.xml`
* `resources/js/app.js`
Expand All @@ -34,21 +34,21 @@ You can also remove:
Modifications are needed to the following files already in a default Laravel install:

* `.env.example` and `.env`
* copy from `APP_URL`, `COMPOSER_PROJECT_NAME`, `DOCKER_MYSQL_LOCAL_PORT`, `DOCKER_NGINX_LOCAL_PORT`, and `DOCKER_SERVER_NAME`
* copy from `APP_URL`, `COMPOSER_PROJECT_NAME`, `DOCKER_MYSQL_LOCAL_PORT`, `DOCKER_NGINX_LOCAL_PORT`, and `DOCKER_SERVER_NAME`
* copy `DB_HOST`, `DB_DATABASE`, `DB_USERNAME`, and `DB_PASSWORD`

Update your Composer packages and scripts:

* Install non-dev packages: `docker/bin/composer require roave/security-advisories sentry/sentry-laravel spatie/laravel-permission owen-it/laravel-auditing`
* Install dev packages: `docker/bin/composer require --dev barryvdh/laravel-ide-helper doctrine/dbal larastan/larastan laravel/telescope phpstan/phpstan-mockery slevomat/coding-standard squizlabs/php_codesniffer`
* Install dev packages: `docker/bin/composer require --dev barryvdh/laravel-ide-helper doctrine/dbal larastan/larastan laravel/telescope nocompromises/php-cs-fixer-config phpstan/phpstan-mockery`
* Move tinker to dev: `docker/bin/composer require --dev laravel/tinker` (answer YES if you are asked if you want to move)
* Add the following commands to the `post-update-cmd` script:
* `@php artisan ide-helper:generate`
* `@php artisan ide-helper:meta`
* Copy additional sections from `scripts`:
* `test`, `test-coverage`, and `test-external`
* `ide-helper-update`
* `phpcs` and `phpcbf`
* `phpcs` and `phpcs-fix`
* `larastan`
* `ci` and `laravel-cache`

Expand Down Expand Up @@ -106,9 +106,8 @@ Dev dependencies
* `doctrine/dbal`
* `larastan/larastan`
* `laravel/telescope`
* `nocompromises/php-cs-fixer-config`
* `phpstan/phpstan-mockery`
* `slevomat/coding-standard`
* `squizlabs/php_codesniffer`

We also move the `laravel/tinker` package into the dev requirements, so it's not installed in production.

Expand All @@ -119,18 +118,14 @@ Within the `scripts` section of `composer.json`, we make the following changes:
* Add `test`, `test-coverage`, and `test-external` scripts
* Add a dedicated `ide-helper-update` script to regenerate everything for that package
* Add `phpcs` for detecting code standard violation
* Add `phpcbf` for automatically fixing code standard violations - only used locally, mainly when adding a new rule
* Add `phpcs-fix` for automatically fixing code standard violations - only used locally, mainly when adding a new rule
* Add `larastan` for static analysis
* Add `ci` and `laravel-cache` for CI

Some scripts use a short command, like `phpcs`, but others use the `@php` alias, like `larastan`. The difference is subtle,
but using `@php` tells composer to use the same php process composer itself is already using. This is especially important
if you want composer's memory limit config to apply.

The `config` block has the code sniffer plugin pre-allowed. This plugin makes it zero-configuration to install the slevomat
ruleset. Without this, we'd need to tweak phpcs.xml, symlink folders, and do more work in CI. As of Composer 2.2, you have
to explicitly allow permission for plugin installation. This config block pre-approves this one expected plugin.

Anytime you add a private package, make sure to add a sanitized config to `auth.example.json`

### Front end
Expand All @@ -143,9 +138,9 @@ Instead of using a separate bootstrap file, we load any necessary code directly

### Code Standards

Configured with `phpcs.xml`
Configured with `.php-cs-fixer.php`

PHP Code Sniffer is used, not `php-cs-fixer` or `pint`.
PHP Coding Standards Fixer is used, not PHP_CodeSniffer or Laravel Pint.

### Static Analysis

Expand Down
5 changes: 1 addition & 4 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule): void
{
//
}
protected function schedule(Schedule $schedule): void {}

protected function commands(): void
{
Expand Down
6 changes: 2 additions & 4 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ class Handler extends ExceptionHandler
/**
* A list of exception types with their corresponding custom log levels.
*
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
* @var array<class-string<Throwable>, \Psr\Log\LogLevel::*>
*/
protected $levels = [
//
];

/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<\Throwable>>
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
//
];

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class Authenticate extends Middleware
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if (!$request->expectsJson()) {
return route('login');
}
}
Expand Down
1 change: 0 additions & 1 deletion app/Http/Middleware/EncryptCookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ class EncryptCookies extends Middleware
* @var array<int, string>
*/
protected $except = [
//
];
}
1 change: 0 additions & 1 deletion app/Http/Middleware/PreventRequestsDuringMaintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ class PreventRequestsDuringMaintenance extends Middleware
* @var array<int, string>
*/
protected $except = [
//
];
}
5 changes: 2 additions & 3 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ class RedirectIfAuthenticated
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param string|null ...$guards
* @param Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param string|null ...$guards
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next, ...$guards)
Expand Down
1 change: 0 additions & 1 deletion app/Http/Middleware/VerifyCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ class VerifyCsrfToken extends Middleware
* @var array<int, string>
*/
protected $except = [
//
];
}
4 changes: 1 addition & 3 deletions app/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,4 @@
* @method static \Illuminate\Database\Eloquent\Builder|Permission whereUpdatedAt($value)
* @mixin \Eloquent
*/
class Permission extends \Spatie\Permission\Models\Permission
{
}
class Permission extends \Spatie\Permission\Models\Permission {}
4 changes: 1 addition & 3 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,4 @@
* @method static \Illuminate\Database\Eloquent\Builder|Role whereUpdatedAt($value)
* @mixin \Eloquent
*/
class Role extends \Spatie\Permission\Models\Role
{
}
class Role extends \Spatie\Permission\Models\Role {}
5 changes: 1 addition & 4 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,5 @@ protected function registerTelescopeInLocal(): void
}
}

public function boot()
{
//
}
public function boot() {}
}
4 changes: 0 additions & 4 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ class AuthServiceProvider extends ServiceProvider

/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();

//
}
}
2 changes: 0 additions & 2 deletions app/Providers/BroadcastServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Expand Down
7 changes: 1 addition & 6 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ class EventServiceProvider extends ServiceProvider

/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
//
}
public function boot() {}

/**
* Determine if events and listeners should be automatically discovered.
Expand Down
4 changes: 0 additions & 4 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class RouteServiceProvider extends ServiceProvider

/**
* Define your route model bindings, pattern filters, and other route configuration.
*
* @return void
*/
public function boot()
{
Expand All @@ -42,8 +40,6 @@ public function boot()

/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting()
{
Expand Down
10 changes: 5 additions & 5 deletions app/Providers/TelescopeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public function register(): void
return true;
}

return $entry->isReportableException() ||
$entry->isFailedRequest() ||
$entry->isFailedJob() ||
$entry->isScheduledTask() ||
$entry->hasMonitoredTag();
return $entry->isReportableException()
|| $entry->isFailedRequest()
|| $entry->isFailedJob()
|| $entry->isScheduledTask()
|| $entry->hasMonitoredTag();
});
}

Expand Down
23 changes: 10 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nocompromises/my-project",
"type": "project",
"license": "NO-LICENSE",
"license": "proprietary",
"require": {
"php": "^8.2",
"ext-pdo": "*",
Expand All @@ -22,12 +22,11 @@
"laravel/telescope": "^4.9",
"laravel/tinker": "^2.8",
"mockery/mockery": "^1.4.4",
"nocompromises/php-cs-fixer-config": "^1.0",
"nunomaduro/collision": "^7.0",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.1",
"roave/security-advisories": "dev-latest",
"slevomat/coding-standard": "^8.3",
"squizlabs/php_codesniffer": "^3.7"
"roave/security-advisories": "dev-latest"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -72,11 +71,11 @@
"@php artisan ide-helper:meta",
"@php artisan ide-helper:models --write --reset"
],
"phpcbf": [
"phpcbf"
],
"phpcs": [
"phpcs -s"
"php-cs-fixer check"
],
"phpcs-fix": [
"php-cs-fixer fix"
],
"larastan": [
"@php vendor/bin/phpstan analyse"
Expand All @@ -103,9 +102,7 @@
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
"allow-plugins": {}
},
"minimum-stability": "stable",
"prefer-stable": true,
Expand All @@ -114,8 +111,8 @@
"test-coverage": "Runs all unit tests with XDebug code coverage, outputs to tests/html-coverage",
"test-external": "Runs the external test suite (tests that hit real APIs)",
"ide-helper-update": "Updates the IDE helper files",
"phpcbf": "Runs PHP_CodeSniffer's code beautifier",
"phpcs": "Runs PHP_CodeSniffer",
"phpcs": "Runs php-cs-fixer on the configured files and reports errors",
"phpcs-fix": "Runs php-cs-fixer on the configured files and automatically fixes errors",
"larastan": "Runs the Larastan (PHPStan)",
"ci": "Runs tasks that should be done for integration: code sniffing, static analysis, and tests",
"laravel-cache": "Runs Laravel caching commands (for CI and prod)"
Expand Down
Loading

0 comments on commit bf2fd26

Please sign in to comment.