Skip to content

Commit

Permalink
clean up more code & bring code to phpstan lvl 5
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Feb 25, 2025
1 parent 5aa5f27 commit 5c1dca3
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 82 deletions.
2 changes: 1 addition & 1 deletion core/Menu/MenuAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function getAllMenus()
* Adds a new entry to the menu.
*
* @param string $menuName The menu's category name. Can be a translation token.
* @param string $subMenuName The menu item's name. Can be a translation token.
* @param null|string $subMenuName The menu item's name. Can be a translation token.
* @param string|array $url The URL the admin menu entry should link to, or an array of query parameters
* that can be used to build the URL.
* @param int $order The order hint.
Expand Down
1 change: 1 addition & 0 deletions core/Plugin/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abstract class API
{
private static $instances;

/** @var bool */
protected $autoSanitizeInputParams = true;

/**
Expand Down
24 changes: 12 additions & 12 deletions plugins/MultiSites/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class API extends \Piwik\Plugin\API
public const ECOMMERCE_ORDERS_METRIC = 'orders';
public const ECOMMERCE_REVENUE_METRIC = 'ecommerce_revenue';

/** @var array<string,array<string,string>> */
private static $baseMetrics = [
self::NB_VISITS_METRIC => [
self::METRIC_TRANSLATION_KEY => 'General_ColumnNbVisits',
Expand Down Expand Up @@ -89,8 +90,8 @@ class API extends \Piwik\Plugin\API
* Only used when a scheduled task is running
* @param bool $enhanced When true, return additional goal & ecommerce metrics
* @param null|string $pattern If specified, only the website which names (or site ID) match the pattern will be returned using SitesManager.getPatternMatchSites
* @param string|array $showColumns If specified, only the requested columns will be fetched
* @return DataTable
* @param string|array<string> $showColumns If specified, only the requested columns will be fetched
* @return DataTableInterface
*/
public function getAll(
string $period,
Expand All @@ -116,7 +117,7 @@ public function getAll(
* });
* });
*
* @param array &$idSites List of idSites that the current user would be allowed to see in all websites dashboard.
* @param array<int> &$idSites List of idSites that the current user would be allowed to see in all websites dashboard.
*/
Piwik::postEvent('MultiSites.filterSites', [&$idSites]);

Expand Down Expand Up @@ -149,7 +150,7 @@ public function getAll(
*
* @param ?string $pattern
* @param ?string $_restrictSitesToLogin
* @return array
* @return array<int>
*/
private function getSitesIdFromPattern(?string $pattern, ?string $_restrictSitesToLogin): array
{
Expand Down Expand Up @@ -202,7 +203,7 @@ private function getSitesIdFromPattern(?string $pattern, ?string $_restrictSites
* @param null|string $_restrictSitesToLogin Hack used to enforce we restrict the returned data to the specified username
* Only used when a scheduled task is running
* @param bool $enhanced When true, return additional goal & ecommerce metrics
* @return DataTable
* @return DataTableInterface
*/
public function getOne(
int $idSite,
Expand Down Expand Up @@ -238,7 +239,7 @@ public function getOne(
* @param null|string $segment
* @param string $pattern
* @param int $filter_limit
* @return array
* @return array[]
* @throws Exception
*/
public function getAllWithGroups(
Expand Down Expand Up @@ -420,8 +421,7 @@ function ($value) {
*
* @param DataTable|DataTable\Map $currentData
* @param DataTable|DataTable\Map $pastData
* @param array $apiMetrics The array of string fields to calculate evolution
* metrics for.
* @param array<string,array<string,string>> $apiMetrics The array of string fields to calculate evolution metrics for.
* @throws Exception
*/
private function calculateEvolutionPercentages(
Expand Down Expand Up @@ -543,7 +543,7 @@ private function preformatApiMetricsForTotalsCalculation(array $apiMetrics): arr
* $this->buildDataTable.
*
* @param DataTableInterface $dataTable
* @param array $apiMetrics Metrics info.
* @param array<string,string> $apiMetrics Metrics info.
*/
private function setMetricsTotalsMetadata(DataTableInterface $dataTable, array $apiMetrics): void
{
Expand Down Expand Up @@ -571,8 +571,8 @@ private function setMetricsTotalsMetadata(DataTableInterface $dataTable, array $
* Sets the previous total visits, actions & revenue for a DataTable returned by
* $this->buildDataTable.
*
* @param DataTableInterface $dataTable
* @param DataTableInterface $pastData
* @param DataTable|DataTable\Map $dataTable
* @param DataTable|DataTable\Map $pastData
* @param array $apiMetrics Metrics info.
*/
private function setPreviousMetricsTotalsMetadata(
Expand Down Expand Up @@ -618,7 +618,7 @@ private function setPreviousMetricsTotalsMetadata(

/**
* @param Row[] $rows
* @return array
* @return Row[]
*/
private function filterRowsForTotalsCalculation(array $rows): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
*/
class EcommerceOnlyEvolutionMetric extends EvolutionMetric
{
/** @var bool */
private $isRevenueEvolution;

public function __construct(
$wrapped,
DataTable $pastData,
$evolutionMetricName = false,
$quotientPrecision = 0,
int $quotientPrecision = 0,
?DataTable $currentData = null
) {
parent::__construct($wrapped, $pastData, $evolutionMetricName, $quotientPrecision, $currentData);

$this->isRevenueEvolution = $this->getName() == 'revenue_evolution';
$this->isRevenueEvolution = $this->getName() === 'revenue_evolution';
}

public function compute(Row $row)
Expand Down
12 changes: 4 additions & 8 deletions plugins/MultiSites/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

class Controller extends \Piwik\Plugin\Controller
{
/**
* @var Translator
*/
/** @var Translator */
private $translator;

public function __construct(Translator $translator)
Expand All @@ -46,7 +44,7 @@ public function standalone(): string
/**
* @throws \Piwik\NoAccessException
*/
public function getSitesInfo($isWidgetized = false): string
protected function getSitesInfo(bool $isWidgetized = false): string
{
Piwik::checkUserHasSomeViewAccess();

Expand Down Expand Up @@ -80,11 +78,9 @@ public function getSitesInfo($isWidgetized = false): string
return $view->render();
}

public function getEvolutionGraph($columns = null)
public function getEvolutionGraph(): string
{
if (empty($columns)) {
$columns = Request::fromRequest()->getStringParameter('columns');
}
$columns = Request::fromRequest()->getStringParameter('columns');
$api = "API.get";

if ($columns == 'revenue') {
Expand Down
18 changes: 8 additions & 10 deletions plugins/MultiSites/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ class Dashboard
/** @var DataTable */
private $sitesByGroup;

/**
* @var int
*/
/** @var int */
private $numSites = 0;

/**
* Array of metrics that will be displayed and will be number formatted
* @var array
* @var array<string>
*/
private $displayedMetricColumns = [
'nb_visits', 'nb_pageviews', 'hits', 'nb_actions', 'revenue',
Expand Down Expand Up @@ -63,7 +61,7 @@ public function __construct(string $period, string $date, ?string $segment)

$sites->deleteRow(DataTable::ID_SUMMARY_ROW);

/** @var DataTable $pastData */
/** @var null|DataTable $pastData */
$pastData = $sites->getMetadata('pastData');

$sites->filter(function (DataTable $table) use ($pastData) {
Expand Down Expand Up @@ -99,10 +97,10 @@ public function setSitesTable(DataTable $sites): void
$this->rememberNumberOfSites();
}

public function getSites(array $request, int $limit)
public function getSites(array $request, int $limit): array
{
$request['filter_limit'] = $limit;
$request['filter_offset'] = isset($request['filter_offset']) ? $request['filter_offset'] : 0;
$request['filter_offset'] = isset($request['filter_offset']) ? (int) $request['filter_offset'] : 0;

$this->makeSitesFlatAndApplyGenericFilters($this->sitesByGroup, $request);
$sites = $this->convertDataTableToArrayAndApplyQueuedFilters($this->sitesByGroup, $request);
Expand Down Expand Up @@ -205,7 +203,7 @@ public function getLastDate(): string
return $lastPeriod;
}

private function convertDataTableToArrayAndApplyQueuedFilters(DataTable $table, array $request)
private function convertDataTableToArrayAndApplyQueuedFilters(DataTable $table, array $request): array
{
$request['serialize'] = 0;
$request['expanded'] = 0;
Expand All @@ -214,7 +212,7 @@ private function convertDataTableToArrayAndApplyQueuedFilters(DataTable $table,
$request['disable_generic_filters'] = 1;

$responseBuilder = new ResponseBuilder('json', $request);
return json_decode($responseBuilder->getResponse($table, 'MultiSites', 'getAll'), true);
return json_decode($responseBuilder->getResponse($table, 'MultiSites', 'getAll'), true) ?: [];
}

private function moveSitesHavingAGroupIntoSubtables(DataTable $sites): DataTable
Expand Down Expand Up @@ -318,8 +316,8 @@ private function makeSitesFlatAndApplyGenericFilters(DataTable $table, array $re
$table->disableFilter('Limit');

// this will apply the sort filter
/** @var DataTable $table */
$genericFilter = new DataTablePostProcessor('MultiSites', 'getAll', $request);
/** @var DataTable */
$table = $genericFilter->applyGenericFilters($table);

// make sure from now on the sites will be no longer sorted, they were already sorted
Expand Down
14 changes: 7 additions & 7 deletions plugins/MultiSites/DataTable/Filter/NestedSitesLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@
*/
class NestedSitesLimiter extends BaseFilter
{
/** @var int */
private $offset = 0;
/** @var int */
private $limit = 0;
/**
* @var Row[]
*/
/** @var Row[] */
private $rows = [];

/**
* Constructor.
*
* @param DataTable $table The table to eventually filter.
*/
public function __construct($table, $offset, $limit)
public function __construct(DataTable $table, int $offset, int $limit)
{
parent::__construct($table);
$this->offset = $offset;
Expand Down Expand Up @@ -112,7 +112,7 @@ private function hasRows(): bool
return count($this->rows) !== 0;
}

private function addRowIfNeeded(Row $row, $numRows): void
private function addRowIfNeeded(Row $row, int $numRows): void
{
$inOffset = $numRows >= $this->offset;

Expand All @@ -122,9 +122,9 @@ private function addRowIfNeeded(Row $row, $numRows): void
}

/**
* @param Row $lastGroupFromPreviousPage
* @param null|Row $lastGroupFromPreviousPage
*/
private function prependGroupIfFirstSiteBelongsToAGroupButGroupIsMissingInRows($lastGroupFromPreviousPage): void
private function prependGroupIfFirstSiteBelongsToAGroupButGroupIsMissingInRows(?Row $lastGroupFromPreviousPage): void
{
if ($lastGroupFromPreviousPage && !empty($this->rows)) {
// the result starts with a row that does belong to a group, we make sure to show this group before that site
Expand Down
1 change: 0 additions & 1 deletion plugins/MultiSites/tests/Fixtures/ManySitesWithVisits.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ private function setUpGoals(): void

private function setUpWebsites(): void
{

$this->idSiteEcommerce = self::createWebsite($this->dateTime, 1, 'Site Ecommerce');
$this->idSiteGoalDefaultValue = self::createWebsite($this->dateTime, 0, 'Site Goal Default Value');
$this->idSiteGoalEventValue = self::createWebsite($this->dateTime, 0, 'Site Goal Event Value');
Expand Down
Loading

0 comments on commit 5c1dca3

Please sign in to comment.