Skip to content

Commit

Permalink
Add more type hints and strict typing
Browse files Browse the repository at this point in the history
  • Loading branch information
jdarwood007 committed Nov 29, 2023
1 parent 1cbdf2e commit cb91bf0
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 57 deletions.
1 change: 1 addition & 0 deletions Sources/Actions/Admin/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use SMF\User;
use SMF\Utils;
use SMF\WebFetch\WebFetchApi;
use SMF\PackageManager\XmlArray;

/**
* Provides the search functionality inside the admin control panel.
Expand Down
4 changes: 2 additions & 2 deletions Sources/BBCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1143,15 +1143,15 @@ function ($a, $b) {
*
* @param string|bool $message The string to parse.
* @param bool $smileys Whether to parse smileys. Default: true.
* @param string $cache_id The cache ID.
* @param string|int $cache_id The cache ID.
* If $cache_id is left empty, an ID will be generated automatically.
* Manually specifiying a ID is helpful in cases when an integration hook
* wants to identify particular strings to act upon, but is otherwise
* unnecessary.
* @param array $parse_tags If set, only parses these tags rather than all of them.
* @return string The parsed string.
*/
public function parse(string $message, bool $smileys = true, string $cache_id = '', array $parse_tags = []): string
public function parse(string $message, bool $smileys = true, string|int $cache_id = '', array $parse_tags = []): string
{
// Don't waste cycles
if (strval($message) === '') {
Expand Down
17 changes: 10 additions & 7 deletions Sources/Search/APIs/Custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

namespace SMF\Search\APIs;

use SMF\Config;
use SMF\Db\DatabaseApi as Db;
use SMF\Search\SearchApi;
use SMF\Utils;
use SMF\Search\SearchApiInterface;

/**
* Used for the "custom search index" option
* Class Custom
*/
class Custom extends SearchApi
class Custom extends SearchApi implements SearchApiInterface
{
/**
* @var array Index settings
Expand Down Expand Up @@ -71,7 +74,7 @@ public function __construct()
/**
* {@inheritDoc}
*/
public function supportsMethod($methodName, $query_params = null): bool
public function supportsMethod(string $methodName, array $query_params = []): bool
{
$return = false;

Expand Down Expand Up @@ -109,7 +112,7 @@ public function isValid(): bool
/**
* {@inheritDoc}
*/
public function searchSort($a, $b): int
public function searchSort(string $a, string $b): int
{
global $excludedWords;

Expand All @@ -122,7 +125,7 @@ public function searchSort($a, $b): int
/**
* {@inheritDoc}
*/
public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude, $isExcluded): void
public function prepareIndexes(string $word, array &$wordsSearch, array &$wordsExclude, bool $isExcluded): void
{
$subwords = Utils::text2words($word, $this->min_word_length, true);

Expand All @@ -136,7 +139,7 @@ public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude,
}

foreach ($subwords as $subword) {
if (Utils::entityStrlen($subword) >= $this->min_word_length && !in_array($subword, $this->bannedWords)) {
if (Utils::entityStrlen((string) $subword) >= $this->min_word_length && !in_array($subword, $this->bannedWords)) {
$wordsSearch['indexed_words'][] = $subword;

if ($isExcluded) {
Expand All @@ -149,7 +152,7 @@ public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude,
/**
* {@inheritDoc}
*/
public function indexedWordQuery(array $words, array $search_data)
public function indexedWordQuery(array $words, array $search_data): mixed
{
$query_select = [
'id_msg' => 'm.id_msg',
Expand Down Expand Up @@ -202,7 +205,7 @@ public function indexedWordQuery(array $words, array $search_data)
$count = 0;

if (!empty($query_params['excluded_phrases']) && empty(Config::$modSettings['search_force_index'])) {
foreach ($query_params['excluded_phrases'] as $phrase) {
foreach ($query_params['excluded_phrases'] as $excludedWord) {
$query_where[] = 'subject NOT ' . $this->query_match_type . ' {string:exclude_subject_words_' . $count . '}';

if ($this->query_match_type === 'RLIKE') {
Expand Down
17 changes: 10 additions & 7 deletions Sources/Search/APIs/Fulltext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

namespace SMF\Search\APIs;

use SMF\Config;
use SMF\Db\DatabaseApi as Db;
use SMF\Search\SearchApi;
use SMF\Utils;
use SMF\Search\SearchApiInterface;

/**
* Class Fulltext
* Used for fulltext index searching
*/
class Fulltext extends SearchApi
class Fulltext extends SearchApi implements SearchApiInterface
{
/**
* @var array Which words are banned
Expand Down Expand Up @@ -60,7 +63,7 @@ public function __construct()
/**
* {@inheritDoc}
*/
public function supportsMethod($methodName, $query_params = null): bool
public function supportsMethod(string $methodName, array $query_params = []): bool
{
$return = false;

Expand Down Expand Up @@ -92,7 +95,7 @@ public function supportsMethod($methodName, $query_params = null): bool
*
* @return int The minimum word length
*/
protected function _getMinWordLength()
protected function _getMinWordLength(): int
{
if (Config::$db_type == 'postgresql') {
return 0;
Expand All @@ -118,13 +121,13 @@ protected function _getMinWordLength()
$min_word_length = 4;
}

return $min_word_length;
return (int) $min_word_length;
}

/**
* {@inheritDoc}
*/
public function searchSort($a, $b): int
public function searchSort(string $a, string $b): int
{
global $excludedWords;

Expand All @@ -137,7 +140,7 @@ public function searchSort($a, $b): int
/**
* {@inheritDoc}
*/
public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude, $isExcluded): void
public function prepareIndexes(string $word, array &$wordsSearch, array &$wordsExclude, bool $isExcluded): void
{
$subwords = Utils::text2words($word, PHP_INT_MAX, false);

Expand Down Expand Up @@ -168,7 +171,7 @@ public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude,
/**
* {@inheritDoc}
*/
public function indexedWordQuery(array $words, array $search_data)
public function indexedWordQuery(array $words, array $search_data): mixed
{
$query_select = [
'id_msg' => 'm.id_msg',
Expand Down
7 changes: 5 additions & 2 deletions Sources/Search/APIs/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

namespace SMF\Search\APIs;

use SMF\Search\SearchApi;
use SMF\Search\SearchApiInterface;

/**
* Standard non full index, non custom index search
*/
class Standard extends SearchApi
class Standard extends SearchApi implements SearchApiInterface
{
/**
* {@inheritDoc}
*/
public function supportsMethod($methodName, $query_params = null): bool
public function supportsMethod(string $methodName, array $query_params = []): bool
{
$return = false;

Expand Down
23 changes: 14 additions & 9 deletions Sources/Search/SearchApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

namespace SMF\Search;

use SMF\BackwardCompatibility;
Expand All @@ -22,6 +24,7 @@
use SMF\PackageManager\SubsPackage;
use SMF\User;
use SMF\Utils;
use SMF\Actions\Search;

/**
* Class SearchApi
Expand Down Expand Up @@ -399,7 +402,7 @@ public function __construct()
/**
* {@inheritDoc}
*/
public function supportsMethod($methodName, $query_params = []): bool
public function supportsMethod(string $methodName, array $query_params = []): bool
{
switch ($methodName) {
case 'postRemoved':
Expand All @@ -421,26 +424,28 @@ public function supportsMethod($methodName, $query_params = []): bool
*/
public function isValid(): bool
{
return false;
}

/**
* {@inheritDoc}
*/
public function searchSort($a, $b): int
public function searchSort(string $a, string $b): int
{
return 0;
}

/**
* {@inheritDoc}
*/
public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude, $isExcluded): void
public function prepareIndexes(string $word, array &$wordsSearch, array &$wordsExclude, bool $isExcluded): void
{
}

/**
* {@inheritDoc}
*/
public function indexedWordQuery(array $words, array $search_data)
public function indexedWordQuery(array $words, array $search_data): mixed
{
}

Expand All @@ -461,7 +466,7 @@ public function postModified(array &$msgOptions, array &$topicOptions, array &$p
/**
* {@inheritDoc}
*/
public function postRemoved($id_msg): void
public function postRemoved(int $id_msg): void
{
$result = Db::$db->query(
'',
Expand Down Expand Up @@ -521,7 +526,7 @@ public function topicsRemoved(array $topics): void
/**
* {@inheritDoc}
*/
public function topicsMoved(array $topics, $board_to): void
public function topicsMoved(array $topics, int $board_to): void
{
}

Expand All @@ -539,7 +544,7 @@ public function initializeSearch(): void
/**
* {@inheritDoc}
*/
public function searchQuery(array $query_params, array $searchWords, array $excludedIndexWords, array &$participants, array &$searchArray)
public function searchQuery(array $query_params, array $searchWords, array $excludedIndexWords, array &$participants, array &$searchArray): void
{
$update_cache = empty($_SESSION['search_cache']) || ($_SESSION['search_cache']['params'] != $this->compressParams());

Expand Down Expand Up @@ -1068,7 +1073,7 @@ protected function setSearchTerms(): void
// .. first, we check for things like -"some words", but not "-some words".
foreach ($matches[1] as $index => $word) {
if ($word === '-') {
if (($word = trim($phraseArray[$index], '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) {
if (($word = trim($phraseArray[$index], '-_\' ')) !== '' && !in_array($word, $this->blacklisted_words)) {
$this->excludedWords[] = $word;
}

Expand All @@ -1079,7 +1084,7 @@ protected function setSearchTerms(): void
// Now we look for -test, etc.... normaller.
foreach ($wordArray as $index => $word) {
if (strpos(trim($word), '-') === 0) {
if (($word = trim($word, '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) {
if (($word = trim($word, '-_\' ')) !== '' && !in_array($word, $this->blacklisted_words)) {
$this->excludedWords[] = $word;
}

Expand Down
16 changes: 9 additions & 7 deletions Sources/Search/SearchApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

namespace SMF\Search;

use SMF\Msg;
Expand All @@ -29,7 +31,7 @@ interface SearchApiInterface
* @param array $query_params Any parameters for the query
* @return bool Whether or not the specified method is supported
*/
public function supportsMethod($methodName, $query_params = []): bool;
public function supportsMethod(string $methodName, array $query_params = []): bool;

/**
* Whether this method is valid for implementation or not.
Expand All @@ -47,7 +49,7 @@ public function isValid(): bool;
* @param string $b Word B
* @return int An integer indicating how the words should be sorted
*/
public function searchSort($a, $b): int;
public function searchSort(string $a, string $b): int;

/**
* Callback while preparing indexes for searching.
Expand All @@ -57,7 +59,7 @@ public function searchSort($a, $b): int;
* @param array $wordsExclude Words to exclude
* @param bool $isExcluded Whether the specified word should be excluded
*/
public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude, $isExcluded): void;
public function prepareIndexes(string $word, array &$wordsSearch, array &$wordsExclude, bool $isExcluded): void;

/**
* Search for indexed words.
Expand All @@ -66,7 +68,7 @@ public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude,
* @param array $search_data An array of search data
* @return mixed
*/
public function indexedWordQuery(array $words, array $search_data);
public function indexedWordQuery(array $words, array $search_data): mixed;

/**
* Callback when a post is created.
Expand Down Expand Up @@ -95,7 +97,7 @@ public function postModified(array &$msgOptions, array &$topicOptions, array &$p
*
* @param int $id_msg The ID of the post that was removed
*/
public function postRemoved($id_msg): void;
public function postRemoved(int $id_msg): void;

/**
* Callback when a topic is removed.
Expand All @@ -110,7 +112,7 @@ public function topicsRemoved(array $topics): void;
* @param array $topics The ID(s) of the moved topic(s)
* @param int $board_to The board that the topics were moved to
*/
public function topicsMoved(array $topics, $board_to): void;
public function topicsMoved(array $topics, int $board_to): void;

/**
* Sets whatever properties are necessary in order to perform the search.
Expand All @@ -134,7 +136,7 @@ public function initializeSearch(): void;
* @param array $searchArray
* @return mixed
*/
public function searchQuery(array $query_params, array $searchWords, array $excludedIndexWords, array &$participants, array &$searchArray);
public function searchQuery(array $query_params, array $searchWords, array $excludedIndexWords, array &$participants, array &$searchArray): void;

/**
* Figures out which search result topics the user participated in.
Expand Down
4 changes: 3 additions & 1 deletion Sources/Search/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* @version 3.0 Alpha 1
*/

declare(strict_types=1);

namespace SMF\Search;

use SMF\BackwardCompatibility;
Expand Down Expand Up @@ -409,7 +411,7 @@ public function format(int $counter = 0, array $format_options = []): array
* @param array $query_customizations Customizations to the SQL query.
* @return Generator<array> Iterating over result gives SearchResult instances.
*/
public static function get($ids, array $query_customizations = [])
public static function get(/*int|array*/ $ids, array $query_customizations = [])/*: Generator*/
{
$selects = $query_customizations['selects'] ?? [
'm.*',
Expand Down
Loading

0 comments on commit cb91bf0

Please sign in to comment.