Skip to content

Commit

Permalink
Modernize codebase
Browse files Browse the repository at this point in the history
- Require PHP 8.3
- Require Laravel 11
- Require Laravel Telescope 5
- Replace phpcs with php-cs-fixer to be able to use the PER-CS standard
- Set phpstan to the highest level and enable bleeding edge
- Use type-safe config functions from Laravel 11
  • Loading branch information
adriaanzon committed Mar 15, 2024
1 parent 7ee52a5 commit ea490af
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 55 deletions.
23 changes: 9 additions & 14 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
name: Analysis

on:
push:
paths:
- '**.php'
- 'phpstan.neon.dist'
- 'phpcs.xml'
on: [push]

jobs:
phpstan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
- uses: ramsey/composer-install@v2
- uses: ramsey/composer-install@v3
- name: Run PHPStan
run: ./vendor/bin/phpstan --error-format=github
run: vendor/bin/phpstan

phpcs:
php-cs-fixer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
- uses: ramsey/composer-install@v2
- name: Run PHP CodeSniffer
run: ./vendor/bin/phpcs
- uses: ramsey/composer-install@v3
- name: Run PHP-CS-Fixer
run: vendor/bin/php-cs-fixer check --diff
8 changes: 4 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name: Tests

on: [push, pull_request]
on: [push]

jobs:
phpunit:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
- uses: ramsey/composer-install@v2
- uses: ramsey/composer-install@v3
- name: Run PHPUnit tests
run: ./vendor/bin/phpunit
run: vendor/bin/phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/
build/
composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
13 changes: 13 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->append([__FILE__])
;

return (new PhpCsFixer\Config())
->setRules([
'@PER-CS' => true,
])
->setFinder($finder)
;
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ composer require adriaanzon/laravel-telescope-monitor

## Configuration

The configuration channel can be configured by adding `TELESCOPE_MONITOR_LOG_CHANNEL` to your dotenv file, for example:
The log channel can be configured by adding `TELESCOPE_MONITOR_LOG_CHANNEL` to your dotenv file, for example:

```dotenv
TELESCOPE_MONITOR_LOG_CHANNEL=slack
```

In your local development environment, you'd typically disable Laravel Telescope Monitor by excluding `TELESCOPE_MONITOR_LOG_CHANNEL` from your .env file or setting it to null:

```dotenv
TELESCOPE_MONITOR_LOG_CHANNEL=null
```

To configure other options, you can publish the [configuration file][]:

