From 6fbe75eab5ecc307f173958d3ad085fd07bbf895 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Thu, 10 Dec 2020 09:37:44 +0100
Subject: [PATCH] :ambulance: fix(core) Fix errors reported by phpstan to have
 small baseline

Removed phpstan.neon which will be in dev-tools instead.
Fixed some phpstan reported problems to avoid having a huge baseline.

issue #6114
---
 html/index.php                               | 16 ++++++++++-----
 html/main.php                                | 16 ++++++++++++++-
 html/secondfactor.php                        | 10 +++++++++-
 html/setup.php                               | 17 +++++++++++++---
 include/class_config.inc                     |  9 +++++----
 include/class_ldapMultiplexer.inc            | 14 +++++++++++--
 include/class_pluglist.inc                   |  2 +-
 include/functions.inc                        | 21 --------------------
 include/management/class_management.inc      |  3 +++
 include/php_setup.inc                        |  4 ++--
 include/simpleplugin/interface_SimpleTab.inc | 18 ++++++++---------
 include/variables_common.inc                 | 21 ++++++++++++++++++++
 phpstan.neon                                 | 20 -------------------
 plugins/generic/welcome/main.inc             |  7 +++++++
 14 files changed, 108 insertions(+), 70 deletions(-)
 delete mode 100644 phpstan.neon

diff --git a/html/index.php b/html/index.php
index dcbfeb0d5..e0fec91d6 100644
--- a/html/index.php
+++ b/html/index.php
@@ -31,6 +31,12 @@ header('X-XSS-Protection: 1; mode=block');
 header('X-Content-Type-Options: nosniff');
 header('X-Frame-Options: deny');
 
+/**
+ * @var Smarty $smarty    Defined in php_setup.inc
+ * @var string $BASE_DIR  Defined in php_setup.inc
+ * @var string $ssl       Defined in php_setup.inc
+ */
+
 /*****************************************************************************
  *                               M   A   I   N                               *
  *****************************************************************************/
