From df4df3f746baa04143ba5ab965db1ea938873e35 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 1 Mar 2023 11:01:54 +0100 Subject: [PATCH] Some adjustments to make App properly initializable in phpstan context --- src/Auth/Cas.php | 3 +++ src/Log.php | 9 +++++++-- src/SentryIntegration.php | 3 +++ src/SentrySpan.php | 3 +++ src/SentryTransaction.php | 6 ++++++ src/Session.php | 6 +++++- src/Tpl.php | 3 +++ 7 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Auth/Cas.php b/src/Auth/Cas.php index 07f695e..cc52c8c 100644 --- a/src/Auth/Cas.php +++ b/src/Auth/Cas.php @@ -22,6 +22,9 @@ class Cas extends Method { * @return boolean */ public static function init() { + // In phpstan context, do not initialize + if (defined('__PHPSTAN_RUNNING__') && constant('__PHPSTAN_RUNNING__')) + return true; self :: $fake_authenticated_user = App :: get( 'auth.cas.fake_authenticated_user', null, 'string'); if (self :: $fake_authenticated_user) return true; diff --git a/src/Log.php b/src/Log.php index 65bbe83..9c9d061 100644 --- a/src/Log.php +++ b/src/Log.php @@ -86,8 +86,13 @@ class Log { // PHP error_log() fallback self :: $error_log_fallback = App::get('log.error_log_fallback', true, 'bool'); - // Set log level - self :: set_level($level?$level:App::get('log.level')); + // Set log level: + // Note: in Phpstan context, force FATAL level + if (defined('__PHPSTAN_RUNNING__') && constant('__PHPSTAN_RUNNING__')) + self :: set_level('FATAL'); + else + self :: set_level($level?$level:App::get('log.level')); + // Log PHP errors if (!is_null($php_errors_levels)) { diff --git a/src/SentryIntegration.php b/src/SentryIntegration.php index 397efe0..4afde2a 100644 --- a/src/SentryIntegration.php +++ b/src/SentryIntegration.php @@ -37,6 +37,9 @@ class SentryIntegration { */ public static function init($dsn=null, $traces_sample_rate=null, $php_error_types=null) { + // In phpstan context, do not initialize + if (defined('__PHPSTAN_RUNNING__') && constant('__PHPSTAN_RUNNING__')) + return; \Sentry\init([ 'dsn' => $dsn?$dsn:App::get('sentry.dsn'), 'traces_sample_rate' => ( diff --git a/src/SentrySpan.php b/src/SentrySpan.php index a4c9570..22236d2 100644 --- a/src/SentrySpan.php +++ b/src/SentrySpan.php @@ -47,6 +47,9 @@ class SentrySpan { * @return void */ public function __construct($op, $name) { + // In phpstan context, do not initialize + if (defined('__PHPSTAN_RUNNING__') && constant('__PHPSTAN_RUNNING__')) + return; $this -> parent = \Sentry\SentrySdk::getCurrentHub()->getSpan(); // Check if we have a parent span (this is the case if we started a transaction earlier) if (is_null($this -> parent)) return; diff --git a/src/SentryTransaction.php b/src/SentryTransaction.php index 0e456d8..e89ad89 100644 --- a/src/SentryTransaction.php +++ b/src/SentryTransaction.php @@ -27,6 +27,9 @@ class SentryTransaction { * @return void */ public function __construct($op=null, $name=null) { + // In phpstan context, do not initialize + if (defined('__PHPSTAN_RUNNING__') && constant('__PHPSTAN_RUNNING__')) + return; // Setup context for the full transaction $this->context = new \Sentry\Tracing\TransactionContext(); $this->context->setName( @@ -50,6 +53,9 @@ class SentryTransaction { * @return void */ public function __destruct() { + // In phpstan context, do nothing + if (defined('__PHPSTAN_RUNNING__') && constant('__PHPSTAN_RUNNING__')) + return; SentrySpan :: finishAll(); $this->transaction->finish(); } diff --git a/src/Session.php b/src/Session.php index 11499d0..d0e1d3b 100644 --- a/src/Session.php +++ b/src/Session.php @@ -22,7 +22,11 @@ class Session { * @return void */ public static function init($max_duration=null, $timeout=null) { - if (php_sapi_name() == "cli") + // In CLI or Phpstan context, do not initialize + if ( + php_sapi_name() == "cli" + || (defined('__PHPSTAN_RUNNING__') && constant('__PHPSTAN_RUNNING__')) + ) return; // Define session max duration diff --git a/src/Tpl.php b/src/Tpl.php index 6e16dbb..8acb4be 100644 --- a/src/Tpl.php +++ b/src/Tpl.php @@ -97,6 +97,9 @@ class Tpl { */ public static function init($templates_dir=null, $templates_c_dir=null, $debug_ajax=null, $static_root_url=null) { + // In phpstan context, do not initialize + if (defined('__PHPSTAN_RUNNING__') && constant('__PHPSTAN_RUNNING__')) + return; // Handle templates directories self :: $core_templates_directory = realpath(__DIR__."/../templates"); self :: register_templates_directory(self :: $core_templates_directory);