```shell
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
],
"license": "MIT",
"require": {
"php": "^8.1",
"laravel/telescope": "^4.7|^5.0"
"php": "^8.3",
"laravel/framework": "^11.0",
"laravel/telescope": "^5.0"
},
"require-dev": {
"orchestra/testbench": "^7.1",
"phpstan/phpstan": "^1.4",
"squizlabs/php_codesniffer": "^3.6"
"friendsofphp/php-cs-fixer": "^3.51",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^9.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 0 additions & 7 deletions phpcs.xml

This file was deleted.

6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Access to an undefined property Laravel\\\\Telescope\\\\Storage\\\\EntryModel\\:\\:\\$content\\.$#"
count: 1
path: src/TelescopeEntryRepository.php
9 changes: 8 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
- vendor/larastan/larastan/extension.neon
- phpstan-baseline.neon

parameters:
level: 4
level: 9
paths:
- src
- config
databaseMigrationsPath:
- vendor/laravel/telescope/database/migrations
8 changes: 4 additions & 4 deletions src/ReportFailedJobListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ReportFailedJobListener
{
public function handle(JobFailed $event): void
{
if (! config('telescope.enabled') || ! config('telescope-monitor.report_failed_jobs')) {
if (! config()->boolean('telescope.enabled') || ! config()->boolean('telescope-monitor.report_failed_jobs')) {
return;
}

Expand All @@ -32,9 +32,9 @@ public function handle(JobFailed $event): void
*/
public function recordException(Throwable $exception): void
{
$trace = collect($exception->getTrace())->map(function ($item) {
return Arr::only($item, ['file', 'line']);
})->toArray();
$trace = collect($exception->getTrace())
->map(fn(array $item) => Arr::only($item, ['file', 'line']))
->toArray();

Telescope::recordException(new IncomingExceptionEntry($exception, [
'class' => get_class($exception),
Expand Down
21 changes: 11 additions & 10 deletions src/TelescopeExceptionLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
namespace AdriaanZon\TelescopeMonitor;

use Illuminate\Support\Facades\Log;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\IncomingExceptionEntry;

class TelescopeExceptionLogger
readonly class TelescopeExceptionLogger
{
protected TelescopeEntryRepository $entryRepository;

public function __construct(TelescopeEntryRepository $entryRepository)
{
$this->entryRepository = $entryRepository;
}
public function __construct(protected TelescopeEntryRepository $entryRepository) {}

/**
* Log new exceptions to a given channel, with a URL to the Telescope entry. By checking
* whether the latest entry with the same "family hash" wasn't marked as resolved, it
* prevents flooding the channel with subsequent occurrences of the same exception.
*
* @param list<IncomingEntry> $entries
*/
public function log(array $entries, ?string $channel = null): void
{
Expand All @@ -26,18 +24,21 @@ public function log(array $entries, ?string $channel = null): void
Log::channel($channel)->error($entry->content['message'], [
'type' => $entry->content['class'],
'location' => $entry->content['file'] . ':' . $entry->content['line'],
'details' => url(config('telescope.path') . '/exceptions/' . $entry->uuid),
'details' => url(config()->string('telescope.path') . '/exceptions/' . $entry->uuid),
]);
}
}
}

/**
* @param list<IncomingEntry> $entries
*/
public function logToConfiguredChannel(array $entries): void
{
if (blank($channel = config('telescope-monitor.log_channel'))) {
if (blank(config('telescope-monitor.log_channel'))) {
return;
}

$this->log($entries, $channel);
$this->log($entries, config()->string('telescope-monitor.log_channel'));
}
}
4 changes: 2 additions & 2 deletions src/TelescopeMonitorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TelescopeMonitorServiceProvider extends ServiceProvider
{
public function register()
public function register(): void
{
$this->publishes([__DIR__ . '/../config/telescope-monitor.php' => config_path('telescope-monitor.php')]);

Expand All @@ -20,7 +20,7 @@ public function register()
);
}

public function boot()
public function boot(): void
{
Event::listen(JobFailed::class, ReportFailedJobListener::class);
}
Expand Down
12 changes: 7 additions & 5 deletions tests/Integration/LogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@
namespace Tests\Integration;

use Exception;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Log;
use Laravel\Telescope\ExceptionContext;
use Laravel\Telescope\IncomingExceptionEntry;
use Laravel\Telescope\Telescope;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use Tests\TestCase;

class LogTest extends TestCase
{
use RefreshDatabase;

protected function usesNullLogChannel($app)
protected function usesNullLogChannel(Application $app): void
{
$app['config']->set('telescope-monitor.log_channel', 'null');
}

public function testExceptionsDontGetLoggedWithoutConfiguration()
public function testExceptionsDontGetLoggedWithoutConfiguration(): void
{
$exception = new Exception('whoops');

Expand All @@ -37,8 +39,8 @@ public function testExceptionsDontGetLoggedWithoutConfiguration()
$this->app->call(Telescope::store(...));
}

/** @define-env usesNullLogChannel */
public function testExceptionsGetLoggedToLogConfiguredChannel()
#[DefineEnvironment('usesNullLogChannel')]
public function testExceptionsGetLoggedToLogConfiguredChannel(): void
{
$exception = new Exception('whoops');

Expand All @@ -53,7 +55,7 @@ public function testExceptionsGetLoggedToLogConfiguredChannel()

Log::shouldReceive('channel->error')->once()->with('whoops', [
'type' => Exception::class,
'location' => __FILE__ . ':' . 43,
'location' => __FILE__ . ':' . 45,
'details' => 'http://localhost/telescope/exceptions/' . $entry->uuid,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
return new class () extends Migration {
/**
* Run the migrations.
*
Expand Down

0 comments on commit ea490af

Please sign in to comment.