Skip to content

Commit

Permalink
Additional type declarations for data storage classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Jan 11, 2025
1 parent f18fabc commit 72004a9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
18 changes: 9 additions & 9 deletions includes/avatar-privacy/data-storage/class-filesystem-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __construct() {
*
* @return string
*/
public function get_base_dir() {
public function get_base_dir(): string {
if ( empty( $this->base_dir ) ) {
$this->base_dir = "{$this->get_upload_dir()['basedir']}/" . self::CACHE_DIR;

Expand All @@ -107,7 +107,7 @@ public function get_base_dir() {
*
* @return string
*/
public function get_base_url() {
public function get_base_url(): string {
if ( empty( $this->base_url ) ) {
$this->base_url = "{$this->get_upload_dir()['baseurl']}/" . self::CACHE_DIR;
}
Expand All @@ -124,7 +124,7 @@ public function get_base_url() {
*
* @phpstan-return UploadDir
*/
protected function get_upload_dir() {
protected function get_upload_dir(): array {
if ( ! isset( $this->upload_dir ) ) {
$multisite = \is_multisite();

Expand Down Expand Up @@ -154,7 +154,7 @@ protected function get_upload_dir() {
*
* @return bool True if the file was successfully stored in the cache, false otherwise.
*/
public function set( $filename, $data, $force = false ) {
public function set( string $filename, string $data, bool $force = false ): bool {
$file = $this->get_base_dir() . $filename;

if ( \file_exists( $file ) && ! $force ) {
Expand All @@ -178,7 +178,7 @@ public function set( $filename, $data, $force = false ) {
*
* @return string
*/
public function get_url( $filename ) {
public function get_url( string $filename ): string {
return $this->get_base_url() . $filename;
}

Expand All @@ -189,7 +189,7 @@ public function get_url( $filename ) {
*
* @return bool True if the file was successfully removed from the cache, false otherwise.
*/
public function delete( $filename ) {
public function delete( string $filename ): bool {
$file = $this->get_base_dir() . $filename;

return \wp_is_writable( $file ) && delete_file( $file );
Expand All @@ -203,7 +203,7 @@ public function delete( $filename ) {
*
* @return void
*/
public function invalidate( $subdir = '', $regex = '' ) {
public function invalidate( string $subdir = '', string $regex = '' ): void {
try {
$iterator = $this->get_recursive_file_iterator( $subdir, $regex );
} catch ( \UnexpectedValueException $e ) {
Expand Down Expand Up @@ -232,7 +232,7 @@ public function invalidate( $subdir = '', $regex = '' ) {
*
* @return void
*/
public function invalidate_files_older_than( $age, $subdir = '', $regex = '' ) {
public function invalidate_files_older_than( int $age, string $subdir = '', string $regex = '' ): void {
try {
$now = \time();
$iterator = $this->get_recursive_file_iterator( $subdir, $regex );
Expand All @@ -258,7 +258,7 @@ public function invalidate_files_older_than( $age, $subdir = '', $regex = '' ) {
*
* @return \OuterIterator<string,\SplFileInfo>
*/
protected function get_recursive_file_iterator( $subdir = '', $regex = '' ) {
protected function get_recursive_file_iterator( string $subdir = '', string $regex = '' ): \OuterIterator {
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator( "{$this->get_base_dir()}{$subdir}", \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS ),
\RecursiveIteratorIterator::CHILD_FIRST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* This file is part of Avatar Privacy.
*
* Copyright 2018-2019 Peter Putzer.
* Copyright 2018-2024 Peter Putzer.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -87,7 +87,7 @@ public function __construct() {
*
* @return string The option name without the prefix, or '' if an invalid name was given.
*/
public function remove_prefix( $name ) {
public function remove_prefix( string $name ): string {
$parts = \explode( self::PREFIX, $name, 2 );
if ( '' === $parts[0] ) {
return $parts[1];
Expand All @@ -106,7 +106,7 @@ public function remove_prefix( $name ) {
*
* @return bool True if the option can be safely set, false otherwise.
*/
public function lock( $option ) {
public function lock( string $option ): bool {
$now = \microtime( true );
$secret = \wp_hash( "{$option}|{$now}", 'nonce' );
$lock = "{$option}_lock";
Expand All @@ -126,7 +126,7 @@ public function lock( $option ) {
* it was not lockedor because unlock was successful),
* false otherwise.
*/
public function unlock( $option ) {
public function unlock( string $option ): bool {
$lock = "{$option}_lock";

return ! $this->get( $lock ) || $this->delete( $lock );
Expand Down
4 changes: 2 additions & 2 deletions includes/avatar-privacy/data-storage/class-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* This file is part of Avatar Privacy.
*
* Copyright 2018-2020 Peter Putzer.
* Copyright 2018-2024 Peter Putzer.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -62,7 +62,7 @@ public function __construct() {
*
* @return void
*/
public function reset_avatar_default() {
public function reset_avatar_default(): void {
switch ( $this->get( 'avatar_default', null, true ) ) {
case 'rings':
case 'comment':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* This file is part of Avatar Privacy.
*
* Copyright 2018-2022 Peter Putzer.
* Copyright 2018-2024 Peter Putzer.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -109,7 +109,7 @@ public function __construct( Network_Options $network_options ) {
*
* @return void
*/
public function setup( $previous_version ) {
public function setup( string $previous_version ): void {
parent::setup( $previous_version );

// The table is set up correctly, but maybe we need to migrate some data
Expand All @@ -126,7 +126,7 @@ public function setup( $previous_version ) {
*
* @return bool
*/
public function use_global_table() {
public function use_global_table(): bool {
$global_table = (bool) $this->network_options->get( Network_Options::USE_GLOBAL_TABLE, false );

/**
Expand Down Expand Up @@ -157,7 +157,7 @@ public function use_global_table() {
*
* @return string
*/
protected function get_table_definition( $table_name ) {
protected function get_table_definition( string $table_name ): string {
return "CREATE TABLE {$table_name} (
id mediumint(9) NOT NULL AUTO_INCREMENT,
email varchar(100) NOT NULL,
Expand All @@ -176,7 +176,7 @@ protected function get_table_definition( $table_name ) {
*
* @return void
*/
protected function maybe_prepare_migration_queue() {
protected function maybe_prepare_migration_queue(): void {
$queue = $this->network_options->get( Network_Options::START_GLOBAL_TABLE_MIGRATION );

if ( \is_array( $queue ) && $this->network_options->lock( Network_Options::GLOBAL_TABLE_MIGRATION ) ) {
Expand All @@ -202,7 +202,7 @@ protected function maybe_prepare_migration_queue() {
*
* @return void
*/
protected function maybe_migrate_from_global_table() {
protected function maybe_migrate_from_global_table(): void {
if (
// The plugin is not network-activated (or not on a multisite installation).
! \is_plugin_active_for_network( \plugin_basename( \AVATAR_PRIVACY_PLUGIN_FILE ) ) ||
Expand Down Expand Up @@ -250,7 +250,7 @@ protected function maybe_migrate_from_global_table() {
*
* @return int|false The number of migrated rows or false on error.
*/
public function migrate_from_global_table( $site_id ) {
public function migrate_from_global_table( ?int $site_id ) {
global $wpdb;

// Get table names.
Expand Down Expand Up @@ -336,7 +336,7 @@ public function migrate_from_global_table( $site_id ) {
*
* @return string|false The prepared query, or false.
*/
protected function prepare_email_query( array $emails, $table ) {
protected function prepare_email_query( array $emails, string $table ) {
global $wpdb;

if ( empty( $emails ) || empty( $table ) ) {
Expand Down Expand Up @@ -383,7 +383,7 @@ protected function prepare_delete_query( array $ids_to_delete, $table ) {
*
* @return bool True if the schema was modified, false otherwise.
*/
public function maybe_upgrade_schema( $previous_version ) {
public function maybe_upgrade_schema( string $previous_version ): bool {
$result = false;

if ( \version_compare( $previous_version, '2.4.0', '<' ) ) {
Expand All @@ -401,7 +401,7 @@ public function maybe_upgrade_schema( $previous_version ) {
*
* @return bool
*/
protected function maybe_drop_hash_column() {
protected function maybe_drop_hash_column(): bool {
global $wpdb;

$table_name = $this->get_table_name();
Expand All @@ -422,7 +422,7 @@ protected function maybe_drop_hash_column() {
*
* @return bool
*/
protected function maybe_fix_last_updated_column_default() {
protected function maybe_fix_last_updated_column_default(): bool {
global $wpdb;

$table_name = $this->get_table_name();
Expand All @@ -449,7 +449,7 @@ protected function maybe_fix_last_updated_column_default() {
*
* @return int The number of upgraded rows.
*/
public function maybe_upgrade_data( $previous_version ) {
public function maybe_upgrade_data( string $previous_version ): int {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* This file is part of Avatar Privacy.
*
* Copyright 2020-2023 Peter Putzer.
* Copyright 2020-2024 Peter Putzer.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -89,7 +89,7 @@ public function __construct() {
*
* @return bool
*/
public function use_global_table() {
public function use_global_table(): bool {
return false;
}

Expand All @@ -108,7 +108,7 @@ public function use_global_table() {
*
* @return string
*/
protected function get_table_definition( $table_name ) {
protected function get_table_definition( string $table_name ): string {
$identifier_length = $this->database_supports_large_index() ? 256 : 175;

return "CREATE TABLE {$table_name} (
Expand Down Expand Up @@ -143,7 +143,7 @@ protected function database_supports_large_index(): bool {
*
* @return bool True if the schema was modified, false otherwise.
*/
public function maybe_upgrade_schema( $previous_version ) {
public function maybe_upgrade_schema( string $previous_version ): bool {
return false;
}

Expand All @@ -156,7 +156,7 @@ public function maybe_upgrade_schema( $previous_version ) {
*
* @return int The number of upgraded rows.
*/
public function maybe_upgrade_data( $previous_version ) {
public function maybe_upgrade_data( string $previous_version ): int {
return 0;
}
}
Loading

0 comments on commit 72004a9

Please sign in to comment.