diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e18c62..d004398 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## [3.0.0](https://github.com/phalcon/traits/releases/tag/3.0.0) (2024-12-28) + +### Added + +- Added static methods for `Str` and `Arr` traits [#41](https://github.com/phalcon/traits/issues/41) + +### Fixed + +- Added return types to all methods +- Enabled PHP Codesniffer in the CI run +- Enabled PHPStan with max configuration and made necessary adjustments +- Enabled Sonarqube in the CI run +- Added quality badges from Sonarqube + ## [2.0.1](https://github.com/phalcon/traits/releases/tag/2.0.1) (2023-01-03) ### Fixed diff --git a/README.md b/README.md index 0901072..86f0dc9 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ [![Phalcon CI](https://github.com/phalcon/traits/actions/workflows/main.yml/badge.svg?branch=1.x)](https://github.com/phalcon/traits/actions/workflows/main.yml) [![PDS Skeleton](https://img.shields.io/badge/pds-skeleton-blue.svg?style=flat-square)](https://github.com/php-pds/skeleton) -[![codecov](https://codecov.io/gh/phalcon/traits/branch/1.x/graph/badge.svg?token=I4bgs0E168)](https://codecov.io/gh/phalcon/traits) -[![Codacy Badge](https://app.codacy.com/project/badge/Grade/049cb6c1c06342579ebffb8e78ab9dd1)](https://www.codacy.com/gh/phalcon/traits/dashboard?utm_source=github.com&utm_medium=referral&utm_content=phalcon/traits&utm_campaign=Badge_Grade) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=phalcon_traits&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=phalcon_traits) +[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=phalcon_traits&metric=coverage)](https://sonarcloud.io/summary/new_code?id=phalcon_traits) This package contains traits with methods that are used for Phalcon v6 onward. diff --git a/src/Factory/FactoryTrait.php b/src/Factory/FactoryTrait.php index db3686e..8856f36 100644 --- a/src/Factory/FactoryTrait.php +++ b/src/Factory/FactoryTrait.php @@ -32,6 +32,13 @@ trait FactoryTrait */ private array $mapper = []; + /** + * Returns the exception class for the factory + * + * @return string + */ + abstract protected function getExceptionClass(): string; + /** * Returns a service based on the name; throws exception if it does not * exist @@ -69,11 +76,4 @@ protected function init(array $services = []): void { $this->mapper = array_merge($this->getServices(), $services); } - - /** - * Returns the exception class for the factory - * - * @return string - */ - abstract protected function getExceptionClass(): string; } diff --git a/src/Helper/Arr/FilterTrait.php b/src/Helper/Arr/FilterTrait.php index ddefd64..f898c58 100644 --- a/src/Helper/Arr/FilterTrait.php +++ b/src/Helper/Arr/FilterTrait.php @@ -28,7 +28,7 @@ trait FilterTrait * * @return array */ - protected function toFilter( + protected function staticToFilter( array $collection, callable $method = null, int $mode = 0 @@ -39,4 +39,19 @@ protected function toFilter( return array_filter($collection, $method, $mode); } + + /** + * @param array $collection + * @param callable|null $method + * @param int $mode + * + * @return array + */ + protected function toFilter( + array $collection, + callable $method = null, + int $mode = 0 + ): array { + return self::staticToFilter($collection, $method, $mode); + } } diff --git a/src/Helper/Str/CamelizeTrait.php b/src/Helper/Str/CamelizeTrait.php index 231f7a1..7c0c97a 100644 --- a/src/Helper/Str/CamelizeTrait.php +++ b/src/Helper/Str/CamelizeTrait.php @@ -35,12 +35,12 @@ trait CamelizeTrait * * @return string */ - public function toCamelize( + public static function staticCamelize( string $text, string $delimiters = '\-_', bool $lowerFirst = false ): string { - $exploded = $this->processArray($text, $delimiters); + $exploded = self::processArray($text, $delimiters); $output = array_map( function ($element) { @@ -58,13 +58,28 @@ function ($element) { return $result; } + /** + * @param string $text + * @param string $delimiters + * @param bool $lowerFirst + * + * @return string + */ + public function toCamelize( + string $text, + string $delimiters = '\-_', + bool $lowerFirst = false + ): string { + return self::staticCamelize($text, $delimiters, $lowerFirst); + } + /** * @param string $text * @param string $delimiters * * @return array */ - protected function processArray( + protected static function processArray( string $text, string $delimiters = '\-_' ): array { diff --git a/src/Helper/Str/DirFromFileTrait.php b/src/Helper/Str/DirFromFileTrait.php index 1353640..90ca299 100644 --- a/src/Helper/Str/DirFromFileTrait.php +++ b/src/Helper/Str/DirFromFileTrait.php @@ -31,7 +31,7 @@ trait DirFromFileTrait * * @return string */ - protected function toDirFromFile(string $file): string + protected static function staticToDirFromFile(string $file): string { $name = pathinfo($file, PATHINFO_FILENAME); $start = mb_substr($name, 0, -2); @@ -42,4 +42,14 @@ protected function toDirFromFile(string $file): string return implode('/', mb_str_split($start, 2)) . '/'; } + + /** + * @param string $file + * + * @return string + */ + protected function toDirFromFile(string $file): string + { + return self::staticToDirFromFile($file); + } } diff --git a/src/Helper/Str/DirSeparatorTrait.php b/src/Helper/Str/DirSeparatorTrait.php index 7546e87..d2ad02a 100644 --- a/src/Helper/Str/DirSeparatorTrait.php +++ b/src/Helper/Str/DirSeparatorTrait.php @@ -28,8 +28,18 @@ trait DirSeparatorTrait * * @return string */ - protected function toDirSeparator(string $directory): string + protected static function staticToDirSeparator(string $directory): string { return rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; } + + /** + * @param string $directory + * + * @return string + */ + protected function toDirSeparator(string $directory): string + { + return self::staticToDirSeparator($directory); + } } diff --git a/src/Helper/Str/InterpolateTrait.php b/src/Helper/Str/InterpolateTrait.php index 95484ae..3c220ee 100644 --- a/src/Helper/Str/InterpolateTrait.php +++ b/src/Helper/Str/InterpolateTrait.php @@ -28,7 +28,7 @@ trait InterpolateTrait * * @return string */ - protected function toInterpolate( + protected static function staticToInterpolate( string $input, array $context = [], string $left = '%', @@ -45,4 +45,21 @@ protected function toInterpolate( return strtr($input, $replace); } + + /** + * @param string $input + * @param string[] $context + * @param string $left + * @param string $right + * + * @return string + */ + protected function toInterpolate( + string $input, + array $context = [], + string $left = '%', + string $right = '%' + ): string { + return self::staticToInterpolate($input, $context, $left, $right); + } } diff --git a/src/Helper/Str/LowerTrait.php b/src/Helper/Str/LowerTrait.php index b640518..4144881 100644 --- a/src/Helper/Str/LowerTrait.php +++ b/src/Helper/Str/LowerTrait.php @@ -28,10 +28,23 @@ trait LowerTrait * * @return string */ - protected function toLower( + protected static function staticLower( string $text, string $encoding = 'UTF-8' ): string { return mb_convert_case($text, MB_CASE_LOWER, $encoding); } + + /** + * @param string $text + * @param string $encoding + * + * @return string + */ + protected function toLower( + string $text, + string $encoding = 'UTF-8' + ): string { + return self::staticLower($text, $encoding); + } } diff --git a/src/Helper/Str/UncamelizeTrait.php b/src/Helper/Str/UncamelizeTrait.php index 8a55dda..1746b01 100644 --- a/src/Helper/Str/UncamelizeTrait.php +++ b/src/Helper/Str/UncamelizeTrait.php @@ -24,7 +24,7 @@ trait UncamelizeTrait * * @return string */ - public function toUncamelize( + public static function staticUncamelize( string $text, string $delimiter = '_' ): string { @@ -36,4 +36,17 @@ public function toUncamelize( return mb_convert_case($text, MB_CASE_LOWER, 'UTF-8'); } + + /** + * @param string $text + * @param string $delimiter + * + * @return string + */ + public function toUncamelize( + string $text, + string $delimiter = '_' + ): string { + return self::staticUncamelize($text, $delimiter); + } } diff --git a/src/Helper/Str/UpperTrait.php b/src/Helper/Str/UpperTrait.php index 04cab86..f26ba0d 100644 --- a/src/Helper/Str/UpperTrait.php +++ b/src/Helper/Str/UpperTrait.php @@ -28,10 +28,23 @@ trait UpperTrait * * @return string */ - protected function toUpper( + protected static function staticUpper( string $text, string $encoding = 'UTF-8' ): string { return mb_convert_case($text, MB_CASE_UPPER, $encoding); } + + /** + * @param string $text + * @param string $encoding + * + * @return string + */ + protected function toUpper( + string $text, + string $encoding = 'UTF-8' + ): string { + return self::staticUpper($text, $encoding); + } } diff --git a/src/Php/FileTrait.php b/src/Php/FileTrait.php index ed9dc4a..1f3e86b 100644 --- a/src/Php/FileTrait.php +++ b/src/Php/FileTrait.php @@ -27,6 +27,43 @@ */ trait FileTrait { + /** + * Closes an open file pointer + * + * @link https://php.net/manual/en/function.fclose.php + * + * @param resource $handle + * + * @return bool + */ + protected function phpFclose($handle) + { + return fclose($handle); + } + + /** + * Gets line from file pointer and parse for CSV fields + * + * @param resource $stream + * @param int $length + * @param string $separator + * @param string $enclosure + * @param string $escape + * + * @return array|null|false + * + * @link https://php.net/manual/en/function.fgetcsv.php + */ + protected function phpFgetCsv( + $stream, + int $length = 0, + string $separator = ',', + string $enclosure = '"', + string $escape = '\\' + ) { + return fgetcsv($stream, $length, $separator, $enclosure, $escape); + } + /** * @param string $filename * @@ -46,7 +83,7 @@ protected function phpFileExists(string $filename) * * @link https://php.net/manual/en/function.file-get-contents.php */ - protected function phpFileGetContents(string $filename): false|string + protected function phpFileGetContents(string $filename): false | string { return file_get_contents($filename); } @@ -66,48 +103,10 @@ protected function phpFilePutContents( $data, int $flags = 0, $context = null - ): false|int { + ): false | int { return file_put_contents($filename, $data, $flags, $context); } - /** - * Closes an open file pointer - * - * @link https://php.net/manual/en/function.fclose.php - * - * @param resource $handle - * - * @return bool - */ - protected function phpFclose($handle) - { - return fclose($handle); - } - - /** - * Gets line from file pointer and parse for CSV fields - * - * @param resource $stream - * @param int $length - * @param string $separator - * @param string $enclosure - * @param string $escape - * - * @return array|null|false - * - * @link https://php.net/manual/en/function.fgetcsv.php - */ - protected function phpFgetCsv( - $stream, - int $length = 0, - string $separator = ',', - string $enclosure = '"', - string $escape = '\\' - ) { - return fgetcsv($stream, $length, $separator, $enclosure, $escape); - } - - /** * @param string $filename * @param string $mode diff --git a/src/Php/IniTrait.php b/src/Php/IniTrait.php index b881ffd..2dd1670 100644 --- a/src/Php/IniTrait.php +++ b/src/Php/IniTrait.php @@ -33,6 +33,81 @@ trait IniTrait protected function phpIniGet( string $input, string $defaultValue = "" + ): string { + return self::staticPhpIniGet($input, $defaultValue); + } + + /** + * Query a php.ini value and return it back as boolean + * + * @param string $input + * @param bool $defaultValue + * + * @return bool + * + * @link https://php.net/manual/en/function.ini-get.php + * @link https://php.net/manual/en/ini.list.php + */ + protected function phpIniGetBool( + string $input, + bool $defaultValue = false + ): bool { + return self::staticPhpIniGetBool($input, $defaultValue); + } + + /** + * Query a php.ini value and return it back as integer + * + * @param string $input + * @param int $defaultValue + * + * @return int + * + * @link https://php.net/manual/en/function.ini-get.php + * @link https://php.net/manual/en/ini.list.php + */ + protected function phpIniGetInt(string $input, int $defaultValue = 0): int + { + return self::staticPhpIniGetInt($input, $defaultValue); + } + + /** + * Parse a configuration file + * + * @param string $filename + * @param bool $processSections + * @param int $scannerMode + * + * @return array|false + * + * @link https://php.net/manual/en/function.parse-ini-file.php + */ + protected function phpParseIniFile( + string $filename, + bool $processSections = false, + int $scannerMode = 1 + ): array | false { + return self::staticPhpParseIniFile( + $filename, + $processSections, + $scannerMode + ); + } + + /** + * Gets the value of a configuration option + * + * @param string $input + * @param string $defaultValue + * + * @return string + * + * @link https://php.net/manual/en/function.ini-get.php + * @link https://php.net/manual/en/ini.list.php + */ + protected static function staticPhpIniGet( + string $input, + string $defaultValue = "" ): string { $value = ini_get($input); if (false === $value) { @@ -53,7 +128,7 @@ protected function phpIniGet( * @link https://php.net/manual/en/function.ini-get.php * @link https://php.net/manual/en/ini.list.php */ - protected function phpIniGetBool( + protected static function staticPhpIniGetBool( string $input, bool $defaultValue = false ): bool { @@ -62,16 +137,14 @@ protected function phpIniGetBool( return $defaultValue; } - $value = match (strtolower($value)) { + return match (strtolower($value)) { 'true', 'on', 'yes', 'y', - '1' => true, + '1' => true, default => false, }; - - return $value; } /** @@ -85,9 +158,11 @@ protected function phpIniGetBool( * @link https://php.net/manual/en/function.ini-get.php * @link https://php.net/manual/en/ini.list.php */ - protected function phpIniGetInt(string $input, int $defaultValue = 0): int - { - return (int)$this->iniGet($input, (string)$defaultValue); + protected static function staticPhpIniGetInt( + string $input, + int $defaultValue = 0 + ): int { + return (int)self::staticPhpIniGet($input, (string)$defaultValue); } /** @@ -101,11 +176,11 @@ protected function phpIniGetInt(string $input, int $defaultValue = 0): int * * @link https://php.net/manual/en/function.parse-ini-file.php */ - protected function phpParseIniFile( + protected static function staticPhpParseIniFile( string $filename, bool $processSections = false, int $scannerMode = 1 - ) { + ): array | false { return parse_ini_file($filename, $processSections, $scannerMode); } } diff --git a/src/Php/JsonTrait.php b/src/Php/JsonTrait.php index 1603f4e..c7db0e3 100644 --- a/src/Php/JsonTrait.php +++ b/src/Php/JsonTrait.php @@ -13,31 +13,14 @@ namespace Phalcon\Traits\Php; -use function json_encode; use function json_decode; +use function json_encode; /** * JSON wrapper methods */ trait JsonTrait { - /** - * @param mixed $value - * @param int $flags - * @param int $depth - * - * @return false|string - * - * @link https://php.net/manual/en/function.json-encode.php - */ - protected function phpJsonEncode( - mixed $value, - int $flags = 0, - int $depth = 512 - ): false|string { - return json_encode($value, $flags, $depth); - } - /** * @param string $json * @param bool|null $associative @@ -56,4 +39,21 @@ protected function phpJsonDecode( ): mixed { return json_decode($json, $associative, $flags, $depth); } + + /** + * @param mixed $value + * @param int $flags + * @param int $depth + * + * @return false|string + * + * @link https://php.net/manual/en/function.json-encode.php + */ + protected function phpJsonEncode( + mixed $value, + int $flags = 0, + int $depth = 512 + ): false | string { + return json_encode($value, $flags, $depth); + } } diff --git a/src/Php/UrlTrait.php b/src/Php/UrlTrait.php index 4099f0e..eadd3eb 100644 --- a/src/Php/UrlTrait.php +++ b/src/Php/UrlTrait.php @@ -80,7 +80,7 @@ protected function doBase64EncodeUrl(string $input): string protected function phpBase64Decode( string $input, bool $strict = false - ): string|false { + ): string | false { return base64_decode($input, $strict); } @@ -104,7 +104,7 @@ protected function phpBase64Encode(string $input): string * * @link https://www.php.net/manual/en/function.parse-url.php */ - protected function phpParseUrl(string $url, int $component = -1): array|false|int|string|null + protected function phpParseUrl(string $url, int $component = -1): array | false | int | string | null { return parse_url($url, $component); }