1- <?php
1+ <?php declare (strict_types= 1 );
22/**
33 * Provides an interface to the NeuroDB configuration
44 *
5- * PHP Version 5
5+ * PHP Version 7
66 *
77 * @category Main
88 * @package Loris
@@ -49,11 +49,11 @@ class NDB_Config
4949 /**
5050 * The singleton design pattern - autoloads config file
5151 *
52- * @param string $configFile the neurodb config.xml config file
52+ * @param string|null $configFile the neurodb config.xml config file
5353 *
54- * @return NDB_Config object
54+ * @return \ NDB_Config object
5555 */
56- static function &singleton ($ configFile = null )
56+ static function &singleton (? string $ configFile = null ): \ NDB_Config
5757 {
5858 static $ config = null ;
5959 if (is_null ($ config )) {
@@ -90,7 +90,7 @@ class NDB_Config
9090 *
9191 * @throws Exception
9292 */
93- function load ($ configFile = "../project/config.xml " )
93+ function load (string $ configFile = "../project/config.xml " ): void
9494 {
9595 // load the configuration data into a global variable $config
9696 $ newroot = simplexml_load_file ($ configFile );
@@ -114,7 +114,7 @@ class NDB_Config
114114 *
115115 * @throws LorisException when any config file is missing
116116 */
117- function configFilePath ($ pathToFile = "config.xml " )
117+ function configFilePath (string $ pathToFile = "config.xml " ): string
118118 {
119119 // first check for a project/config.xml, then check for the config
120120 // file in a standard unix filesystem location -> /etc/loris/config.xml
@@ -152,6 +152,12 @@ class NDB_Config
152152 * and this gets it into the same format that was previously used
153153 * for backwards compatibility.
154154 *
155+ * FIXME Despite the name, this function does not just return arrays, but
156+ * also returns a string. Crucially, this is done in the process of creating
157+ * a DB connection and so we cannot add a return type to this function.
158+ * Future refactoring should be done to better encapsulate this logic to
159+ * allow for strong typing.
160+ *
155161 * @param SimpleXMLElement $xml The root element of the parsed XML.
156162 *
157163 * @return array of XML configuration
@@ -246,13 +252,17 @@ class NDB_Config
246252 /**
247253 * Gets a setting from the database config tables
248254 *
249- * @param string $name The name of the config setting to get
250- * @param integer $id (optional) The ID of the config setting we're
255+ * FIXME This function currently returns a string, array, or null.
256+ * Refactoring should be done to better encapsulate the logic in this
257+ * function to allow for strong typing.
258+ *
259+ * @param string $name The name of the config setting to get
260+ * @param int|null $id (optional) The ID of the config setting we're
251261 * getting to avoid ambiguity.
252262 *
253- * @return string The value from the database, or null if nothing found.
263+ * @return mixed The value from the database, or null if nothing found.
254264 */
255- function getSettingFromDB ($ name , $ id= null )
265+ function getSettingFromDB (string $ name , ? int $ id = null )
256266 {
257267 // These should never come from the DB
258268 switch ($ name ) {
@@ -340,7 +350,10 @@ class NDB_Config
340350 foreach ($ configSetting as $ childSetting ) {
341351 $ childName = $ childSetting ['Name ' ];
342352 $ childID = $ childSetting ['ChildID ' ];
343- $ childValue = $ this ->getSettingFromDB ($ childName , $ childID );
353+ $ childValue = $ this ->getSettingFromDB (
354+ $ childName ,
355+ intval ($ childID )
356+ );
344357 $ tree [$ childName ] = $ childValue ;
345358 }
346359 return $ tree ;
@@ -354,9 +367,9 @@ class NDB_Config
354367 *
355368 * @param string $name The name of the XML node to retrieve.
356369 *
357- * @return string The value from the config.xml
370+ * @return array| string The value from the config.xml
358371 */
359- function getSettingFromXML ($ name )
372+ function getSettingFromXML (string $ name )
360373 {
361374 // loop over the settings, and find the node
362375 foreach ($ this ->_settings as $ key => $ value ) {
@@ -375,13 +388,13 @@ class NDB_Config
375388 }
376389
377390 /**
378- * Gets a setting by name
379- *
380- * @param string $name The name of the setting to retrieve
381- *
382- * @return mixed The contents of the node identified by $name
383- */
384- function getSetting ($ name )
391+ * Gets a setting by name
392+ *
393+ * @param string $name The name of the setting to retrieve
394+ *
395+ * @return mixed The contents of the node identified by $name
396+ */
397+ function getSetting (string $ name )
385398 {
386399 try {
387400 $ XMLValue = $ this ->getSettingFromXML ($ name );
@@ -402,22 +415,22 @@ class NDB_Config
402415 }
403416
404417 /**
405- * Get list of projects for this projects, given a ProjectID.
406- *
407- * @param integer $ProjectID The ProjectID we want settings for
408- *
409- * @return array of settings for this project.
410- */
411- function getProjectSettings ($ ProjectID )
418+ * Get list of projects for this projects, given a ProjectID.
419+ *
420+ * @param integer $ProjectID The ProjectID we want settings for
421+ *
422+ * @return array of settings for this project.
423+ */
424+ function getProjectSettings (int $ ProjectID ): array
412425 {
413426 $ factory = NDB_Factory::singleton ();
414427 $ DB = $ factory ->database ();
415428 try
416429 {
417- $ info =$ DB ->pselectRow (
418- "SELECT * FROM Project WHERE ProjectID=:sp " ,
419- array ('sp ' => $ ProjectID )
420- );
430+ $ info =$ DB ->pselectRow (
431+ "SELECT * FROM Project WHERE ProjectID=:sp " ,
432+ array ('sp ' => $ ProjectID )
433+ );
421434 } catch (DatabaseException $ e )
422435 {
423436 return null ;
@@ -441,19 +454,15 @@ class NDB_Config
441454 *
442455 * @return array of settings for this subprojectID
443456 */
444- function getSubprojectSettings ($ subprojectID )
457+ function getSubprojectSettings (int $ subprojectID ): array
445458 {
446459 $ factory = NDB_Factory::singleton ();
447460 $ DB = $ factory ->database ();
448461
449- try {
450- $ info = $ DB ->pselectRow (
451- "SELECT * FROM subproject WHERE SubprojectID=:sp " ,
452- array ('sp ' => $ subprojectID )
453- );
454- } catch (DatabaseException $ e ) {
455- return null ;
456- }
462+ $ info = $ DB ->pselectRow (
463+ "SELECT * FROM subproject WHERE SubprojectID=:sp " ,
464+ array ('sp ' => $ subprojectID )
465+ );
457466 // Format the results the same way it was formatted in config.xml
458467 // This variable assignment was done for phpcs... if anyone figures out
459468 // a way around this, please correct it
@@ -469,6 +478,7 @@ class NDB_Config
469478 'RecruitmentTarget ' => $ info ['RecruitmentTarget ' ],
470479 );
471480 }
481+ return array ();
472482 }
473483
474484 /**
@@ -480,7 +490,7 @@ class NDB_Config
480490 *
481491 * @return boolean true if the user can access the menu item
482492 */
483- static function checkMenuPermission ($ menuID )
493+ static function checkMenuPermission (int $ menuID ): bool
484494 {
485495 $ DB = Database::singleton ();
486496 $ user = User::singleton ();
@@ -509,16 +519,16 @@ class NDB_Config
509519 * of using $this->getSetting() to give more options for how to load
510520 * the menus, exactly (config.xml, different database tables, etc..)
511521 *
512- * @param integer $parent The parent ID of the tree. This should not
522+ * @param int|null $parent The parent ID of the tree. This should not
513523 * be passed. It's used to build the tree recursively
514524 * from the LorisMenu table.
515525 *
516526 * @return array Label, Visible, Link, and ID to build the menu
517527 * for the currently logged in user
518528 */
519- static function getMenuTabs ($ parent = null )
529+ static function getMenuTabs (? int $ parent = null ): array
520530 {
521- $ DB = Database::singleton ();
531+ $ DB = \ Database::singleton ();
522532 if ($ parent === null ) {
523533 $ thisLevel = $ DB ->pselect (
524534 "SELECT Label,
@@ -532,7 +542,7 @@ class NDB_Config
532542 );
533543
534544 foreach ($ thisLevel as &$ thisRow ) {
535- $ nextLevel = NDB_Config::getMenuTabs ($ thisRow ['ID ' ]);
545+ $ nextLevel = NDB_Config::getMenuTabs (intval ( $ thisRow ['ID ' ]) );
536546
537547 if (!empty ($ nextLevel )) {
538548 $ thisRow ['subtabs ' ] = $ nextLevel ;
@@ -555,14 +565,14 @@ class NDB_Config
555565 $ LevelWithPerm = array_filter (
556566 $ thisLevel ,
557567 function ($ el ) {
558- return NDB_Config::checkMenuPermission ($ el ['ID ' ]);
568+ return NDB_Config::checkMenuPermission (intval ( $ el ['ID ' ]) );
559569 }
560570 );
561571
562572 if (!empty ($ LevelWithPerm )) {
563573 return $ LevelWithPerm ;
564574 }
565- return ;
575+ return array () ;
566576 }
567577 }
568578
@@ -573,7 +583,7 @@ class NDB_Config
573583 *
574584 * @return array of Label => URL
575585 */
576- function getExternalLinks ($ type )
586+ function getExternalLinks (string $ type ): array
577587 {
578588 $ factory = NDB_Factory::singleton ();
579589 $ db = $ factory ->database ();
0 commit comments