@@ -51,7 +57,7 @@ if (isset($_REQUEST['signout']) && $_REQUEST['signout']) {
       phpCAS::client(
         CAS_VERSION_2_0,
         $config->get_cfg_value('casHost', 'localhost'),
-        (int)($config->get_cfg_value('casPort', 443)),
+        $config->get_cfg_value('casPort', '443'),
         $config->get_cfg_value('casContext', '')
       );
       // Set the CA certificate that is the issuer of the cert
@@ -108,20 +114,20 @@ session::un_set('plist');
 unset($plist);
 
 /* Set template compile directory */
-$smarty->compile_dir = $config->get_cfg_value('templateCompileDirectory', SPOOL_DIR);
+$smarty->setCompileDir($config->get_cfg_value('templateCompileDirectory', SPOOL_DIR));
 
 /* Check for compile directory */
-if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
+if (!(is_dir($smarty->getCompileDir()) && is_writable($smarty->getCompileDir()))) {
   throw new FatalError(
     htmlescape(sprintf(
       _('Directory "%s" specified as compile directory is not accessible!'),
-      $smarty->compile_dir
+      $smarty->getCompileDir()
     ))
   );
 }
 
 /* Check for old files in compile directory */
-clean_smarty_compile_dir($smarty->compile_dir);
+clean_smarty_compile_dir($smarty->getCompileDir());
 
 Language::init();
 
diff --git a/html/main.php b/html/main.php
index d5ac9e11c..f0c5ac8b9 100644
--- a/html/main.php
+++ b/html/main.php
@@ -19,6 +19,14 @@
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
 
+/**
+ * @var Smarty $smarty                  Defined in php_setup.inc
+ * @var string $BASE_DIR                Defined in php_setup.inc
+ * @var string $ssl                     Defined in php_setup.inc
+ * @var string $error_collector         Defined in php_setup.inc
+ * @var string $error_collector_mailto  Defined in php_setup.inc
+ */
+
 /* Basic setup, remove eventually registered sessions */
 require_once("../include/php_setup.inc");
 require_once("functions.inc");
@@ -89,12 +97,15 @@ session::set('_LAST_PAGE_REQUEST', time());
 logging::debug(DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
 
 /* Set template compile directory */
-$smarty->compile_dir = $config->get_cfg_value("templateCompileDirectory", SPOOL_DIR);
+$smarty->setCompileDir($config->get_cfg_value('templateCompileDirectory', SPOOL_DIR));
 
 Language::init();
 
 /* Prepare plugin list */
 pluglist::load();
+/**
+ * @var pluglist $plist built by pluglist::load
+ */
 
 /* Check previous plugin index */
 if (session::is_set('plugin_index')) {
@@ -215,6 +226,9 @@ if (($_SERVER['REQUEST_METHOD'] == 'POST')
 
 /* Load plugin */
 pluglist::runMainInc($plugin_index);
+/**
+ * @var string $display Filled by pluglist::runMainInc
+ */
 
 /* Print_out last ErrorMessage repeated string. */
 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
diff --git a/html/secondfactor.php b/html/secondfactor.php
index 5b6f833d6..8d70273c5 100644
--- a/html/secondfactor.php
+++ b/html/secondfactor.php
@@ -19,6 +19,14 @@
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
 
+/**
+ * @var Smarty $smarty                  Defined in php_setup.inc
+ * @var string $BASE_DIR                Defined in php_setup.inc
+ * @var string $ssl                     Defined in php_setup.inc
+ * @var string $error_collector         Defined in php_setup.inc
+ * @var string $error_collector_mailto  Defined in php_setup.inc
+ */
+
 /* Basic setup, remove eventually registered sessions */
 require_once("../include/php_setup.inc");
 require_once("functions.inc");
@@ -96,7 +104,7 @@ foreach (LoginPost::$secondFactorMethods as $secondFactorMethod) {
 session::set('DEBUGLEVEL', $config->get_cfg_value('DEBUGLEVEL'));
 
 /* Set template compile directory */
-$smarty->compile_dir = $config->get_cfg_value('templateCompileDirectory', SPOOL_DIR);
+$smarty->setCompileDir($config->get_cfg_value('templateCompileDirectory', SPOOL_DIR));
 
 Language::init();
 
diff --git a/html/setup.php b/html/setup.php
index c405c8582..093ba5a36 100644
--- a/html/setup.php
+++ b/html/setup.php
@@ -41,6 +41,14 @@ header('X-XSS-Protection: 1; mode=block');
 header('X-Content-Type-Options: nosniff');
 header('X-Frame-Options: deny');
 
+/**
+ * @var Smarty $smarty                  Defined in php_setup.inc
+ * @var string $BASE_DIR                Defined in php_setup.inc
+ * @var string $ssl                     Defined in php_setup.inc
+ * @var string $error_collector         Defined in php_setup.inc
+ * @var string $error_collector_mailto  Defined in php_setup.inc
+ */
+
 /* Set cookie lifetime to one day (The parameter is in seconds ) */
 session_set_cookie_params(24 * 60 * 60);
 // default cache_expire is 180
@@ -57,14 +65,14 @@ CSRFProtection::check();
 reset_errors();
 
 /* Set template compile directory */
-$smarty->compile_dir = SPOOL_DIR;
+$smarty->setCompileDir(SPOOL_DIR);
 
 /* Check for compile directory */
-if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
+if (!(is_dir($smarty->getCompileDir()) && is_writable($smarty->getCompileDir()))) {
   throw new FatalError(
     htmlescape(sprintf(
       _('Directory "%s" specified as compile directory is not accessible!'),
-      $smarty->compile_dir
+      $smarty->getCompileDir()
     ))
   );
 }
@@ -104,6 +112,9 @@ IconTheme::loadThemes('themes');
 $ui = new userinfoNoAuth('setup');
 /* Call setup */
 setup::mainInc();
+/**
+ * @var string $display filled by setup::mainInc
+ */
 
 $focus = '<script type="text/javascript">';
 $focus .= 'next_msg_dialog();';
diff --git a/include/class_config.inc b/include/class_config.inc
index 41083da99..dd70d2bb4 100644
--- a/include/class_config.inc
+++ b/include/class_config.inc
@@ -361,13 +361,14 @@ class config
       }
       asort($servers);
       reset($servers);
-    }
 
-    /* SERVER not defined? Load the one with the shortest base */
-    if (!isset($this->current['SERVER'])) {
-      $this->current['SERVER'] = key($servers);
+      /* SERVER not defined? Load the one with the shortest base */
+      if (!isset($this->current['SERVER'])) {
+        $this->current['SERVER'] = key($servers);
+      }
     }
 
+
     /* Parse LDAP referral informations */
     if (!isset($this->current['ADMINDN']) || !isset($this->current['ADMINPASSWORD'])) {
       $this->current['BASE']          = $this->current['REFERRAL'][$this->current['SERVER']]['BASE'];
diff --git a/include/class_ldapMultiplexer.inc b/include/class_ldapMultiplexer.inc
index 6cb5c86d3..fc745583e 100644
--- a/include/class_ldapMultiplexer.inc
+++ b/include/class_ldapMultiplexer.inc
@@ -25,8 +25,18 @@
  * Source code for class ldapMultiplexer
  */
 
- /*!
-  * \brief This class contains all function to manage ldap multiplexer
+ /**
+  * This class contains all function to manage ldap multiplexer
+  * @method void set_size_limit ($size)
+  * @method void cd ($dir)
+  * @method boolean|resource modify_batch (array $changes)
+  * @method string get_error ($details = TRUE)
+  * @method array get_objectclasses ($force_reload = FALSE)
+  * @method string|array fetch (bool $cleanUpNumericIndices = FALSE)
+  * @method string|resource search ($filter, $attrs = [], $scope = 'subtree', array $controls = NULL)
+  * @method string|resource cat ($dn, $attrs = ["*"], $filter = "(objectclass=*)")
+  * @method int count ()
+  * @method bool success ()
   */
 class ldapMultiplexer
 {
diff --git a/include/class_pluglist.inc b/include/class_pluglist.inc
index 78d652011..c87279fcd 100644
--- a/include/class_pluglist.inc
+++ b/include/class_pluglist.inc
@@ -572,7 +572,7 @@ class pluglist
       if (is_file("$plugin_dir/main.inc")) {
         $display = '';
         require("$plugin_dir/main.inc");
-      } elseif (is_callable([$plugin, 'mainInc'])) {
+      } elseif (is_callable([get_class($plugin), 'mainInc'])) {
         $plugin::mainInc();
       } else {
         throw new FatalError(
diff --git a/include/functions.inc b/include/functions.inc
index 5646e0591..64388aaa9 100644
--- a/include/functions.inc
+++ b/include/functions.inc
@@ -32,27 +32,6 @@ require_once(CACHE_DIR.'/'.CLASS_CACHE);
 require_once('functions_debug.inc');
 require_once('accept-to-gettext.inc');
 
-/* Define constants for debugging */
-define('DEBUG_TRACE',    1); /*! Debug level for tracing of common actions (save, check, etc.) */
-define('DEBUG_LDAP',     2); /*! Debug level for LDAP queries */
-define('DEBUG_DB',       4); /*! Debug level for database operations */
-define('DEBUG_SHELL',    8); /*! Debug level for shell commands */
-define('DEBUG_POST',     16); /*! Debug level for POST content */
-define('DEBUG_SESSION',  32); /*! Debug level for SESSION content */
-define('DEBUG_CONFIG',   64); /*! Debug level for CONFIG information */
-define('DEBUG_ACL',      128); /*! Debug level for ACL infos */
-define('DEBUG_SI',       256); /*! Debug level for communication with Argonaut */
-define('DEBUG_MAIL',     512); /*! Debug level for all about mail (mailAccounts, imap, sieve etc.) */
-define('DEBUG_FAI',      1024); /* FAI (incomplete) */
-
-/* Define constants for LDAP operations */
-define('LDAP_READ',   1);
-define('LDAP_ADD',    2);
-define('LDAP_MOD',    3);
-define('LDAP_DEL',    4);
-define('LDAP_SEARCH', 5);
-define('LDAP_AUTH',   6);
-
 /*!
  * \brief Does autoloading for classes used in FusionDirectory.
  *
diff --git a/include/management/class_management.inc b/include/management/class_management.inc
index a0b3b0f8d..0e62eb456 100644
--- a/include/management/class_management.inc
+++ b/include/management/class_management.inc
@@ -50,6 +50,9 @@ class management implements FusionDirectoryDialog
   protected $previousDns  = [];
 
   // The opened object.
+  /**
+   * @var ?simpleTabs
+   */
   protected $tabObject    = NULL;
   protected $dialogObject = NULL;
 
diff --git a/include/php_setup.inc b/include/php_setup.inc
index 6e0ad8434..1fb24c027 100644
--- a/include/php_setup.inc
+++ b/include/php_setup.inc
@@ -351,8 +351,8 @@ require(SMARTY);
 
 $smarty = new Smarty;
 
-$smarty->template_dir = $BASE_DIR.'/ihtml/';
-$smarty->caching      = FALSE;
+$smarty->setTemplateDir($BASE_DIR.'/ihtml/');
+$smarty->caching      = Smarty::CACHING_OFF;
 $smarty->assign('css_files', []);
 $smarty->assign('js_files', []);
 
diff --git a/include/simpleplugin/interface_SimpleTab.inc b/include/simpleplugin/interface_SimpleTab.inc
index 404fe925c..456636f0d 100644
--- a/include/simpleplugin/interface_SimpleTab.inc
+++ b/include/simpleplugin/interface_SimpleTab.inc
@@ -27,18 +27,16 @@
 /*! \brief This interface is implemented by classes intended to be used as tabs, mainly simplePlugin itself.
  */
 
+/**
+ * @property bool $is_account
+ * @property string $dn
+ * @property bool $is_template
+ * @property string $_template_cn (only for main tab of templates)
+ * @property array $attributesAccess (only for main tab of templates)
+ * @property simpleTabs $parent
+ */
 interface SimpleTab extends FusionDirectoryDialog
 {
-  /*
-   * Public vars expected as well by classes implementing this interface:
-   * bool $is_account
-   * string $dn
-   * bool $is_template
-   * string $_template_cn (only for main tab of templates)
-   * array $attributesAccess (only for main tab of templates)
-   * simpleTabs $parent
-   */
-
   /*
    * Public methods needed in some cases:
    * compute_dn (): string (only for main tab)
diff --git a/include/variables_common.inc b/include/variables_common.inc
index f79605da8..3621e9526 100644
--- a/include/variables_common.inc
+++ b/include/variables_common.inc
@@ -79,3 +79,24 @@ define('PHP_MIN_VERSION', '7.0.0');
  * \brief Toggle crashing on PHP error, used for test suites
  */
 define('PHP_ERROR_FATAL', 'FALSE');
+
+/* Define constants for debugging */
+define('DEBUG_TRACE',    1); /*! Debug level for tracing of common actions (save, check, etc.) */
+define('DEBUG_LDAP',     2); /*! Debug level for LDAP queries */
+define('DEBUG_DB',       4); /*! Debug level for database operations */
+define('DEBUG_SHELL',    8); /*! Debug level for shell commands */
+define('DEBUG_POST',     16); /*! Debug level for POST content */
+define('DEBUG_SESSION',  32); /*! Debug level for SESSION content */
+define('DEBUG_CONFIG',   64); /*! Debug level for CONFIG information */
+define('DEBUG_ACL',      128); /*! Debug level for ACL infos */
+define('DEBUG_SI',       256); /*! Debug level for communication with Argonaut */
+define('DEBUG_MAIL',     512); /*! Debug level for all about mail (mailAccounts, imap, sieve etc.) */
+define('DEBUG_FAI',      1024); /* FAI (incomplete) */
+
+/* Define constants for LDAP operations */
+define('LDAP_READ',   1);
+define('LDAP_ADD',    2);
+define('LDAP_MOD',    3);
+define('LDAP_DEL',    4);
+define('LDAP_SEARCH', 5);
+define('LDAP_AUTH',   6);
diff --git a/phpstan.neon b/phpstan.neon
deleted file mode 100644
index ea0e6fbb9..000000000
--- a/phpstan.neon
+++ /dev/null
@@ -1,20 +0,0 @@
-includes:
-  - phpstan-baseline.neon
-
-parameters:
-  bootstrapFiles:
-    - include/php_setup.inc
-    - include/class_msg_dialog.inc
-    - include/class_userinfo.inc
-  scanDirectories:
-    - /usr/share/php
-  fileExtensions:
-    - php
-    - inc
-  universalObjectCratesClasses:
-    - simplePlugin
-  excludes_analyse:
-    - */class_sieve*.inc
-    - */localfiles/*
-    - */vendor/*
-    - */test/*
diff --git a/plugins/generic/welcome/main.inc b/plugins/generic/welcome/main.inc
index 72ca1db7f..26ea89dca 100644
--- a/plugins/generic/welcome/main.inc
+++ b/plugins/generic/welcome/main.inc
@@ -19,6 +19,13 @@
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
 
+/**
+ * @var Smarty $smarty
+ * @var pluglist $plist
+ * @var userinfo $ui
+ * @var bool $cleanup
+ */
+
 if (!$cleanup) {
   $smarty->assign('iconmenu',       $plist->show_iconmenu());
   $smarty->assign('headline',       sprintf(_('Welcome %s!'), $ui->cn));
-- 
GitLab