From c95e636501d5a88478920f9c772857d169103af8 Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tvdijen@gmail.com>
Date: Mon, 4 Feb 2019 20:03:01 +0100
Subject: [PATCH] Fixes for modules/statistics

---
 modules/statistics/bin/loganalyzer.php        |   2 +-
 modules/statistics/bin/logcleaner.php         |   2 +-
 modules/statistics/hooks/hook_configpage.php  |   2 +
 modules/statistics/hooks/hook_cron.php        |   2 +-
 modules/statistics/hooks/hook_frontpage.php   |   2 +
 modules/statistics/hooks/hook_sanitycheck.php |   2 +
 modules/statistics/lib/AccessCheck.php        |   7 +-
 modules/statistics/lib/Aggregator.php         |  67 +++++++++++-
 modules/statistics/lib/DateHandler.php        |  44 +++++++-
 modules/statistics/lib/DateHandlerMonth.php   |  21 +++-
 modules/statistics/lib/Graph/GoogleCharts.php |  35 ++++--
 modules/statistics/lib/LogCleaner.php         |  24 ++++-
 modules/statistics/lib/RatioDataset.php       |  28 ++++-
 modules/statistics/lib/Ruleset.php            |  32 +++++-
 modules/statistics/lib/StatDataset.php        | 101 +++++++++++++++++-
 .../lib/Statistics/FieldPresentation/Base.php |  15 +++
 .../Statistics/FieldPresentation/Entity.php   |   3 +
 .../lib/Statistics/Rulesets/BaseRule.php      |  52 ++++++++-
 .../lib/Statistics/Rulesets/Ratio.php         |  42 +++++++-
 .../statistics/lib/StatisticsController.php   |   8 ++
 modules/statistics/www/showstats.php          |   8 ++
 21 files changed, 467 insertions(+), 32 deletions(-)

