Skip to content

Commit

Permalink
Lint files
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Mar 12, 2024
1 parent f65ef80 commit 94a38cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
13 changes: 8 additions & 5 deletions src/Sentry/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

namespace Studiometa\WPToolkit\Sentry;

class Config {
class Config
{
public function __construct(

Check warning on line 7 in src/Sentry/Config.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Config.php#L7

Added line #L7 was not covered by tests
public string $dsn,
public string $js_loader_script,
public string $environment,
public string $release,
public float $traces_sample_rate,
public float $profiles_sample_rate,
)
{}
) {
}

public function toArray(): array {
public function toArray(): array

Check warning on line 17 in src/Sentry/Config.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Config.php#L17

Added line #L17 was not covered by tests
{
return [
'dsn' => $this->dsn,
'environment' => $this->environment,
Expand All @@ -23,7 +25,8 @@ public function toArray(): array {
];

Check warning on line 25 in src/Sentry/Config.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Config.php#L19-L25

Added lines #L19 - L25 were not covered by tests
}

public function getJsConfig():string {
public function getJsConfig():string|false

Check warning on line 28 in src/Sentry/Config.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Config.php#L28

Added line #L28 was not covered by tests
{
$config = $this->toArray();
unset($config['dsn']);
return json_encode($config);

Check warning on line 32 in src/Sentry/Config.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Config.php#L30-L32

Added lines #L30 - L32 were not covered by tests
Expand Down
34 changes: 20 additions & 14 deletions src/Sentry/Sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@

use Studiometa\WPToolkit\Sentry\Config;
use function Sentry\init as init_sentry;
use function Studiometa\WPToolkit\{env,request};
use function Studiometa\WPToolkit\env;
use function Studiometa\WPToolkit\request;
use function WeCodeMore\earlyAddAction as early_add_action;

class Sentry {
private static Config $config;

static public function configureWithDefaults( string $root_dir ) {
class Sentry
{
public static function configureWithDefaults(string $root_dir): void

Check warning on line 13 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L13

Added line #L13 was not covered by tests
{
$release = '';

Check warning on line 15 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L15

Added line #L15 was not covered by tests

$file_path = $root_dir . '/package.json';
if (file_exists($file_path)) {
$package = json_decode( file_get_contents( $file_path ) );
$release = $package->name . '@' . $package->version;
$package_content = file_get_contents($file_path);
if ($package_content) {

Check warning on line 20 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L17-L20

Added lines #L17 - L20 were not covered by tests
/** @var object{name: string, version:string} */
$package = json_decode($package_content);
$release = $package->name . '@' . $package->version;

Check warning on line 23 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L22-L23

Added lines #L22 - L23 were not covered by tests
}
}

$sample_rate = (float) (env('SENTRY_SAMPLE_RATE') ?: 0);
$traces_sample_rate = (float) (env('SENTRY_TRACES_SAMPLE_RATE') ?: $sample_rate);
$profiles_sample_rate = (float) (env('SENTRY_PROFILES_SAMPLE_RATE') ?: $sample_rate);

Check warning on line 29 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L27-L29

Added lines #L27 - L29 were not covered by tests

return self::configure(
self::configure(
new Config(
dsn: env('SENTRY_DSN'),
js_loader_script: env('SENTRY_JS_LOADER_SCRIPT'),
Expand All @@ -35,23 +40,24 @@ static public function configureWithDefaults( string $root_dir ) {
);
}

static private function configure( Config $config ) {
self::$config = $config;

private static function configure(Config $config): void

Check warning on line 43 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L43

Added line #L43 was not covered by tests
{
init_sentry($config->toArray());

Check warning on line 45 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L45

Added line #L45 was not covered by tests

early_add_action('init', function() use ($config) {
early_add_action('init', function () use ($config) {
wp_enqueue_script('sentry-loader-script', $config->js_loader_script, []);
$js_config = $config->getJsConfig();
$inline_script = "window.sentryOnLoad = () => { Sentry.init({$js_config}) }";
wp_add_inline_script('sentry-loader-script', $inline_script, 'before');
});

Check warning on line 52 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L47-L52

Added lines #L47 - L52 were not covered by tests

if ( $config->traces_sample_rate > 0 ) {
if ($config->traces_sample_rate > 0) {

Check warning on line 54 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L54

Added line #L54 was not covered by tests
// Setup Sentry performance + profiling
// Setup context for the full transaction
$transactionContext = new \Sentry\Tracing\TransactionContext();

Check warning on line 57 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L57

Added line #L57 was not covered by tests
$transactionContext->setName(request()->server->get('REQUEST_URI', 'wp-cli'));
/** @var string */
$transactionName = request()->server->get('REQUEST_URI', 'wp-cli');
$transactionContext->setName($transactionName);
$transactionContext->setOp('http.server');

Check warning on line 61 in src/Sentry/Sentry.php

View check run for this annotation

Codecov / codecov/patch

src/Sentry/Sentry.php#L59-L61

Added lines #L59 - L61 were not covered by tests

// Start the transaction
Expand Down

0 comments on commit 94a38cd

Please sign in to comment.