Some adjustments to make App properly initializable in phpstan context

This commit is contained in:
Benjamin Renard 2023-03-01 11:01:54 +01:00
parent 508dda6562
commit df4df3f746
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
7 changed files with 30 additions and 3 deletions

View file

@ -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;

View file

@ -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)) {

View file

@ -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' => (

View file

@ -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;

View file

@ -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();
}

View file

@ -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

View file

@ -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);