diff --git a/modules/statistics/bin/loganalyzer.php b/modules/statistics/bin/loganalyzer.php
index 85ae093f3..81e3d698a 100755
--- a/modules/statistics/bin/loganalyzer.php
+++ b/modules/statistics/bin/loganalyzer.php
@@ -70,8 +70,8 @@ foreach ($results as $slot => $val) {
 
 /**
  * This function prints the help output.
+ * @return void
  */
-
 function printHelp()
 {
     global $progName;
diff --git a/modules/statistics/bin/logcleaner.php b/modules/statistics/bin/logcleaner.php
index b160cdab2..18432cf38 100755
--- a/modules/statistics/bin/logcleaner.php
+++ b/modules/statistics/bin/logcleaner.php
@@ -70,8 +70,8 @@ if (!$dryrun) {
 
 /**
  * This function prints the help output.
+ * @return void
  */
-
 function printHelp()
 {
     global $progName;
diff --git a/modules/statistics/hooks/hook_configpage.php b/modules/statistics/hooks/hook_configpage.php
index d9c1f1df7..36853ed2d 100644
--- a/modules/statistics/hooks/hook_configpage.php
+++ b/modules/statistics/hooks/hook_configpage.php
@@ -1,8 +1,10 @@
 <?php
+
 /**
  * Hook to add the statistics module to the config page.
  *
  * @param \SimpleSAML\XHTML\Template &$template The template that we should alter in this hook.
+ * @return void
  */
 function statistics_hook_configpage(\SimpleSAML\XHTML\Template &$template)
 {
diff --git a/modules/statistics/hooks/hook_cron.php b/modules/statistics/hooks/hook_cron.php
index a3ee4952c..f0de516fa 100644
--- a/modules/statistics/hooks/hook_cron.php
+++ b/modules/statistics/hooks/hook_cron.php
@@ -4,8 +4,8 @@
  * Hook to run a cron job.
  *
  * @param array &$croninfo  Output
+ * @return void
  */
-
 function statistics_hook_cron(&$croninfo)
 {
     assert(is_array($croninfo));
diff --git a/modules/statistics/hooks/hook_frontpage.php b/modules/statistics/hooks/hook_frontpage.php
index 2dade3b2a..bb4e93417 100644
--- a/modules/statistics/hooks/hook_frontpage.php
+++ b/modules/statistics/hooks/hook_frontpage.php
@@ -1,8 +1,10 @@
 <?php
+
 /**
  * Hook to add the modinfo module to the frontpage.
  *
  * @param array &$links  The links on the frontpage, split into sections.
+ * @return void
  */
 function statistics_hook_frontpage(&$links)
 {
diff --git a/modules/statistics/hooks/hook_sanitycheck.php b/modules/statistics/hooks/hook_sanitycheck.php
index 879ee3ee7..7d9fa2216 100644
--- a/modules/statistics/hooks/hook_sanitycheck.php
+++ b/modules/statistics/hooks/hook_sanitycheck.php
@@ -1,8 +1,10 @@
 <?php
+
 /**
  * Hook to do sanity checks
  *
  * @param array &$hookinfo  hookinfo
+ * @return void
  */
 function statistics_hook_sanitycheck(&$hookinfo)
 {
diff --git a/modules/statistics/lib/AccessCheck.php b/modules/statistics/lib/AccessCheck.php
index 750a5d052..875071441 100644
--- a/modules/statistics/lib/AccessCheck.php
+++ b/modules/statistics/lib/AccessCheck.php
@@ -7,13 +7,16 @@ namespace SimpleSAML\Module\statistics;
  *
  * @package SimpleSAMLphp
  */
-
 class AccessCheck
 {
     /**
      * Check that the user has access to the statistics.
-     *
      * If the user doesn't have access, send the user to the login page.
+     *
+     * @param \SimpleSAML\Configuration $statconfig
+     * @return void
+     * @throws \Exception
+     * @throws \SimpleSAML\Error\Exception
      */
     public static function checkAccess(\SimpleSAML\Configuration $statconfig)
     {
diff --git a/modules/statistics/lib/Aggregator.php b/modules/statistics/lib/Aggregator.php
index 966012d63..dc311d4d4 100644
--- a/modules/statistics/lib/Aggregator.php
+++ b/modules/statistics/lib/Aggregator.php
@@ -6,21 +6,40 @@ namespace SimpleSAML\Module\statistics;
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class Aggregator
 {
+    /** @var \SimpleSAML\Configuration */
     private $statconfig;
+
+    /** @var string */
     private $statdir;
+
+    /** @var string */
     private $inputfile;
+
+    /** @var array */
     private $statrules;
+
+    /** @var int */
     private $offset;
-    private $metadata;
+
+    /** @var array|null */
+    private $metadata = null;
+
+    /** @var bool */
     private $fromcmdline;
+
+    /** @var int */
     private $starttime;
+
+    /** @var array */
     private $timeres;
 
+
     /**
      * Constructor
+     *
+     * @param bool $fromcmdline
      */
     public function __construct($fromcmdline = false)
     {
@@ -32,11 +51,14 @@ class Aggregator
         $this->statrules = $this->statconfig->getValue('statrules');
         $this->timeres = $this->statconfig->getValue('timeres');
         $this->offset = $this->statconfig->getValue('offset', 0);
-        $this->metadata = null;
 
         $this->starttime = time();
     }
 
+
+    /**
+     * @return void
+     */
     public function dumpConfig()
     {
         echo 'Statistics directory   : '.$this->statdir."\n";
@@ -44,11 +66,19 @@ class Aggregator
         echo 'Offset                 : '.$this->offset."\n";
     }
 
+
+    /**
+     * @return void
+     */
     public function debugInfo()
     {
         echo 'Memory usage           : '.number_format(memory_get_usage() / 1048576, 2)." MB\n"; // 1024*1024=1048576
     }
 
+
+    /**
+     * @return void
+     */
     public function loadMetadata()
     {
         $filename = $this->statdir.'/.stat.metadata';
@@ -59,11 +89,19 @@ class Aggregator
         $this->metadata = $metadata;
     }
 
+
+    /**
+     * @return array|null
+     */
     public function getMetadata()
     {
         return $this->metadata;
     }
 
+
+    /**
+     * @return void
+     */
     public function saveMetadata()
     {
         $this->metadata['time'] = time() - $this->starttime;
@@ -74,6 +112,12 @@ class Aggregator
         file_put_contents($filename, serialize($this->metadata), LOCK_EX);
     }
 
+
+    /**
+     * @param bool $debug
+     * @return array
+     * @throws \Exception
+     */
     public function aggregate($debug = false)
     {
         $this->loadMetadata();
@@ -210,6 +254,12 @@ class Aggregator
         return $results;
     }
 
+
+    /**
+     * @param array $content
+     * @param mixed $colrule
+     * @return string
+     */
     private static function getDifCol($content, $colrule)
     {
         if (is_int($colrule)) {
@@ -225,6 +275,12 @@ class Aggregator
         }
     }
 
+
+    /**
+     * @param mixed $previous
+     * @param array $newdata
+     * @return array
+     */
     private function cummulateData($previous, $newdata)
     {
         $dataset = [];
@@ -244,6 +300,11 @@ class Aggregator
         return $dataset;
     }
 
+
+    /**
+     * @param array $results
+     * @return void
+     */
     public function store($results)
     {
         $datehandler = [
diff --git a/modules/statistics/lib/DateHandler.php b/modules/statistics/lib/DateHandler.php
index ae9807df9..17534e2d6 100644
--- a/modules/statistics/lib/DateHandler.php
+++ b/modules/statistics/lib/DateHandler.php
@@ -2,25 +2,30 @@
 
 namespace SimpleSAML\Module\statistics;
 
-/*
+/**
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class DateHandler
 {
+    /** @var int */
     protected $offset;
 
     /**
      * Constructor
      *
-     * @param array $offset Date offset
+     * @param int $offset Date offset
      */
     public function __construct($offset)
     {
         $this->offset = $offset;
     }
 
+
+    /**
+     * @param int $timestamp
+     * @return int
+     */
     protected function getDST($timestamp)
     {
         if (idate('I', $timestamp)) {
@@ -29,12 +34,24 @@ class DateHandler
         return 0;
     }
 
+
+    /**
+     * @param int $epoch
+     * @param int $slotsize
+     * @return float
+     */
     public function toSlot($epoch, $slotsize)
     {
         $dst = $this->getDST($epoch);
         return floor(($epoch + $this->offset + $dst) / $slotsize);
     }
 
+
+    /**
+     * @param int $slot
+     * @param int $slotsize
+     * @return int
+     */
     public function fromSlot($slot, $slotsize)
     {
         $temp = $slot * $slotsize - $this->offset;
@@ -42,16 +59,37 @@ class DateHandler
         return $slot * $slotsize - $this->offset - $dst;
     }
 
+
+    /**
+     * @param int $epoch
+     * @param string $dateformat
+     * @return string
+     */
     public function prettyDateEpoch($epoch, $dateformat)
     {
         return date($dateformat, $epoch);
     }
 
+
+    /**
+     * @param int $slot
+     * @param int $slotsize
+     * @param string $dateformat
+     * @return string
+     */
     public function prettyDateSlot($slot, $slotsize, $dateformat)
     {
         return $this->prettyDateEpoch($this->fromSlot($slot, $slotsize), $dateformat);
     }
 
+
+    /**
+     * @param int $from
+     * @param int $to
+     * @param int $slotsize
+     * @param string $dateformat
+     * @return string
+     */
     public function prettyHeader($from, $to, $slotsize, $dateformat)
     {
         $text = $this->prettyDateSlot($from, $slotsize, $dateformat);
diff --git a/modules/statistics/lib/DateHandlerMonth.php b/modules/statistics/lib/DateHandlerMonth.php
index 058795f57..4404c44e1 100644
--- a/modules/statistics/lib/DateHandlerMonth.php
+++ b/modules/statistics/lib/DateHandlerMonth.php
@@ -6,7 +6,6 @@ namespace SimpleSAML\Module\statistics;
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class DateHandlerMonth extends DateHandler
 {
     /**
@@ -19,6 +18,12 @@ class DateHandlerMonth extends DateHandler
         $this->offset = $offset;
     }
 
+
+    /**
+     * @param int $epoch
+     * @param int $slotsize
+     * @return int
+     */
     public function toSlot($epoch, $slotsize)
     {
         $dsttime = $this->getDST($epoch) + $epoch;
@@ -27,6 +32,12 @@ class DateHandlerMonth extends DateHandler
         return $slot;
     }
 
+
+    /**
+     * @param int $slot
+     * @param int $slotsize
+     * @return int
+     */
     public function fromSlot($slot, $slotsize)
     {
         $month = ($slot % 12);
@@ -34,6 +45,14 @@ class DateHandlerMonth extends DateHandler
         return mktime(0, 0, 0, $month + 1, 1, $year);
     }
 
+
+    /**
+     * @param int $from
+     * @param int $to
+     * @param int $slotsize
+     * @param string $dateformat
+     * @return string
+     */
     public function prettyHeader($from, $to, $slotsize, $dateformat)
     {
         $month = ($from % 12) + 1;
diff --git a/modules/statistics/lib/Graph/GoogleCharts.php b/modules/statistics/lib/Graph/GoogleCharts.php
index 129639de5..22f4e9993 100644
--- a/modules/statistics/lib/Graph/GoogleCharts.php
+++ b/modules/statistics/lib/Graph/GoogleCharts.php
@@ -9,7 +9,6 @@ namespace SimpleSAML\Module\statistics\Graph;
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class GoogleCharts
 {
     /**
@@ -36,12 +35,21 @@ class GoogleCharts
         $this->y = $y;
     }
 
+
+    /**
+     * @param array $axis
+     * @return string
+     */
     private function encodeaxis($axis)
     {
         return join('|', $axis);
     }
 
-    // t:10.0,58.0,95.0
+    /**
+     * t:10.0,58.0,95.0
+     * @param array $datasets
+     * @return string
+     */
     private function encodedata($datasets)
     {
         $setstr = [];
@@ -51,6 +59,11 @@ class GoogleCharts
         return 'e:'.join(',', $setstr);
     }
 
+
+    /**
+     * @param array $values
+     * @return string
+     */
     public static function extEncode($values) // $max = 4095, $min = 0
     {
         $extended_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
@@ -70,15 +83,17 @@ class GoogleCharts
         return $chardata;
     }
 
+
     /**
      * Generate a Google Charts URL which points to a generated image.
      * More documentation on Google Charts here:
      *   http://code.google.com/apis/chart/
      *
-     * @param string $axis        Axis
-     * @param string $axpis       Axis positions
+     * @param array $axis        Axis
+     * @param array $axispos       Axis positions
      * @param array $datasets    Datasets values
-     * @param integer $max         Max value. Will be the topmost value on the Y-axis.
+     * @param array $maxes         Max value. Will be the topmost value on the Y-axis.
+     * @return string
      */
     public function show($axis, $axispos, $datasets, $maxes)
     {
@@ -111,6 +126,12 @@ class GoogleCharts
         return $url;
     }
 
+
+    /**
+     * @param array $axis
+     * @param array $datasets
+     * @return string
+     */
     public function showPie($axis, $datasets)
     {
         $url = 'https://chart.apis.google.com/chart?'.
@@ -129,6 +150,7 @@ class GoogleCharts
         return $url;
     }
 
+
     /**
      * Takes a input value, and generates a value that suits better as a max
      * value on the Y-axis. In example 37.6 will not make a good max value, instead
@@ -145,7 +167,8 @@ class GoogleCharts
      *      }
      * </code>
      *
-     * @param integer $max    Input value.
+     * @param int $max    Input value.
+     * @return int
      */
     public static function roof($max)
     {
diff --git a/modules/statistics/lib/LogCleaner.php b/modules/statistics/lib/LogCleaner.php
index eafb50190..ee20e18bf 100644
--- a/modules/statistics/lib/LogCleaner.php
+++ b/modules/statistics/lib/LogCleaner.php
@@ -2,21 +2,32 @@
 
 namespace SimpleSAML\Module\statistics;
 
-/*
+/**
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class LogCleaner
 {
+    /** @var \SimpleSAML\Configuration */
     private $statconfig;
+
+    /** @var string */
     private $statdir;
+
+    /** @var string */
     private $inputfile;
+
+    /** @var array */
     private $statrules;
+
+    /** @var int */
     private $offset;
 
+
     /**
      * Constructor
+     *
+     * @param string|null $inputfile
      */
     public function __construct($inputfile = null)
     {
@@ -32,7 +43,8 @@ class LogCleaner
         }
     }
 
-    /*
+
+    /**
      * @return void
      */
     public function dumpConfig()
@@ -43,9 +55,10 @@ class LogCleaner
     }
 
 
-    /*
+    /**
      * @param bool $debug
      * @return array
+     * @throws \Exception
      */
     public function clean($debug = false)
     {
@@ -125,10 +138,11 @@ class LogCleaner
     }
 
 
-    /*
+    /**
      * @param array $todelete
      * @param string $outputfile
      * @return void
+     * @throws \Exceeption
      */
     public function store($todelete, $outputfile)
     {
diff --git a/modules/statistics/lib/RatioDataset.php b/modules/statistics/lib/RatioDataset.php
index 5449a3fd1..eaed46524 100644
--- a/modules/statistics/lib/RatioDataset.php
+++ b/modules/statistics/lib/RatioDataset.php
@@ -2,13 +2,15 @@
 
 namespace SimpleSAML\Module\statistics;
 
-/*
+/**
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class RatioDataset extends StatDataset
 {
+    /**
+     * @return void
+     */
     public function aggregateSummary()
     {
         /**
@@ -42,6 +44,12 @@ class RatioDataset extends StatDataset
         $this->summary = array_reverse($this->summary, true);
     }
 
+
+    /**
+     * @param string $k
+     * @param array $a
+     * @return int
+     */
     private function ag($k, $a)
     {
         if (array_key_exists($k, $a)) {
@@ -50,6 +58,12 @@ class RatioDataset extends StatDataset
         return 0;
     }
 
+
+    /**
+     * @param int $v1
+     * @param int $v2
+     * @return int|float
+     */
     private function divide($v1, $v2)
     {
         if ($v2 == 0) {
@@ -58,6 +72,12 @@ class RatioDataset extends StatDataset
         return ($v1 / $v2);
     }
 
+
+    /**
+     * @param array $result1
+     * @param array $result2
+     * @return array
+     */
     public function combine($result1, $result2)
     {
         $combined = [];
@@ -74,6 +94,10 @@ class RatioDataset extends StatDataset
         return $combined;
     }
 
+
+    /**
+     * @return null
+     */
     public function getPieData()
     {
         return null;
diff --git a/modules/statistics/lib/Ruleset.php b/modules/statistics/lib/Ruleset.php
index 935bad407..f0a41a70c 100644
--- a/modules/statistics/lib/Ruleset.php
+++ b/modules/statistics/lib/Ruleset.php
@@ -6,16 +6,25 @@ namespace SimpleSAML\Module\statistics;
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class Ruleset
 {
+    /** \SimpleSAML\Configuration */
     private $statconfig;
+
+    /** @var array */
     private $availrulenames;
+
+    /** @var array */
     private $availrules;
+
+    /** @var array */
     private $available;
 
+
     /**
      * Constructor
+     *
+     * @param \SimpleSAML\Configuration $statconfig
      */
     public function __construct($statconfig)
     {
@@ -23,6 +32,10 @@ class Ruleset
         $this->init();
     }
 
+
+    /**
+     * @return void
+     */
     private function init()
     {
         $statdir = $this->statconfig->getValue('statdir');
@@ -61,18 +74,30 @@ class Ruleset
         $this->availrulenames = $available_rules;
     }
 
+
+    /**
+     * @return array
+     */
     public function availableRules()
     {
         return $this->availrules;
     }
 
+
+    /**
+     * @return array
+     */
     public function availableRulesNames()
     {
         return $this->availrulenames;
     }
 
+
     /**
      * Resolve which rule is selected. Taking user preference and checks if it exists.
+     *
+     * @param array|null $preferRule
+     * @return array|null
      */
     private function resolveSelectedRule($preferRule = null)
     {
@@ -85,6 +110,11 @@ class Ruleset
         return $rule;
     }
 
+
+    /**
+     * @param array|null $preferRule
+     * @return \SimpleSAML\Module\statistics\Statistics\Rulesets\BaseRule
+     */
     public function getRule($preferRule)
     {
         $rule = $this->resolveSelectedRule($preferRule);
diff --git a/modules/statistics/lib/StatDataset.php b/modules/statistics/lib/StatDataset.php
index 6868d0237..5183f12db 100644
--- a/modules/statistics/lib/StatDataset.php
+++ b/modules/statistics/lib/StatDataset.php
@@ -6,28 +6,53 @@ namespace SimpleSAML\Module\statistics;
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class StatDataset
 {
+    /** @var \SimpleSAML\Configuration */
     protected $statconfig;
+
+    /** @var \SimpleSAML\Configuration */
     protected $ruleconfig;
+
+    /** @var \SimpleSAML\Configuration */
     protected $timeresconfig;
+
+    /** @var string */
     protected $ruleid;
 
+    /** @var int */
     protected $fileslot;
+
+    /** @var string */
     protected $timeres;
 
+    /** @var string */
     protected $delimiter;
+
+    /** @var array */
     protected $results;
+
+    /** @var array */
     protected $summary;
+
+    /** @var int */
     protected $max;
 
+    /** @var \SimpleSAML\Module\statistics\DateHandler */
     protected $datehandlerFile;
+
+    /** @var \SimpleSAML\Module\statistics\DateHandler */
     protected $datehandlerTick;
 
 
     /**
      * Constructor
+     *
+     * @param \SimpleSAML\Configuration $statconfig
+     * @param \SimpleSAML\Configuration $ruleconfig
+     * @param string $ruleid
+     * @param string $timeres
+     * @param int $fileslot
      */
     public function __construct($statconfig, $ruleconfig, $ruleid, $timeres, $fileslot)
     {
@@ -56,16 +81,29 @@ class StatDataset
         $this->loadData();
     }
 
+
+    /**
+     * @return int
+     */
     public function getFileSlot()
     {
         return $this->fileslot;
     }
 
+
+    /**
+     * @return string
+     */
     public function getTimeRes()
     {
         return $this->timeres;
     }
 
+
+    /**
+     * @param string $delimiter
+     * @return void
+     */
     public function setDelimiter($delimiter = '_')
     {
         if (empty($delimiter)) {
@@ -74,6 +112,10 @@ class StatDataset
         $this->delimiter = $delimiter;
     }
 
+
+    /**
+     * @return string|null
+     */
     public function getDelimiter()
     {
         if ($this->delimiter === '_') {
@@ -82,6 +124,10 @@ class StatDataset
         return $this->delimiter;
     }
 
+
+    /**
+     * @return void
+     */
     public function calculateMax()
     {
         $maxvalue = 0;
@@ -94,6 +140,10 @@ class StatDataset
         $this->max = Graph\GoogleCharts::roof($maxvalue);
     }
 
+
+    /**
+     * @return array
+     */
     public function getDebugData()
     {
         $debugdata = [];
@@ -110,6 +160,10 @@ class StatDataset
         return $debugdata;
     }
 
+
+    /**
+     * @return void
+     */
     public function aggregateSummary()
     {
         // aggregate summary table from dataset. To be used in the table view
@@ -127,6 +181,10 @@ class StatDataset
         $this->summary = array_reverse($this->summary, true);
     }
 
+
+    /**
+     * @return array
+     */
     public function getTopDelimiters()
     {
         // create a list of delimiter keys that has the highest total summary in this period
@@ -144,6 +202,10 @@ class StatDataset
         return $topdelimiters;
     }
 
+
+    /**
+     * @return array
+     */
     public function availDelimiters()
     {
         $availDelimiters = [];
@@ -153,6 +215,10 @@ class StatDataset
         return array_keys($availDelimiters);
     }
 
+
+    /**
+     * @return array
+     */
     public function getPieData()
     {
         $piedata = [];
@@ -167,21 +233,37 @@ class StatDataset
         return $piedata;
     }
 
+
+    /**
+     * @return int
+     */
     public function getMax()
     {
         return $this->max;
     }
 
+
+    /**
+     * @return array
+     */
     public function getSummary()
     {
         return $this->summary;
     }
 
+
+    /**
+     * @return array
+     */
     public function getResults()
     {
         return $this->results;
     }
 
+
+    /**
+     * @return array
+     */
     public function getAxis()
     {
         $slotsize = $this->timeresconfig->getValue('slot');
@@ -209,8 +291,10 @@ class StatDataset
         return ['axis' => $axis, 'axispos' => $axispos];
     }
 
-    /*
+
+    /**
      * Walk through dataset to get percent values from max into dataset[].
+     * @return array
      */
     public function getPercentValues()
     {
@@ -232,6 +316,11 @@ class StatDataset
         return $dataset;
     }
 
+
+    /**
+     * @return array
+     * @throws \Exception
+     */
     public function getDelimiterPresentation()
     {
         $config = \SimpleSAML\Configuration::getInstance();
@@ -257,6 +346,10 @@ class StatDataset
         return [];
     }
 
+
+    /**
+     * @return array
+     */
     public function getDelimiterPresentationPie()
     {
         $topdelimiters = $this->getTopDelimiters();
@@ -274,6 +367,10 @@ class StatDataset
         return $pieaxis;
     }
 
+
+    /**
+     * @return void
+     */
     public function loadData()
     {
         $statdir = $this->statconfig->getValue('statdir');
diff --git a/modules/statistics/lib/Statistics/FieldPresentation/Base.php b/modules/statistics/lib/Statistics/FieldPresentation/Base.php
index bb2568f1d..8f0bd99c0 100644
--- a/modules/statistics/lib/Statistics/FieldPresentation/Base.php
+++ b/modules/statistics/lib/Statistics/FieldPresentation/Base.php
@@ -4,10 +4,21 @@ namespace SimpleSAML\Module\statistics\Statistics\FieldPresentation;
 
 class Base
 {
+    /** @var array */
     protected $fields;
+
+    /** @var \SimpleSAML\XHTML\Template */
     protected $template;
+
+    /** @var string */
     protected $config;
 
+
+    /**
+     * @param array $fields
+     * @param string $config
+     * @param \SimpleSAML\XHTML\Template $template
+     */
     public function __construct($fields, $config, $template)
     {
         $this->fields = $fields;
@@ -15,6 +26,10 @@ class Base
         $this->config = $config;
     }
 
+
+    /**
+     * @return array
+     */
     public function getPresentation()
     {
         return ['_' => 'Total'];
diff --git a/modules/statistics/lib/Statistics/FieldPresentation/Entity.php b/modules/statistics/lib/Statistics/FieldPresentation/Entity.php
index 624d215b7..df88c8e4f 100644
--- a/modules/statistics/lib/Statistics/FieldPresentation/Entity.php
+++ b/modules/statistics/lib/Statistics/FieldPresentation/Entity.php
@@ -4,6 +4,9 @@ namespace SimpleSAML\Module\statistics\Statistics\FieldPresentation;
 
 class Entity extends Base
 {
+    /**
+     * @return array
+     */
     public function getPresentation()
     {
         $mh = \SimpleSAML\Metadata\MetaDataStorageHandler::getMetadataHandler();
diff --git a/modules/statistics/lib/Statistics/Rulesets/BaseRule.php b/modules/statistics/lib/Statistics/Rulesets/BaseRule.php
index 875557bc8..77c3a2051 100644
--- a/modules/statistics/lib/Statistics/Rulesets/BaseRule.php
+++ b/modules/statistics/lib/Statistics/Rulesets/BaseRule.php
@@ -6,16 +6,27 @@ namespace SimpleSAML\Module\statistics\Statistics\Rulesets;
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class BaseRule
 {
+    /** @var \SimpleSAML\Configuration */
     protected $statconfig;
+
+    /** @var \SimpleSAML\Configuration */
     protected $ruleconfig;
+
+    /** @var string */
     protected $ruleid;
-    protected $available;
+
+    /** @var array|null */
+    protected $available = null;
 
     /**
      * Constructor
+     *
+     * @param \SimpleSAML\Configuration $statconfig
+     * @param \SimpleSAML\Configuration $ruleconfig
+     * @param string $ruleid
+     * @param array $available
      */
     public function __construct($statconfig, $ruleconfig, $ruleid, $available)
     {
@@ -25,17 +36,24 @@ class BaseRule
         $this->ruleconfig = $ruleconfig;
         $this->ruleid = $ruleid;
 
-        $this->available = null;
         if (array_key_exists($ruleid, $available)) {
             $this->available = $available[$ruleid];
         }
     }
 
+
+    /**
+     * @return string
+     */
     public function getRuleID()
     {
         return $this->ruleid;
     }
 
+
+    /**
+     * @return array
+     */
     public function availableTimeRes()
     {
         $timeresConfigs = $this->statconfig->getValue('timeres');
@@ -48,6 +66,11 @@ class BaseRule
         return $available_times;
     }
 
+
+    /**
+     * @param string $timeres
+     * @return array
+     */
     public function availableFileSlots($timeres)
     {
         $timeresConfigs = $this->statconfig->getValue('timeres');
@@ -74,6 +97,11 @@ class BaseRule
         return $available_times;
     }
 
+
+    /**
+     * @param string $preferTimeRes
+     * @return string
+     */
     protected function resolveTimeRes($preferTimeRes)
     {
         $timeresavailable = array_keys($this->available);
@@ -86,6 +114,12 @@ class BaseRule
         return $timeres;
     }
 
+
+    /**
+     * @param string $timeres
+     * @param string $preferTime
+     * @return int
+     */
     protected function resolveFileSlot($timeres, $preferTime)
     {
         // Get which time (fileslot) to use.. First get a default, which is the most recent one.
@@ -97,6 +131,12 @@ class BaseRule
         return $fileslot;
     }
 
+
+    /**
+     * @param string $timeres
+     * @param string $preferTime
+     * @return array
+     */
     public function getTimeNavigation($timeres, $preferTime)
     {
         $fileslot = $this->resolveFileSlot($timeres, $preferTime);
@@ -118,6 +158,12 @@ class BaseRule
         return ['prev' => $available_times_prev, 'next' => $available_times_next];
     }
 
+
+    /**
+     * @param string $preferTimeRes
+     * @param string $preferTime
+     * @return \SimpleSAML\Module\statistics\StatDataset
+     */
     public function getDataSet($preferTimeRes, $preferTime)
     {
         $timeres = $this->resolveTimeRes($preferTimeRes);
diff --git a/modules/statistics/lib/Statistics/Rulesets/Ratio.php b/modules/statistics/lib/Statistics/Rulesets/Ratio.php
index 1f76f6b47..ed3a6eb99 100644
--- a/modules/statistics/lib/Statistics/Rulesets/Ratio.php
+++ b/modules/statistics/lib/Statistics/Rulesets/Ratio.php
@@ -6,14 +6,22 @@ namespace SimpleSAML\Module\statistics\Statistics\Rulesets;
  * @author Andreas Ă…kre Solberg <andreas.solberg@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class Ratio extends BaseRule
 {
+    /** @var \SimpleSAML\Module\statistics\Statistics\Rulesets\BaseRule $refrule1 */
     protected $refrule1;
+
+    /** @var \SimpleSAML\Module\statistics\Statistics\Rulesets\BaseRule $refrule2 */
     protected $refrule2;
 
+
     /**
      * Constructor
+     *
+     * @param \SimpleSAML\Configuration $statconfig
+     * @param \SimpleSAML\Configuration $ruleconfig
+     * @param string $ruleid
+     * @param array $available
      */
     public function __construct($statconfig, $ruleconfig, $ruleid, $available)
     {
@@ -33,31 +41,63 @@ class Ratio extends BaseRule
         $this->refrule2 = new BaseRule($this->statconfig, $statruleConfig2, $refNames[1], $available);
     }
 
+
+    /**
+     * @return array
+     */
     public function availableTimeRes()
     {
         return $this->refrule1->availableTimeRes();
     }
 
+
+    /**
+     * @param string $timeres
+     * @return array
+     */
     public function availableFileSlots($timeres)
     {
         return $this->refrule1->availableFileSlots($timeres);
     }
 
+
+    /**
+     * @param string $preferTimeRes
+     * @return string
+     */
     protected function resolveTimeRes($preferTimeRes)
     {
         return $this->refrule1->resolveTimeRes($preferTimeRes);
     }
 
+
+    /**
+     * @param string $timeres
+     * @param string $preferTime
+     * @return int
+     */
     protected function resolveFileSlot($timeres, $preferTime)
     {
         return $this->refrule1->resolveFileSlot($timeres, $preferTime);
     }
 
+
+    /**
+     * @param string $timeres
+     * @param string $preferTime
+     * @return array
+     */
     public function getTimeNavigation($timeres, $preferTime)
     {
         return $this->refrule1->getTimeNavigation($timeres, $preferTime);
     }
 
+
+    /**
+     * @param string $preferTimeRes
+     * @param string $preferTime
+     * @return \SimpleSAML\Module\statistics\RatioDataset
+     */
     public function getDataSet($preferTimeRes, $preferTime)
     {
         $timeres = $this->resolveTimeRes($preferTimeRes);
diff --git a/modules/statistics/lib/StatisticsController.php b/modules/statistics/lib/StatisticsController.php
index 2da8806a7..511030793 100644
--- a/modules/statistics/lib/StatisticsController.php
+++ b/modules/statistics/lib/StatisticsController.php
@@ -203,6 +203,14 @@ class StatisticsController
         return $t;
     }
 
+
+    /**
+     * @param \SimpleSAML\XHTML\Template $t
+     * @param string $type
+     * @param string|null $key
+     * @param string|null $value
+     * @return string|array
+     */
     private function getBaseURL($t, $type = 'get', $key = null, $value = null)
     {
         $vars = [
diff --git a/modules/statistics/www/showstats.php b/modules/statistics/www/showstats.php
index 7011126ea..544a9c4f7 100644
--- a/modules/statistics/www/showstats.php
+++ b/modules/statistics/www/showstats.php
@@ -151,6 +151,14 @@ $t->data['jquery'] = ['core' => false, 'ui' => true, 'css' => true];
 
 $t->show();
 
+
+/**
+ * @param \SimpleSAML\XHTML\Template $t
+ * @param string $type
+ * @param string|null $key
+ * @param string|null $value
+ * @return string|array
+ */
 function getBaseURL($t, $type = 'get', $key = null, $value = null)
 {
     $vars = [
-- 
GitLab