From ad7e6030b8a6a6ae52bae9290309e510bcc0f5db Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Tue, 27 Aug 2024 17:32:07 +0200
Subject: [PATCH] wip poject class

---
 .../deployment/classes/project.class.php      | 157 ++++++++++--------
 1 file changed, 90 insertions(+), 67 deletions(-)

diff --git a/public_html/deployment/classes/project.class.php b/public_html/deployment/classes/project.class.php
index 6132350b..7d2b7332 100644
--- a/public_html/deployment/classes/project.class.php
+++ b/public_html/deployment/classes/project.class.php
@@ -865,7 +865,8 @@ class project extends base
                 $aDelete[] = $sDir2;
             } else {
                 echo t("class-project-warning-cannot-delete-build-dir", $sDir2);
-            };
+            }
+            ;
         }
 
         return $aDelete;
@@ -1142,29 +1143,29 @@ class project extends base
     }
 
     /**
-     * return array of all (active and inactive) phases
+     * Get array of all (active and inactive) phases
      * @return array
      */
-    public function getPhases()
+    public function getPhases(): array
     {
         return $this->_aConfig["phases"];
     }
 
     /**
-     * return array of all (active and inactive) phases
+     * Get array of all (active and inactive) phases
      * @return array
      */
-    public function getPlaces()
+    public function getPlaces(): array
     {
         return $this->_aPlaces;
     }
     /**
-     * get a flat array with active phases of the project
+     * Get a flat array with active phases of the project
      * @return array
      */
