Skip to content

Commit

Permalink
refactor: Improves readability mainly by utilizing PHP8's constructor…
Browse files Browse the repository at this point in the history
… property promotion feature.

Signed-off-by: Faraz Samapoor <f.samapoor@gmail.com>
  • Loading branch information
fsamapoor committed May 1, 2024
1 parent d82fb01 commit a8d25b3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 61 deletions.
5 changes: 2 additions & 3 deletions apps/theming/lib/Migration/InitBackgroundImagesMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@
use OCP\Migration\IOutput;

class InitBackgroundImagesMigration implements \OCP\Migration\IRepairStep {

private IJobList $jobList;

public function __construct(IJobList $jobList) {
$this->jobList = $jobList;
}

public function getName() {
public function getName(): string {
return 'Initialize migration of background images from dashboard to theming app';
}

public function run(IOutput $output) {
public function run(IOutput $output): void {
$this->jobList->add(MigrateBackgroundImages::class, ['stage' => MigrateBackgroundImages::STAGE_PREPARE]);
}
}
6 changes: 4 additions & 2 deletions apps/theming/lib/Service/BackgroundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,13 @@ class BackgroundService {
private string $userId;
private ThemingDefaults $themingDefaults;

public function __construct(IRootFolder $rootFolder,
public function __construct(
IRootFolder $rootFolder,
IAppData $appData,
IConfig $config,
?string $userId,
ThemingDefaults $themingDefaults) {
ThemingDefaults $themingDefaults,
) {
if ($userId === null) {
return;
}
Expand Down
17 changes: 4 additions & 13 deletions apps/theming/lib/Service/JSDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,12 @@
use OCP\IConfig;

class JSDataService implements \JsonSerializable {
private ThemingDefaults $themingDefaults;
private Util $util;
private IConfig $appConfig;
private ThemesService $themesService;

public function __construct(
ThemingDefaults $themingDefaults,
Util $util,
IConfig $appConfig,
ThemesService $themesService
private ThemingDefaults $themingDefaults,
private Util $util,
private IConfig $appConfig,
private ThemesService $themesService,
) {
$this->themingDefaults = $themingDefaults;
$this->util = $util;
$this->appConfig = $appConfig;
$this->themesService = $themesService;
}

public function jsonSerialize(): array {
Expand Down
44 changes: 15 additions & 29 deletions apps/theming/lib/Service/ThemeInjectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,17 @@
use OCP\IUserSession;

class ThemeInjectionService {
private ?string $userId = null;

private IURLGenerator $urlGenerator;
private ThemesService $themesService;
private DefaultTheme $defaultTheme;
private Util $util;
private IConfig $config;
private ?string $userId;

public function __construct(IURLGenerator $urlGenerator,
ThemesService $themesService,
DefaultTheme $defaultTheme,
Util $util,
IConfig $config,
IUserSession $userSession) {
$this->urlGenerator = $urlGenerator;
$this->themesService = $themesService;
$this->defaultTheme = $defaultTheme;
$this->util = $util;
$this->config = $config;

if ($userSession->getUser() !== null) {
$this->userId = $userSession->getUser()->getUID();
} else {
$this->userId = null;
}
public function __construct(
private IURLGenerator $urlGenerator,
private ThemesService $themesService,
private DefaultTheme $defaultTheme,
private Util $util,
private IConfig $config,
IUserSession $userSession,
) {
$this->userId = $userSession->getUser()?->getUID();
}

public function injectHeaders(): void {
Expand All @@ -69,12 +55,12 @@ public function injectHeaders(): void {
$this->addThemeHeaders($defaultTheme);

// Themes applied by media queries
foreach($mediaThemes as $theme) {
foreach ($mediaThemes as $theme) {
$this->addThemeHeaders($theme, true, $theme->getMediaQuery());
}

// Themes
foreach($this->themesService->getThemes() as $theme) {
foreach ($this->themesService->getThemes() as $theme) {
// Ignore default theme as already processed first
if ($theme->getId() === $this->defaultTheme->getId()) {
continue;
Expand Down Expand Up @@ -116,9 +102,9 @@ private function addThemeMetaHeaders(array $themes): void {
$metaHeaders = [];

// Meta headers
foreach($this->themesService->getThemes() as $theme) {
foreach ($this->themesService->getThemes() as $theme) {
if (!empty($theme->getMeta())) {
foreach($theme->getMeta() as $meta) {
foreach ($theme->getMeta() as $meta) {
if (!isset($meta['name']) || !isset($meta['content'])) {
continue;
}
Expand All @@ -131,7 +117,7 @@ private function addThemeMetaHeaders(array $themes): void {
}
}

foreach($metaHeaders as $name => $content) {
foreach ($metaHeaders as $name => $content) {
\OCP\Util::addHeader('meta', [
'name' => $name,
'content' => join(' ', array_unique($content)),
Expand Down
22 changes: 8 additions & 14 deletions apps/theming/lib/Service/ThemesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,19 @@
use OCP\IUserSession;

class ThemesService {
private IUserSession $userSession;
private IConfig $config;

/** @var ITheme[] */
private array $themesProviders;

public function __construct(IUserSession $userSession,
IConfig $config,
public function __construct(
private IUserSession $userSession,
private IConfig $config,
DefaultTheme $defaultTheme,
LightTheme $lightTheme,
DarkTheme $darkTheme,
HighContrastTheme $highContrastTheme,
DarkHighContrastTheme $darkHighContrastTheme,
DyslexiaFont $dyslexiaFont) {
$this->userSession = $userSession;
$this->config = $config;

DyslexiaFont $dyslexiaFont,
) {
// Register themes
$this->themesProviders = [
$defaultTheme->getId() => $defaultTheme,
Expand Down Expand Up @@ -86,7 +82,7 @@ public function enableTheme(ITheme $theme): array {
return $themesIds;
}

/** @var ITheme[] */
/** @var ITheme[] $themeId */
$themes = array_filter(array_map(function ($themeId) {
return $this->getThemes()[$themeId];
}, $themesIds));
Expand All @@ -97,7 +93,7 @@ public function enableTheme(ITheme $theme): array {
});

// Retrieve IDs only
/** @var string[] */
/** @var string[] $filteredThemes */
$filteredThemesIds = array_map(function (ITheme $t) {

Check failure on line 97 in apps/theming/lib/Service/ThemesService.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

apps/theming/lib/Service/ThemesService.php:97:34: InvalidArgument: Parameter 1 of closure passed to function array_map expects OCA\Theming\ITheme, but string provided (see https://psalm.dev/004)
return $t->getId();
}, array_values($filteredThemes));
Expand All @@ -123,15 +119,13 @@ public function disableTheme(ITheme $theme): array {
$this->setEnabledThemes($enabledThemes);
return $enabledThemes;
}

return $themesIds;
}

/**
* Check whether a theme is enabled or not
* for the logged-in user
*
* @return bool
*/
public function isEnabled(ITheme $theme): bool {
$user = $this->userSession->getUser();
Expand Down

0 comments on commit a8d25b3

Please sign in to comment.