Skip to content

Commit

Permalink
Merge pull request #43 from niden-code/develop
Browse files Browse the repository at this point in the history
Changelog, badges
  • Loading branch information
niden authored Dec 28, 2024
2 parents 1d55207 + 87f04ce commit 283b3e6
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 89 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
14 changes: 7 additions & 7 deletions src/Factory/FactoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
17 changes: 16 additions & 1 deletion src/Helper/Arr/FilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait FilterTrait
*
* @return array<array-key, mixed>
*/
protected function toFilter(
protected function staticToFilter(
array $collection,
callable $method = null,
int $mode = 0
Expand All @@ -39,4 +39,19 @@ protected function toFilter(

return array_filter($collection, $method, $mode);
}

/**
* @param array<array-key, mixed> $collection
* @param callable|null $method
* @param int $mode
*
* @return array<array-key, mixed>
*/
protected function toFilter(
array $collection,
callable $method = null,
int $mode = 0
): array {
return self::staticToFilter($collection, $method, $mode);
}
}
21 changes: 18 additions & 3 deletions src/Helper/Str/CamelizeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<array-key, string>
*/
protected function processArray(
protected static function processArray(
string $text,
string $delimiters = '\-_'
): array {
Expand Down
12 changes: 11 additions & 1 deletion src/Helper/Str/DirFromFileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
12 changes: 11 additions & 1 deletion src/Helper/Str/DirSeparatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
19 changes: 18 additions & 1 deletion src/Helper/Str/InterpolateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait InterpolateTrait
*
* @return string
*/
protected function toInterpolate(
protected static function staticToInterpolate(
string $input,
array $context = [],
string $left = '%',
Expand All @@ -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);
}
}
15 changes: 14 additions & 1 deletion src/Helper/Str/LowerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 14 additions & 1 deletion src/Helper/Str/UncamelizeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait UncamelizeTrait
*
* @return string
*/
public function toUncamelize(
public static function staticUncamelize(
string $text,
string $delimiter = '_'
): string {
Expand All @@ -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);
}
}
15 changes: 14 additions & 1 deletion src/Helper/Str/UpperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
79 changes: 39 additions & 40 deletions src/Php/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, mixed>|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
*
Expand All @@ -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);
}
Expand All @@ -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<array-key, mixed>|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
Expand Down
Loading

0 comments on commit 283b3e6

Please sign in to comment.