-    public function getActivePhases()
+    public function getActivePhases(): array
     {
-        $aReturn = array();
+        $aReturn = [];
         foreach (array_keys($this->_aConfig["phases"]) as $s) {
             if ($this->isActivePhase($s)) {
                 $aReturn[] = $s;
@@ -1176,8 +1177,9 @@ class project extends base
     /**
      * find the next active phase of a project
      * @param string $sPhase current phase; if empty the function sends back the first phase
+     * @return string
      */
-    public function getNextPhase($sPhase = false)
+    public function getNextPhase(string $sPhase = ''): string
     {
         if ($sPhase) {
             if (!array_key_exists($sPhase, $this->_aConfig["phases"])) {
@@ -1204,21 +1206,25 @@ class project extends base
     }
 
     /**
-     * get an array with deploy status ...  
+     * Get an array with deploy status ...  
      *    'inprogress'=>do versions differ from phase to phase = rollout of a version is in progress
           'hasQueue'=>is there a package in a queue (waiting for deployment time to get ready to be installed)
      * @return array
      */
-    public function getProgress()
+    public function getProgress(): array
     {
         $this->getAllPhaseInfos();
         return $this->_aData['progress'];
     }
     /**
      * check: is the deployment to the next phase enabled for this phase?
-     * @param type $sPhase  current phase
+     * It returns a string when current user has no permissions.
+     * Otherwise it returns true or false.
+     * 
+     * @param string $sPhase  current phase
+     * @return string|bool
      */
-    public function canAcceptPhase($sPhase = false)
+    public function canAcceptPhase(string $sPhase = ''): string|bool
     {
         if (
             !$this->oUser->hasPermission("project-action-accept") && !$this->oUser->hasPermission("project-action-accept-$sPhase")
@@ -1266,37 +1272,38 @@ class project extends base
     }
 
     /**
-     * get list of remote branches and tags
+     * Get list of remote branches and tags.
+     * It returns false if the VCS was not initialize or has no method getRemoteBranches()
+     * 
      * @param bool $bIgnoreCache  flag to ignore exiting cached data
-     * @return array|boolean
+     * @return bool|array
      */
-    public function getRemoteBranches($bIgnoreCache = false)
+    public function getRemoteBranches(bool $bIgnoreCache = false): bool|array
     {
         $this->log(__FUNCTION__ . "($bIgnoreCache) start");
         $this->_initVcs();
         if ($this->_oVcs) {
             if (!method_exists($this->_oVcs, "getRemoteBranches")) {
                 // the version control class does not have this method
-                return '';
+                return false;
             }
             return $this->_oVcs->getRemoteBranches($bIgnoreCache);
         }
         return false;
     }
 
-
-
     /**
-     * get current revision and log message from remote repo
+     * Get current revision and log message from remote repo
+     * 
      * @param boolean  $bRefresh  optional: refresh data; default: use cache
      * @return array
      */
-    public function getRepoRevision($bRefresh = false)
+    public function getRepoRevision(bool $bRefresh = false): array
     {
         $this->log(__FUNCTION__ . "($bRefresh) start");
 
         if (!$this->_aPrjConfig["build"]["type"]) {
-            $this->_aData["phases"]["source"] = array("error" => t("class-project-error-repo-type-not-set"),);
+            $this->_aData["phases"]["source"] = array("error" => t("class-project-error-repo-type-not-set"), );
         } else {
             $this->_initVcs();
             if ($this->_oVcs) {
@@ -1312,15 +1319,15 @@ class project extends base
     }
 
     /**
-     * init version control system (git, ...)
-     * @return vcs-object
+     * Initialize version control system (git, ...) if it is not initialized yet
+     * @return object vcs-object
      */
-    protected function _initVcs()
+    protected function _initVcs(): object
     {
         $this->log(__FUNCTION__ . " start");
         if (!$this->_oVcs) {
             if (!$this->_aPrjConfig["build"]["type"]) {
-                $this->_aData["phases"]["source"] = array("error" => t("class-project-error-repo-type-not-set"),);
+                $this->_aData["phases"]["source"] = array("error" => t("class-project-error-repo-type-not-set"), );
             } else {
                 if (!@include_once("vcs." . $this->_aPrjConfig["build"]["type"] . ".class.php")) {
                     $this->_aData["phases"]["source"] = array(
@@ -1345,11 +1352,11 @@ class project extends base
     }
 
     /**
-     * get an array of enabled plugins
+     * Get an array of enabled plugins
      * @param string  $sSection  one of false|"rollout"|...
      * @return array
      */
-    public function getConfiguredPlugins($sSection = false)
+    public function getConfiguredPlugins(string $sSection = ''): array
     {
         $aReturn = array();
         if (!$sSection) {
@@ -1363,23 +1370,24 @@ class project extends base
     }
 
     /**
-     * get a location of a plugin file with full path
+     * Get a location of a plugin file with full path
+     * 
      * @param string  $sType         type of plugin, i.e. "rollout"
      * @param string  $sPluginName   Name of plugin
      * @return string
      */
-    protected function _getPluginFilename($sType, $sPluginName)
+    protected function _getPluginFilename(string $sType, string $sPluginName): string
     {
         return __DIR__ . '/../plugins/' . $sType . '/' . $sPluginName . '/' . $sType . '_' . $sPluginName . '.php';
     }
 
     /**
-     * get a flat array of all existing ssh keys
+     * Get a flat array of all existing ssh keys
      * @return array
      */
-    protected function _getSshKeys()
+    protected function _getSshKeys(): array
     {
-        $aReturn = array();
+        $aReturn =[];
         foreach (glob($this->_aConfig["dataDir"] . "/sshkeys/*.pub") as $filename) {
             $aReturn[] = str_replace(".pub", "", basename($filename));
         }
@@ -1388,11 +1396,13 @@ class project extends base
     }
 
     /**
-     * get a flat array with regexes of deploy times
+     * Get a flat array with regexes of deploy times.
+     * It returns false if the given phase is not active
+     * 
      * @param string $sPhase  phase
-     * @return array
+     * @return bool|array
      */
-    protected function _getDeploytimes($sPhase)
+    protected function _getDeploytimes(string $sPhase): bool|array
     {
         if (!$this->isActivePhase($sPhase)) {
             $sError = sprintf(t("class-project-warning-phase-not-active"), $sPhase);
@@ -1415,7 +1425,16 @@ class project extends base
     // SETTER
     // ----------------------------------------------------------------------
 
-    protected function _setProcessOutFile($sNewTempfile = false)
+    /**
+     * Generate a filename for the process output.
+     * The last output file will be deleted if it exists.
+     * It returns false if no pram for new basename was given.
+     * Otherwise it returns a filename with full path in temp folder.
+     * 
+     * @param string $sNewTempfile
+     * @return bool|string
+     */
+    protected function _setProcessOutFile(string $sNewTempfile = ''): bool|string
     {
         if ($this->_sProcessTempOut && file_exists($this->_sProcessTempOut)) {
             unlink($this->_sProcessTempOut);
@@ -1426,12 +1445,14 @@ class project extends base
     }
 
     /**
-     * get projects from ldap; it returns ldap search items with cn as 
+     * Get projects from ldap; it returns ldap search items with cn as 
      * array key.
+     * It returns false if no result was found
      * 
-     * @return array
+     * @param string  $sSearchFilter  LDAP search filter
+     * @return bool|array
      */
-    protected function _ldapProjectSearch($sSearchFilter)
+    protected function _ldapProjectSearch(string $sSearchFilter): bool|array
     {
         $aReturn = array();
         require_once("ldap.class.php");
@@ -1467,10 +1488,12 @@ class project extends base
     }
 
     /**
-     * load config of a project
-     * @return boolean
+     * Load config of a project with given project id and turn a bool for success
+     * 
+     * @param  string $sId  new project id of project to load
+     * @return boolean Success
      */
-    public function setProjectById($sId)
+    public function setProjectById(string $sId): bool
     {
         if ($sId !== preg_replace('/[^a-z0-9\-\_]/i', '', $sId)) {
             $this->_errors[] = sprintf(t("class-project-error-config-wrongid"), htmlentities($sId));
@@ -1542,9 +1565,9 @@ class project extends base
     /**
      * set a branchname
      * @param string $sBranchname name of the branch, i.e. "origin/master"
-     * @return bool
+     * @return string
      */
-    public function setBranchname($sBranchname)
+    public function setBranchname(string $sBranchname): string
     {
         $this->_sBranchname = $sBranchname;
         if ($this->_oVcs) {
@@ -1723,10 +1746,10 @@ class project extends base
 
         $sReturn .= '<pre>' . $this->_oVcs->getSources($sTempBuildDir) . '</pre>';
 
-        $aRepodata=$this->getRepoRevision();
-        $sRevisionShort=substr($aRepodata['revision'], 0, 8);
+        $aRepodata = $this->getRepoRevision();
+        $sRevisionShort = substr($aRepodata['revision'], 0, 8);
 
-        $sReturn .= $this->_oHtml->getBox("info", t('commitmessage') . '<pre>'.htmlentities($aRepodata['message']).'</pre>');
+        $sReturn .= $this->_oHtml->getBox("info", t('commitmessage') . '<pre>' . htmlentities($aRepodata['message']) . '</pre>');
         $sReturn .= $this->_execAndSend("ls -lisa $sTempBuildDir");
 
         if (!$this->_iRcAll == 0) {
@@ -1757,24 +1780,24 @@ class project extends base
             # /task#5047
 
         }
-        $aCivars=[
-            'branch'=>$aRepodata['branch'],
-            'branch_short'=>$aRepodata['shortname'],
-            'branch_type'=>$aRepodata['type'],
-            'revision'=>$aRepodata['revision'],
-            'revision_short'=>$sRevisionShort,
-            'imagepart'=>$aRepodata['type']=='tags' 
-                    ? $aRepodata['shortname']
-                    : $sRevisionShort
-                    ,
+        $aCivars = [
+            'branch' => $aRepodata['branch'],
+            'branch_short' => $aRepodata['shortname'],
+            'branch_type' => $aRepodata['type'],
+            'revision' => $aRepodata['revision'],
+            'revision_short' => $sRevisionShort,
+            'imagepart' => $aRepodata['type'] == 'tags'
+                ? $aRepodata['shortname']
+                : $sRevisionShort
+            ,
             // 'message'=>$aRepodata['message'],
         ];
 
         $sCfgContent .= "\n"
             . "# ---------- generated CI SERVER variables\n"
             . "\n"
-            ;
-        foreach ($aCivars as $sKey => $value){
+        ;
+        foreach ($aCivars as $sKey => $value) {
             $sCfgContent .= "export CI_$sKey=\"$value\"; \n";
         }
 
@@ -1918,13 +1941,13 @@ class project extends base
             'date' => $sTs,
             'version' => $sTs2,
             // 'branch' => $sBranch,
-            'branch'=>$aRepodata['branch'],
-            'branch_short'=>$aRepodata['shortname'],
-            'branch_type'=>$aRepodata['type'],
-            'revision'=>$aRepodata['revision'],
-            'revision_short'=>$sRevisionShort,
+            'branch' => $aRepodata['branch'],
+            'branch_short' => $aRepodata['shortname'],
+            'branch_type' => $aRepodata['type'],
+            'revision' => $aRepodata['revision'],
+            'revision_short' => $sRevisionShort,
 
-            'imagepart'=>$aCivars['imagepart'],
+            'imagepart' => $aCivars['imagepart'],
 
             'message' => $aRepodata['message'],
         );
@@ -1975,7 +1998,7 @@ class project extends base
                 return $this->_oHtml->getBox(
                     "error",
                     "FAILED to initialize build plugin " . $sPluginName . '<br>'
-                        . $sReturn
+                    . $sReturn
                 );
             }
 
-- 
GitLab