Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
60.00% covered (warning)
60.00%
6 / 10
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SentryEventFilter
60.00% covered (warning)
60.00%
6 / 10
60.00% covered (warning)
60.00%
3 / 5
21.22
0.00% covered (danger)
0.00%
0 / 1
 beforeSend
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 shouldDrop
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 silentEnvironment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 runningUnderPhpunit
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 runningInTinker
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace App\Exceptions;
4
5use PHPUnit\Framework\TestCase;
6use Sentry\Event;
7use Sentry\EventHint;
8
9/**
10 * Sentry noise ticket (PHP-LARAVEL-67, 19, Z, 7Z, 7T): test suites, local dev
11 * machines and tinker sessions reported into the production project —
12 * Mockery_0::join(), __authtest__@example.com, client 999999, psysh parse
13 * errors, /Users/... paths.
14 *
15 * Decided at RUNTIME, per event, on purpose: the post-deploy `runTest` route
16 * runs the Feature suite on the deployed server, where config is cached with
17 * the production DSN and environment. Anything decided in a config file is
18 * frozen at `config:cache` time and can't tell a test run from a request.
19 *
20 * Wired as `before_send` in config/sentry.php as a [class, method] array —
21 * a closure would break `php artisan config:cache`.
22 */
23class SentryEventFilter
24{
25    /** Environments that never report, whatever DSN the local .env carries. */
26    private const SILENT_ENVIRONMENTS = ['local', 'testing'];
27
28    public static function beforeSend(Event $event, ?EventHint $hint = null): ?Event
29    {
30        return self::shouldDrop($hint) ? null : $event;
31    }
32
33    public static function shouldDrop(?EventHint $hint = null): bool
34    {
35        return self::runningUnderPhpunit()
36            || self::runningInTinker($hint)
37            || self::silentEnvironment();
38    }
39
40    private static function silentEnvironment(): bool
41    {
42        return in_array(app()->environment(), self::SILENT_ENVIRONMENTS, true);
43    }
44
45    /**
46     * PHPUnit's own classes are only loaded when a test suite is running —
47     * true even when APP_ENV and the config cache still say "production".
48     */
49    private static function runningUnderPhpunit(): bool
50    {
51        return defined('PHPUNIT_COMPOSER_INSTALL')
52            || class_exists(TestCase::class, false);
53    }
54
55    /** psysh errors are typos at a REPL prompt, never production defects. */
56    private static function runningInTinker(?EventHint $hint): bool
57    {
58        if ($hint?->exception !== null && str_starts_with($hint->exception::class, 'Psy\\')) {
59            return true;
60        }
61
62        return app()->runningInConsole() && ($_SERVER['argv'][1] ?? null) === 'tinker';
63    }
64}