From 065965034bfafcfffd466dc259cc1a86d3ebbb41 Mon Sep 17 00:00:00 2001 From: Leonid Sheikman Date: Mon, 12 Feb 2024 12:10:35 +0300 Subject: [PATCH] big reformat - add strict_types=1 --- src/ArrayHelper.php | 128 ++++++++++++++++++++++--------------------- src/ConfigHelper.php | 88 +++++++++++++++-------------- src/EmailHelper.php | 13 +++-- src/FileHelper.php | 19 ++++--- src/PhoneHelper.php | 66 ++++++++++++---------- src/StringHelper.php | 2 +- src/UrlHelper.php | 47 ++++++++-------- 7 files changed, 192 insertions(+), 171 deletions(-) diff --git a/src/ArrayHelper.php b/src/ArrayHelper.php index d325645..0390342 100644 --- a/src/ArrayHelper.php +++ b/src/ArrayHelper.php @@ -1,4 +1,4 @@ - $mItem ) { - $result = $bCaseInsensitive ? \mb_strtolower( $mNeedle ) == \mb_strtolower( $key ) : (string) $mNeedle == (string) $key; + if (!empty($mNeedle) && !empty($aHaystack)) { + foreach ($aHaystack as $key => $mItem) { + $result = $bCaseInsensitive ? \mb_strtolower($mNeedle) == \mb_strtolower($key) : (string) $mNeedle == (string) $key; - if ( $result ) { + if ($result) { break; } - if ( $bProcessSubarrays && \is_array( $mItem ) ) { - $result = self::arrayKeyExists( $mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays ); + if ($bProcessSubarrays && \is_array($mItem)) { + $result = self::arrayKeyExists($mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays); - if ( $result ) { + if ($result) { break; } } @@ -85,22 +86,22 @@ public static function arrayValueExists( ): bool { $result = false; - if ( !empty( $mNeedle ) && !empty( $aHaystack ) ) { - foreach ( $aHaystack as $mItem ) { - if ( \is_array( $mItem ) ) { - if ( !$bProcessSubarrays ) { + if (!empty($mNeedle) && !empty($aHaystack)) { + foreach ($aHaystack as $mItem) { + if (\is_array($mItem)) { + if (!$bProcessSubarrays) { continue; } - $result = self::arrayValueExists( $mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays, $bStrictTypes ); + $result = self::arrayValueExists($mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays, $bStrictTypes); } else { - if ( $bCaseInsensitive ) { - $result = $bStrictTypes ? ( \gettype( $mNeedle ) == \gettype( $mItem ) && \mb_strtolower( $mNeedle ) == \mb_strtolower( $mItem ) ) : \mb_strtolower( $mNeedle ) == \mb_strtolower( $mItem ); + if ($bCaseInsensitive) { + $result = $bStrictTypes ? (\gettype($mNeedle) == \gettype($mItem) && \mb_strtolower($mNeedle) == \mb_strtolower($mItem)) : \mb_strtolower($mNeedle) == \mb_strtolower($mItem); } else { $result = $bStrictTypes ? $mNeedle === $mItem : (string) $mNeedle == (string) $mItem; } } - if ( $result ) { + if ($result) { break; } } @@ -116,7 +117,7 @@ public static function arrayValueExists( * * @param string $sHaystack * @param array $aNeedles - * @param int $iOffset (optional, defaults to 0) + * @param int $iOffset (optional, defaults to 0) * @param bool $bCaseInSensitive (optional, defaults to false) * * @return bool @@ -127,9 +128,9 @@ public static function arrayNeedlesExists( ?int $iOffset = 0, ?bool $bCaseInSensitive = false ): bool { - if ( !empty( $aNeedles ) && !empty( $sHaystack ) ) { - foreach ( $aNeedles as $needle ) { - if ( ( $bCaseInSensitive ? \stripos( $sHaystack, $needle, $iOffset ) : \strpos( $sHaystack, $needle, $iOffset ) ) !== false ) { + if (!empty($aNeedles) && !empty($sHaystack)) { + foreach ($aNeedles as $needle) { + if (($bCaseInSensitive ? \stripos($sHaystack, $needle, $iOffset) : \strpos($sHaystack, $needle, $iOffset)) !== false) { return true; } } @@ -162,26 +163,27 @@ public static function arraySearch( ) { $result = false; - if ( !empty( $mNeedle ) && !empty( $aHaystack ) ) { - foreach ( $aHaystack as $key => $mItem ) { - if ( \is_array( $mItem ) ) { - if ( !$bProcessSubarrays ) { + if (!empty($mNeedle) && !empty($aHaystack)) { + foreach ($aHaystack as $key => $mItem) { + if (\is_array($mItem)) { + if (!$bProcessSubarrays) { continue; } - $result = self::arraySearch( $mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays, $bStrictTypes ); + $result = self::arraySearch($mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays, $bStrictTypes); - if ( false !== $result ) { + if (false !== $result) { break; } } else { - if ( $bCaseInsensitive ) { - $result = $bStrictTypes ? ( \gettype( $mNeedle ) == \gettype( $mItem ) && \mb_strtolower( $mNeedle ) == \mb_strtolower( $mItem ) ) : \mb_strtolower( $mNeedle ) == \mb_strtolower( $mItem ); + if ($bCaseInsensitive) { + $result = $bStrictTypes ? (\gettype($mNeedle) == \gettype($mItem) && \mb_strtolower($mNeedle) == \mb_strtolower($mItem)) : \mb_strtolower($mNeedle) == \mb_strtolower($mItem); } else { $result = $bStrictTypes ? $mNeedle === $mItem : (string) $mNeedle == (string) $mItem; } - if ( false !== $result ) { + if (false !== $result) { $result = $key; + break; } } @@ -200,7 +202,7 @@ public static function arraySearch( * * @param mixed $mNeedle * @param array $aHaystack - * @param bool $bCaseInsensitive (optional, defaults to false) + * @param bool $bCaseInsensitive (optional, defaults to false) * @param bool $bProcessSubarrays (optional, defaults to true) * * @return mixed @@ -213,20 +215,21 @@ public static function arrayStrPos( ) { $result = false; - if ( !empty( $mNeedle ) && !empty( $aHaystack ) ) { - foreach ( $aHaystack as $key => $mItem ) { - if ( \is_array( $mItem ) ) { - if ( !$bProcessSubarrays ) { + if (!empty($mNeedle) && !empty($aHaystack)) { + foreach ($aHaystack as $key => $mItem) { + if (\is_array($mItem)) { + if (!$bProcessSubarrays) { continue; } - $result = self::arrayStrPos( $mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays ); + $result = self::arrayStrPos($mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays); - if ( false !== $result ) { + if (false !== $result) { break; } } else { - if ( false !== ( $bCaseInsensitive ? \mb_stripos( $mItem, $mNeedle ) : \mb_strpos( $mItem, $mNeedle ) ) ) { + if (false !== ($bCaseInsensitive ? \mb_stripos($mItem, $mNeedle) : \mb_strpos($mItem, $mNeedle))) { $result = $key; + break; } } @@ -243,14 +246,14 @@ public static function arrayStrPos( * Массив объединённый в строку (к примеру, для сохранения в CSV). * Вложенные подмассивы будут также объединены в строку с разделителем subseparator. * - * @param array $aHaystack + * @param array $aHaystack * @param string $default * @param string $separator * @param string $subseparator - * @param bool $isRecursion (service parameter) + * @param bool $isRecursion (service parameter) * * @return string converted array to string - */ + */ public static function arrayToCsvString( array $aHaystack, ?string $default = '', @@ -258,18 +261,18 @@ public static function arrayToCsvString( ?string $subseparator = '|', ?bool $isRecursion = false ): string { - if ( empty( $aHaystack ) ) { + if (empty($aHaystack)) { return $default; } $separator = $isRecursion ? $separator : '"' . $separator . '"'; $res = []; - foreach ( $aHaystack as $row ) { - $res[] = \is_array( $row ) ? \trim( self::arrayToCsvString( $row, $default, $subseparator, '||', true ), "\"\n\r" ) : \trim( $row ); + foreach ($aHaystack as $row) { + $res[] = \is_array($row) ? \trim(self::arrayToCsvString($row, $default, $subseparator, '||', true), "\"\n\r") : \trim($row); } - return '"' . \implode( $separator, $res ) . '"' . PHP_EOL; + return '"' . \implode($separator, $res) . '"' . PHP_EOL; } /** @@ -277,39 +280,41 @@ public static function arrayToCsvString( * * Превратить Многомерный Массив в одномерный. * - * @param array $aHaystack - * @param string|bool $prefix + * @param array $aHaystack + * @param bool|string $prefix * * @return array * * @author https://github.com/php-curl-class/php-curl-class + * * @edit Leonid Sheikman (leonid74) */ public static function arrayFlattenMulti( ?array $aHaystack = [], - bool $prefix = false + $prefix = false ): array { $aResult = []; - if ( $prefix && $aHaystack === null ) { + if ($prefix && $aHaystack === null) { $aResult[$prefix] = null; return $aResult; } - if ( $prefix && empty( $aHaystack ) ) { + if ($prefix && empty($aHaystack)) { $aResult[$prefix] = ''; return $aResult; } - if ( !empty( $aHaystack ) ) { - foreach ( $aHaystack as $key => $val ) { - if ( \is_scalar( $val ) ) { - $aResult[ $prefix ? $prefix . '[' . $key . ']' : $key ] = $val; + if (!empty($aHaystack)) { + foreach ($aHaystack as $key => $val) { + if (\is_scalar($val)) { + $aResult[$prefix ? $prefix . '[' . $key . ']' : $key] = $val; + continue; } - $aResult = \array_merge( $aResult, self::arrayFlattenMulti( $val, $prefix ? $prefix . '[' . $key . ']' : $key ) ); + $aResult = \array_merge($aResult, self::arrayFlattenMulti($val, $prefix ? $prefix . '[' . $key . ']' : $key)); } } @@ -333,13 +338,13 @@ public static function arrayMapRecursive( string $func, array $aHaystack = [] ): array { - if ( empty( $aHaystack ) ) { + if (empty($aHaystack)) { return []; } $aResult = []; - foreach ( $aHaystack as $key => $value ) { - $aResult[$key] = ( \is_array( $value ) ? self::arrayMapRecursive( $func, $value ) : $func( $value ) ); + foreach ($aHaystack as $key => $value) { + $aResult[$key] = (\is_array($value) ? self::arrayMapRecursive($func, $value) : $func($value)); } return $aResult; @@ -352,7 +357,7 @@ public static function arrayMapRecursive( * * @param array|string $search * @param array|string $replace - * @param array $aHaystack + * @param array $aHaystack * * @return array * @@ -363,9 +368,10 @@ public static function arrayStrReplaceMulti( $replace, array $aHaystack = [] ): array { - if ( empty( $aHaystack ) ) { + if (empty($aHaystack)) { return []; } - return \json_decode( \str_replace( $search, $replace, \json_encode( $aHaystack, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) ), true ); + + return \json_decode(\str_replace($search, $replace, \json_encode($aHaystack, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)), true); } } diff --git a/src/ConfigHelper.php b/src/ConfigHelper.php index 196ef9c..9df05cd 100644 --- a/src/ConfigHelper.php +++ b/src/ConfigHelper.php @@ -1,4 +1,4 @@ - $val ) { - if ( $sPrefix !== null ) { + foreach ($aData as $key => $val) { + if ($sPrefix !== null) { $key = $sPrefix . '.' . $key; } - self::set( $key, $val ); + self::set($key, $val); } } /** * Get the data from the config by key * - * @param string $key Key - * @param mixed $default default value + * @param string $key Key + * @param mixed $default default value * * @return mixed Value */ - public static function get( string $key, $default = null ) + public static function get(string $key, $default = null) { return static::$aData[$key] ?? $default; } @@ -121,9 +123,9 @@ public static function all(): array * * @return bool */ - public static function hasKey( string $key ): bool + public static function hasKey(string $key): bool { - return isset( self::$aData[$key] ); + return isset(self::$aData[$key]); } /** @@ -131,57 +133,59 @@ public static function hasKey( string $key ): bool * * @see hasKey * + * @param string $key + * * @return bool */ - public static function exists( string $key ): bool + public static function exists(string $key): bool { - return self::hasKey( $key ); + return self::hasKey($key); } /** * Get file content as php array * - * @param string $sPath Path to file - * @param array $aPathinfo pathinfo() array + * @param string $sPath Path to file + * @param array $aPathinfo pathinfo() array * * @return array */ - protected static function getFileContent( string $sPath, array $aPathinfo ): array + protected static function getFileContent(string $sPath, array $aPathinfo): array { - switch ( $aPathinfo['extension'] ) { + switch ($aPathinfo['extension']) { case 'php': - $aOptions = require( $sPath ); - break; + $aOptions = require $sPath; - case 'ini': - $aOptions = \parse_ini_file( $sPath, static::$iniProcessSections, static::$iniScannerMode ); break; + case 'ini': + $aOptions = \parse_ini_file($sPath, static::$iniProcessSections, static::$iniScannerMode); - case 'json': - $aOptions = \json_decode( \file_get_contents( $sPath ), true ); break; + case 'json': + $aOptions = \json_decode(\file_get_contents($sPath), true); + break; default: - throw new \Exception( 'Unsupported filetype: ' . $aPathinfo['extension'] ); + throw new \Exception('Unsupported filetype: ' . $aPathinfo['extension']); } - return \is_array( $aOptions ) ? $aOptions : []; + return \is_array($aOptions) ? $aOptions : []; } /** * Set the data to the config * - * @param string $key Key name - * @param mixed $value Value + * @param string $key Key name + * @param mixed $value Value * * @return void */ - protected static function set( string $key, $value ): void + protected static function set(string $key, $value): void { - if ( \is_array( $value ) ) { - foreach ( $value as $key2 => $val2 ) { + if (\is_array($value)) { + foreach ($value as $key2 => $val2) { $key_path = $key . '.' . $key2; - self::set( $key_path, $val2 ); + self::set($key_path, $val2); } } diff --git a/src/EmailHelper.php b/src/EmailHelper.php index ec98131..9d80587 100644 --- a/src/EmailHelper.php +++ b/src/EmailHelper.php @@ -1,4 +1,4 @@ -phone = $sString; + $oResult = new \stdClass(); + $oResult->phone = $sString; $oResult->phone_exists = 'unknown'; - $oResult->is_error = false; + $oResult->is_error = false; - if ( empty( $sString ) ) { + if (empty($sString)) { $oResult->phone_exists = false; + return $oResult; } @@ -111,41 +115,43 @@ public static function isPhoneExist( string $sString, string $apiToken ) $i = 1; do { $json = \json_decode( - \file_get_contents( "https://sms.ru/sms/cost?api_id={$apiToken}&to={$sString}&msg=" . \urlencode( 'Test' . \microtime( true ) ) . "&json=1" ) + \file_get_contents("https://sms.ru/sms/cost?api_id={$apiToken}&to={$sString}&msg=" . \urlencode('Test' . \microtime(true)) . '&json=1') ); - if ( ( \json_last_error() === JSON_ERROR_NONE ) && $json ) { - if ( $json->status === "OK" ) { - foreach ( $json->sms as $data ) { + if ((\json_last_error() === JSON_ERROR_NONE) && $json) { + if ($json->status === 'OK') { + foreach ($json->sms as $data) { $oResult->is_error = false; - if ( $data->status === "OK" ) { + if ($data->status === 'OK') { // The message has been processed, the number exists $oResult->phone_exists = true; + return $oResult; } // The message has been processed, the number does not exists $oResult->phone_exists = false; + return $oResult; } } else { // The request was not executed (possibly an authorization error, parameters, etc...) $oResult->phone_exists = 'unknown'; - $oResult->is_error = true; - $oResult->errors[] = $json->status_code . ': ' . $json->status_text; + $oResult->is_error = true; + $oResult->errors[] = $json->status_code . ': ' . $json->status_text; } } else { // The request was not executed (Error decoding the response or Failed to establish connection with the server) $oResult->phone_exists = 'unknown'; - $oResult->is_error = true; - $oResult->errors[] = 'Error decoding the response or Failed to establish connection with the server'; + $oResult->is_error = true; + $oResult->errors[] = 'Error decoding the response or Failed to establish connection with the server'; } - \usleep( 500000 ); - } while ( ++$i <= 3 ); - } catch ( \Exception $e ) { + \usleep(500000); + } while (++$i <= 3); + } catch (\Exception $e) { $oResult->phone_exists = 'unknown'; - $oResult->is_error = true; - $oResult->errors[] = 'A critical error occurred while checking the phone: (' . $e->getCode() . ') ' . $e->getMessage(); + $oResult->is_error = true; + $oResult->errors[] = 'A critical error occurred while checking the phone: (' . $e->getCode() . ') ' . $e->getMessage(); } return $oResult; diff --git a/src/StringHelper.php b/src/StringHelper.php index 14a2f5e..b40ae34 100644 --- a/src/StringHelper.php +++ b/src/StringHelper.php @@ -6,7 +6,7 @@ * Вспомогательный класс для обработки строк * * @author Leonid Sheikman (leonid74) - * @copyright 2019-2022 Leonid Sheikman + * @copyright 2019-2024 Leonid Sheikman * * @see https://github.com/Leonid74/helpers-php * diff --git a/src/UrlHelper.php b/src/UrlHelper.php index 8922e90..93febcb 100644 --- a/src/UrlHelper.php +++ b/src/UrlHelper.php @@ -1,4 +1,4 @@ -