Skip to content

Commit

Permalink
Refactor lib/private
Browse files Browse the repository at this point in the history
Signed-off-by: Hamid Dehnavi <hamid.dev.pro@gmail.com>
  • Loading branch information
shdehnavi committed Jul 8, 2023
1 parent 9bf6911 commit f096b81
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 110 deletions.
68 changes: 21 additions & 47 deletions lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,40 +63,18 @@
use Psr\Log\LoggerInterface;

class Setup {
/** @var SystemConfig */
protected $config;
/** @var IniGetWrapper */
protected $iniWrapper;
/** @var IL10N */
protected $l10n;
/** @var Defaults */
protected $defaults;
/** @var LoggerInterface */
protected $logger;
/** @var ISecureRandom */
protected $random;
/** @var Installer */
protected $installer;

public function __construct(
SystemConfig $config,
IniGetWrapper $iniWrapper,
IL10N $l10n,
Defaults $defaults,
LoggerInterface $logger,
ISecureRandom $random,
Installer $installer
protected SystemConfig $config,
protected IniGetWrapper $iniWrapper,
protected IL10N $l10n,
protected Defaults $defaults,
protected LoggerInterface $logger,
protected ISecureRandom $random,
protected Installer $installer,
) {
$this->config = $config;
$this->iniWrapper = $iniWrapper;
$this->l10n = $l10n;
$this->defaults = $defaults;
$this->logger = $logger;
$this->random = $random;
$this->installer = $installer;
}

protected static $dbSetupClasses = [
protected static array $dbSetupClasses = [
'mysql' => \OC\Setup\MySQL::class,
'pgsql' => \OC\Setup\PostgreSQL::class,
'oci' => \OC\Setup\OCI::class,
Expand All @@ -110,7 +88,7 @@ public function __construct(
* @param string $name
* @return bool
*/
protected function class_exists($name) {
protected function class_exists(string $name): bool {
return class_exists($name);
}

Expand All @@ -120,7 +98,7 @@ protected function class_exists($name) {
* @param string $name
* @return bool
*/
protected function is_callable($name) {
protected function is_callable(string $name): bool {
return is_callable($name);
}

Expand All @@ -129,7 +107,7 @@ protected function is_callable($name) {
*
* @return array
*/
protected function getAvailableDbDriversForPdo() {
protected function getAvailableDbDriversForPdo(): array {
if (class_exists(\PDO::class)) {
return \PDO::getAvailableDrivers();
}
Expand All @@ -143,7 +121,7 @@ protected function getAvailableDbDriversForPdo() {
* @return array
* @throws Exception
*/
public function getSupportedDatabases($allowAllDatabases = false) {
public function getSupportedDatabases(bool $allowAllDatabases = false): array {
$availableDatabases = [
'sqlite' => [
'type' => 'pdo',
Expand Down Expand Up @@ -202,10 +180,10 @@ public function getSupportedDatabases($allowAllDatabases = false) {
* Gathers system information like database type and does
* a few system checks.
*
* @param bool $allowAllDatabases
* @return array of system info, including an "errors" value
* in case of errors/warnings
*/
public function getSystemInfo($allowAllDatabases = false) {
public function getSystemInfo(bool $allowAllDatabases = false): array {
$databases = $this->getSupportedDatabases($allowAllDatabases);

$dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data');
Expand Down Expand Up @@ -270,11 +248,7 @@ public function getSystemInfo($allowAllDatabases = false) {
];
}

/**
* @param $options
* @return array
*/
public function install($options) {
public function install($options): array {
$l = $this->l10n;

$error = [];
Expand Down Expand Up @@ -448,7 +422,7 @@ public function install($options) {
return $error;
}

public static function installBackgroundJobs() {
public static function installBackgroundJobs(): void {
$jobList = \OC::$server->getJobList();
$jobList->add(TokenCleanupJob::class);
$jobList->add(Rotate::class);
Expand All @@ -458,7 +432,7 @@ public static function installBackgroundJobs() {
/**
* @return string Absolute path to htaccess
*/
private function pathToHtaccess() {
private function pathToHtaccess(): string {
return \OC::$SERVERROOT . '/.htaccess';
}

Expand Down Expand Up @@ -493,7 +467,7 @@ private static function findWebRoot(SystemConfig $config): string {
* @return bool True when success, False otherwise
* @throws \OCP\AppFramework\QueryException
*/
public static function updateHtaccess() {
public static function updateHtaccess(): bool {
$config = \OC::$server->getSystemConfig();

try {
Expand Down Expand Up @@ -557,7 +531,7 @@ public static function updateHtaccess() {
return false;
}

public static function protectDataDirectory() {
public static function protectDataDirectory(): void {
//Require all denied
$now = date('Y-m-d H:i:s');
$content = "# Generated by Nextcloud on $now\n";
Expand Down Expand Up @@ -604,14 +578,14 @@ private function getVendorData(): array {
/**
* @return bool
*/
public function shouldRemoveCanInstallFile() {
public function shouldRemoveCanInstallFile(): bool {
return \OC_Util::getChannel() !== 'git' && is_file(\OC::$configDir.'/CAN_INSTALL');
}

/**
* @return bool
*/
public function canInstallFileExists() {
public function canInstallFileExists(): bool {
return is_file(\OC::$configDir.'/CAN_INSTALL');
}
}
23 changes: 6 additions & 17 deletions lib/private/StreamImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,13 @@
* valid result.
*/
class StreamImage implements IStreamImage {
/** @var resource The internal stream */
private $stream;

/** @var null|string */
private $mimeType;

/** @var int */
private $width;

/** @var int */
private $height;

/** @param resource $stream */
public function __construct($stream, string $mimeType, int $width, int $height) {
$this->stream = $stream;
$this->mimeType = $mimeType;
$this->width = $width;
$this->height = $height;
public function __construct(
private $stream,
private ?string $mimeType,
private int $width,
private int $height,
) {
}

/** @inheritDoc */
Expand Down
9 changes: 5 additions & 4 deletions lib/private/Streamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Streamer {
private array $preferTarFor = [ '/macintosh|mac os x/i' ];

// streamer instance
private $streamerInstance;
private ZipStreamer|TarStreamer $streamerInstance;

/**
* Streamer constructor.
Expand Down Expand Up @@ -91,9 +91,10 @@ public function __construct(IRequest $request, int|float $size, int $numberOfFil

/**
* Send HTTP headers
*
* @param string $name
*/
public function sendHeaders($name) {
public function sendHeaders(string $name): void {
header('X-Accel-Buffering: no');
$extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar';
$fullName = $name . $extension;
Expand Down Expand Up @@ -174,7 +175,7 @@ public function addFileFromStream($stream, string $internalName, int|float $size
* @param string $dirName Directory Path and name to be added to the archive.
* @return bool $success

Check failure on line 176 in lib/private/Streamer.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidNullableReturnType

lib/private/Streamer.php:176:13: InvalidNullableReturnType: The declared return type 'bool' for OC\Streamer::addEmptyDir is not nullable, but 'bool|null' contains null (see https://psalm.dev/144)
*/
public function addEmptyDir($dirName) {
public function addEmptyDir(string $dirName): bool {
return $this->streamerInstance->addEmptyDir($dirName);

Check failure on line 179 in lib/private/Streamer.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

NullableReturnStatement

lib/private/Streamer.php:179:10: NullableReturnStatement: The declared return type 'bool' for OC\Streamer::addEmptyDir is not nullable, but the function returns 'bool|null' (see https://psalm.dev/139)
}

Expand All @@ -184,7 +185,7 @@ public function addEmptyDir($dirName) {
* closing, the file is completely written to the output stream.
* @return bool $success
*/
public function finalize() {
public function finalize(): bool {
return $this->streamerInstance->finalize();
}
}
38 changes: 9 additions & 29 deletions lib/private/SubAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,12 @@
use OCP\IUserManager;

class SubAdmin extends PublicEmitter implements ISubAdmin {
/** @var IUserManager */
private $userManager;

/** @var IGroupManager */
private $groupManager;

/** @var IDBConnection */
private $dbConn;

/** @var IEventDispatcher */
private $eventDispatcher;

/**
* @param IUserManager $userManager
* @param IGroupManager $groupManager
* @param IDBConnection $dbConn
*/
public function __construct(IUserManager $userManager,
IGroupManager $groupManager,
IDBConnection $dbConn,
IEventDispatcher $eventDispatcher) {
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->dbConn = $dbConn;
$this->eventDispatcher = $eventDispatcher;

public function __construct(
private IUserManager $userManager,
private IGroupManager $groupManager,
private IDBConnection $dbConn,
private IEventDispatcher $eventDispatcher,
) {
$this->userManager->listen('\OC\User', 'postDelete', function ($user) {
$this->post_deleteUser($user);
});
Expand Down Expand Up @@ -240,7 +220,7 @@ public function isSubAdminOfGroup(IUser $user, IGroup $group): bool {

$fetch = $result->fetch();
$result->closeCursor();
$result = !empty($fetch) ? true : false;
$result = !empty($fetch);

return $result;
}
Expand Down Expand Up @@ -294,7 +274,7 @@ public function isUserAccessible(IUser $subadmin, IUser $user): bool {
* delete all SubAdmins by $user
* @param IUser $user
*/
private function post_deleteUser(IUser $user) {
private function post_deleteUser(IUser $user): void {
$qb = $this->dbConn->getQueryBuilder();

$qb->delete('group_admin')
Expand All @@ -306,7 +286,7 @@ private function post_deleteUser(IUser $user) {
* delete all SubAdmins by $group
* @param IGroup $group
*/
private function post_deleteGroup(IGroup $group) {
private function post_deleteGroup(IGroup $group): void {
$qb = $this->dbConn->getQueryBuilder();

$qb->delete('group_admin')
Expand Down
24 changes: 11 additions & 13 deletions lib/private/SystemConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
class SystemConfig {
/** @var array */
protected $sensitiveValues = [
protected array $sensitiveValues = [
'instanceid' => true,
'datadirectory' => true,
'dbname' => true,
Expand Down Expand Up @@ -120,18 +120,16 @@ class SystemConfig {
],
];

/** @var Config */
private $config;

public function __construct(Config $config) {
$this->config = $config;
public function __construct(
private Config $config,
) {
}

/**
* Lists all available config keys
* @return array an array of key names
*/
public function getKeys() {
public function getKeys(): array {
return $this->config->getKeys();
}

Expand All @@ -141,7 +139,7 @@ public function getKeys() {
* @param string $key the key of the value, under which will be saved
* @param mixed $value the value that should be stored
*/
public function setValue($key, $value) {
public function setValue(string $key, mixed $value): void {
$this->config->setValue($key, $value);
}

Expand All @@ -151,7 +149,7 @@ public function setValue($key, $value) {
* @param array $configs Associative array with `key => value` pairs
* If value is null, the config key will be deleted
*/
public function setValues(array $configs) {
public function setValues(array $configs): void {
$this->config->setValues($configs);
}

Expand All @@ -162,7 +160,7 @@ public function setValues(array $configs) {
* @param mixed $default the default value to be returned if the value isn't set
* @return mixed the value or $default
*/
public function getValue($key, $default = '') {
public function getValue(string $key, mixed $default = ''): mixed {
return $this->config->getValue($key, $default);
}

Expand All @@ -173,7 +171,7 @@ public function getValue($key, $default = '') {
* @param mixed $default the default value to be returned if the value isn't set
* @return mixed the value or $default
*/
public function getFilteredValue($key, $default = '') {
public function getFilteredValue(string $key, mixed $default = ''): mixed {
$value = $this->getValue($key, $default);

if (isset($this->sensitiveValues[$key])) {
Expand All @@ -188,7 +186,7 @@ public function getFilteredValue($key, $default = '') {
*
* @param string $key the key of the value, under which it was saved
*/
public function deleteValue($key) {
public function deleteValue(string $key): void {
$this->config->deleteKey($key);
}

Expand All @@ -197,7 +195,7 @@ public function deleteValue($key) {
* @param mixed $value
* @return mixed
*/
protected function removeSensitiveValue($keysToRemove, $value) {
protected function removeSensitiveValue(bool|array $keysToRemove, mixed $value): mixed {
if ($keysToRemove === true) {
return IConfig::SENSITIVE_VALUE;
}
Expand Down

0 comments on commit f096b81

Please sign in to comment.