Adds some PHP 8 ready changes to compiler classes. Adds Server and Crypt classes.

This commit is contained in:
2023-01-01 04:11:34 +02:00
parent e614f2ec23
commit e771e7d243
71 changed files with 2697 additions and 872 deletions

View File

@@ -85,7 +85,7 @@ abstract class ArrayHelper
*
* @since 3.1.1
*/
public static function intersect($a_array, $b_array)
public static function intersect($a_array, $b_array): bool
{
// flip the second array
$b_array = array_flip($b_array);

View File

@@ -30,7 +30,7 @@ abstract class Helper
* @var string
* @since 3.0.11
*/
public static $option;
public static string $option;
/**
* The component params list cache
@@ -38,19 +38,18 @@ abstract class Helper
* @var Registry[]
* @since 3.0.11
*/
protected static $params = array();
protected static array $params = [];
/**
* Gets the parameter object for the component
*
* @param string $option The option for the component.
* @param string|null $option The option for the component.
*
* @return Registry A Registry object.
*
* @see Registry
* @since 3.0.11
*/
public static function getParams($option = null): Registry
public static function getParams(?string $option = null): Registry
{
// check that we have an option
if (empty($option))
@@ -73,10 +72,9 @@ abstract class Helper
* @param string|null $default The default return value if none is found
*
* @return string|null A component option
*
* @since 3.0.11
*/
public static function getOption($default = 'empty'): ?string
public static function getOption(string $default = 'empty'): ?string
{
if (empty(self::$option))
{
@@ -95,14 +93,13 @@ abstract class Helper
/**
* Gets the component code name
*
* @param string $option The option for the component.
* @param string|null $option The option for the component.
* @param string|null $default The default return value if none is found
*
* @return string|null A component code name
*
* @since 3.0.11
*/
public static function getCode($option = null, $default = null): ?string
public static function getCode(?string $option = null, ?string $default = null): ?string
{
// check that we have an option
if (empty($option))
@@ -128,7 +125,7 @@ abstract class Helper
*
* @since 3.0.11
*/
public static function get($option = null, $default = null): ?string
public static function get(string $option = null, string $default = null): ?string
{
// check that we have an option
// and get the code name from it
@@ -149,25 +146,43 @@ abstract class Helper
/**
* Check if the helper class of this component has a method
*
* @param String $method The method name to search for
* @param String $option The option for the component.
* @param string $method The method name to search for
* @param string|null $option The option for the component.
*
* @return bool true if method exist
*
* @since 3.0.11
*/
public static function methodExists($method, $option = null)
public static function methodExists(string $method, string $option = null): bool
{
// get the helper class
if (($helper = self::get($option, false)) !== false)
return ($helper = self::get($option, false)) !== false &&
method_exists($helper, $method);
}
/**
* Check if the helper class of this component has a method, and call it with the arguments
*
* @param string $method The method name to search for
* @param array $arguments The arguments for function.
* @param string|null $option The option for the component.
*
* @return mixed return whatever the method returns or null
* @since 3.2.0
*/
public static function _(string $method, array $arguments = [], ?string $option = null)
{
// get the helper class
if (($helper = self::get($option, false)) !== false &&
method_exists($helper, $method))
{
if (method_exists($helper, $method))
{
return true;
}
// we know this is bad...
// so we need to move these
// functions to their own classes
return call_user_func_array([$helper, $method], $arguments);
}
return false;
return null;
}
}

View File

@@ -39,12 +39,12 @@ abstract class GuidHelper
*
* @since 3.0.9
*/
public static function get($trim = true)
public static function get(bool $trim = true): string
{
// Windows
if (function_exists('com_create_guid') === true)
if (function_exists('com_create_guid'))
{
if ($trim === true)
if ($trim)
{
return trim(com_create_guid(), '{}');
}
@@ -56,7 +56,7 @@ abstract class GuidHelper
$rbrace = $trim ? "" : chr(125); // "}"
// OSX/Linux
if (function_exists('openssl_random_pseudo_bytes') === true)
if (function_exists('openssl_random_pseudo_bytes'))
{
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr( ord($data[6]) & 0x0f | 0x40); // set version to 0100
@@ -82,7 +82,7 @@ abstract class GuidHelper
* Validate the Globally Unique Identifier ( and check if table already has this identifier)
*
* @param string $guid
* @param string $table
* @param string|null $table
* @param int $id
* @param string|null $component
*
@@ -90,7 +90,7 @@ abstract class GuidHelper
*
* @since 3.0.9
*/
public static function valid($guid, $table = null, $id = 0, $component = null)
public static function valid($guid, ?string $table = null, int $id = 0, ?string $component = null): bool
{
// check if we have a string
if (self::validate($guid))
@@ -135,56 +135,53 @@ abstract class GuidHelper
*
* @param string $guid
* @param string $table
* @param string/array $what
* @param string|array $what
* @param string|null $component
*
* @return mix
*
* @since 3.0.9
*/
public static function item($guid, $table, $what = 'a.id', $component = null)
public static function item($guid, $table, $what = 'a.id', ?string $component = null)
{
// check if we have a string
if (self::validate($guid))
// check if table already has this identifier
if (self::validate($guid) && StringHelper::check($table))
{
// check if table already has this identifier
if (StringHelper::check($table))
// check that we have the component code name
if (!is_string($component))
{
// check that we have the component code name
if (!is_string($component))
{
$component = (string) Helper::getCode();
}
// Get the database object and a new query object.
$db = Factory::getDbo();
$query = $db->getQuery(true);
$component = (string) Helper::getCode();
}
// Get the database object and a new query object.
$db = Factory::getDbo();
$query = $db->getQuery(true);
if (ArrayHelper::check($what))
if (ArrayHelper::check($what))
{
$query->select($db->quoteName($what));
}
else
{
$query->select($what);
}
$query->from($db->quoteName('#__' . (string) $component . '_' . (string) $table, 'a'))
->where($db->quoteName('a.guid') . ' = ' . $db->quote($guid));
// Set and query the database.
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
if (ArrayHelper::check($what) || $what === 'a.*')
{
$query->select($db->quoteName($what));
return $db->loadObject();
}
else
{
$query->select($what);
}
$query->from($db->quoteName('#__' . (string) $component . '_' . (string) $table, 'a'))
->where($db->quoteName('a.guid') . ' = ' . $db->quote($guid));
// Set and query the database.
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
if (ArrayHelper::check($what) || $what === 'a.*')
{
return $db->loadObject();
}
else
{
return $db->loadResult();
}
return $db->loadResult();
}
}
}

View File

@@ -32,7 +32,7 @@ abstract class JsonHelper
{
if (StringHelper::check($string))
{
json_decode($string);
json_decode((string) $string);
return (json_last_error() === JSON_ERROR_NONE);
}
@@ -52,14 +52,14 @@ abstract class JsonHelper
{
// do some table foot work
$external = false;
if (strpos($table, '#__') !== false)
if (is_string($table) && strpos((string) $table, '#__') !== false)
{
$external = true;
$table = str_replace('#__', '', $table);
$table = str_replace('#__', '', (string) $table);
}
// check if string is JSON
$result = json_decode($value, true);
$result = json_decode((string) $value, true);
if (json_last_error() === JSON_ERROR_NONE)
{
// is JSON
@@ -92,7 +92,7 @@ abstract class JsonHelper
}
return (string) implode($separator, $result);
}
return (string) json_decode($value);
return (string) json_decode((string) $value);
}
return $value;
}

View File

@@ -34,13 +34,13 @@ abstract class ClassfunctionHelper
public static function safe($name)
{
// remove numbers if the first character is a number
if (is_numeric(substr($name, 0, 1)))
if (is_numeric(substr((string) $name, 0, 1)))
{
$name = StringHelper::numbers($name);
}
// remove all spaces and strange characters
return trim(preg_replace("/[^A-Za-z0-9_-]/", '', $name));
return trim(preg_replace("/[^A-Za-z0-9_-]/", '', (string) $name));
}
}

View File

@@ -57,13 +57,13 @@ abstract class FieldHelper
if (StringHelper::check($string))
{
// check that the first character is not a number
if (is_numeric(substr($string, 0, 1)))
if (is_numeric(substr((string)$string, 0, 1)))
{
$string = StringHelper::numbers($string);
}
// remove all other strange characters
$string = trim($string);
$string = trim((string) $string);
$string = preg_replace('/'.$spacer.'+/', ' ', $string);
$string = preg_replace('/\s+/', ' ', $string);
@@ -71,10 +71,10 @@ abstract class FieldHelper
$string = StringHelper::transliterate($string);
// remove all and keep only characters and numbers
$string = preg_replace("/[^A-Za-z0-9 ]/", '', $string);
$string = preg_replace("/[^A-Za-z0-9 ]/", '', (string) $string);
// replace white space with underscore (SAFEST OPTION)
$string = preg_replace('/\s+/', $spacer, $string);
$string = preg_replace('/\s+/', (string) $spacer, $string);
// return all caps
if ($allcap)

View File

@@ -32,7 +32,7 @@ abstract class NamespaceHelper
*
* @since 3.0.9
*/
public static function safe(string $string, bool $removeNumbers = true)
public static function safe(string $string, bool $removeNumbers = true): string
{
// 0nly continue if we have a string with length
if (StringHelper::check($string))
@@ -47,7 +47,7 @@ abstract class NamespaceHelper
// $string = StringHelper::transliterate($string);
// first remove all [\] backslashes
$string = str_replace('\\', '+', $string);
$string = str_replace('\\', '+', (string) $string);
// remove all and keep only characters and [\] backslashes inside of the string
if ($removeNumbers)

View File

@@ -29,7 +29,7 @@ abstract class PluginHelper
*
* @since 3.0.9
*/
public static function safeFolderName($codeName, $group)
public static function safeFolderName(string $codeName, string $group): string
{
// editors-xtd group plugins must have a class with plgButton<PluginName> structure
if ($group === 'editors-xtd')
@@ -52,7 +52,7 @@ abstract class PluginHelper
*
* @since 3.0.9
*/
public static function safeClassName($codeName, $group)
public static function safeClassName(string $codeName, string $group): string
{
// editors-xtd group plugins must have a class with plgButton<PluginName> structure
if ($group === 'editors-xtd')
@@ -75,7 +75,7 @@ abstract class PluginHelper
*
* @since 3.0.9
*/
public static function safeInstallClassName($codeName, $group)
public static function safeInstallClassName(string $codeName, string $group): string
{
// editors-xtd group plugins must have a class with plgButton<PluginName> structure
if ($group === 'editors-xtd')
@@ -98,7 +98,7 @@ abstract class PluginHelper
*
* @since 3.0.9
*/
public static function safeLangPrefix($codeName, $group)
public static function safeLangPrefix(string $codeName, string $group): string
{
// editors-xtd group plugins must have a class with plgButton<PluginName> structure
if ($group === 'editors-xtd')

View File

@@ -65,7 +65,7 @@ abstract class TypeHelper
$string = StringHelper::transliterate($string);
// remove all and keep only characters and numbers and point (TODO just one point)
$string = trim(preg_replace("/[^A-Za-z0-9\.]/", '', $string));
$string = trim(preg_replace("/[^A-Za-z0-9\.]/", '', (string) $string));
// best is to return lower (for all string equality in compiler)
return strtolower($string);

View File

@@ -44,12 +44,7 @@ abstract class StringHelper
*/
public static function check($string): bool
{
if (is_string($string) && strlen($string) > 0)
{
return true;
}
return false;
return is_string($string) && strlen($string) > 0;
}
/**
@@ -65,8 +60,8 @@ abstract class StringHelper
{
if (self::check($string))
{
$initial = strlen($string);
$words = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
$initial = strlen((string) $string);
$words = preg_split('/([\s\n\r]+)/', (string) $string, null, PREG_SPLIT_DELIM_CAPTURE);
$words_count = count((array)$words);
$word_length = 0;
@@ -82,12 +77,12 @@ abstract class StringHelper
$newString = implode(array_slice($words, 0, $last_word));
$final = strlen($newString);
if ($initial != $final && $addTip)
if ($initial !== $final && $addTip)
{
$title = self::shorten($string, 400 , false);
return '<span class="hasTip" title="' . $title . '" style="cursor:help">' . trim($newString) . '...</span>';
}
elseif ($initial != $final && !$addTip)
elseif ($initial !== $final && !$addTip)
{
return trim($newString) . '...';
}
@@ -118,7 +113,7 @@ abstract class StringHelper
if ($type === 'filename')
{
// make sure VDM is not in the string
$string = str_replace('VDM', 'vDm', $string);
$string = str_replace('VDM', 'vDm', (string) $string);
// Remove anything which isn't a word, whitespace, number
// or any of the following caracters -_()
// If you don't need to handle multi-byte characters
@@ -131,7 +126,7 @@ abstract class StringHelper
return preg_replace('/\s+/', ' ', $string);
}
// remove all other characters
$string = trim($string);
$string = trim((string) $string);
$string = preg_replace('/'.$spacer.'+/', ' ', $string);
$string = preg_replace('/\s+/', ' ', $string);
// Transliterate string
@@ -150,7 +145,7 @@ abstract class StringHelper
if ($type === 'L' || $type === 'strtolower')
{
// replace white space with underscore
$string = preg_replace('/\s+/', $spacer, $string);
$string = preg_replace('/\s+/', (string) $spacer, $string);
// default is to return lower
return strtolower($string);
}
@@ -177,14 +172,14 @@ abstract class StringHelper
elseif ($type === 'U' || $type === 'strtoupper')
{
// replace white space with underscore
$string = preg_replace('/\s+/', $spacer, $string);
$string = preg_replace('/\s+/', (string) $spacer, $string);
// return all upper
return strtoupper($string);
}
elseif ($type === 'F' || $type === 'ucfirst')
{
// replace white space with underscore
$string = preg_replace('/\s+/', $spacer, $string);
$string = preg_replace('/\s+/', (string) $spacer, $string);
// return with first character to upper
return ucfirst(strtolower($string));
}
@@ -245,7 +240,7 @@ abstract class StringHelper
$string = $filter->clean(
html_entity_decode(
htmlentities(
$var,
(string) $var,
ENT_COMPAT,
$charset
)
@@ -276,21 +271,22 @@ abstract class StringHelper
public static function numbers($string)
{
// set numbers array
$numbers = array();
$numbers = [];
$search_replace= [];
// first get all numbers
preg_match_all('!\d+!', $string, $numbers);
preg_match_all('!\d+!', (string) $string, $numbers);
// check if we have any numbers
if (isset($numbers[0]) && ArrayHelper::check($numbers[0]))
{
foreach ($numbers[0] as $number)
{
$searchReplace[$number] = self::number((int)$number);
$search_replace[$number] = self::number((int)$number);
}
// now replace numbers in string
$string = str_replace(array_keys($searchReplace), array_values($searchReplace), $string);
$string = str_replace(array_keys($search_replace), array_values($search_replace), (string) $string);
// check if we missed any, strange if we did.
return self::numbers($string);
@@ -400,7 +396,7 @@ abstract class StringHelper
*
* @since 3.0.9
*/
public static function random($size)
public static function random($size): string
{
$bag = "abcefghijknopqrstuwxyzABCDDEFGHIJKLLMMNOPQRSTUVVWXYZabcddefghijkllmmnopqrstuvvwxyzABCEFGHIJKNOPQRSTUWXYZ";
$key = array();