From e04307b527ac9b997a62e8a2b7ef11444ed75b1f Mon Sep 17 00:00:00 2001 From: jawira Date: Mon, 13 Jun 2022 21:44:19 +0200 Subject: [PATCH] feat: Bump minimum PHP version to 8.0 --- .editorconfig | 9 ++ .github/workflows/qa.yaml | 4 +- README.md | 39 +++---- composer.json | 2 +- docs/case-mapping.md | 6 +- src/CaseConverter.php | 1 - src/CaseConverterException.php | 5 +- src/CaseConverterInterface.php | 1 - src/Convert.php | 167 +++++++--------------------- src/Glue/AdaCase.php | 2 - src/Glue/CamelCase.php | 2 - src/Glue/CobolCase.php | 2 - src/Glue/DashGluer.php | 3 +- src/Glue/DotNotation.php | 3 +- src/Glue/Gluer.php | 74 +++--------- src/Glue/SpaceGluer.php | 3 +- src/Glue/UnderscoreGluer.php | 3 +- src/Glue/UppercaseGluer.php | 3 +- src/Split/DashSplitter.php | 3 +- src/Split/DotSplitter.php | 4 +- src/Split/SpaceSplitter.php | 3 +- src/Split/Splitter.php | 9 +- src/Split/UnderscoreSplitter.php | 2 +- src/Split/UppercaseSplitter.php | 2 +- tests/phpunit/CaseConverterTest.php | 1 - tests/phpunit/ConvertTest.php | 57 +++++----- 26 files changed, 141 insertions(+), 269 deletions(-) diff --git a/.editorconfig b/.editorconfig index 7c6ebd5..4e244fc 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,6 +10,15 @@ ij_visual_guides = 80, 120 [*.php] indent_size = 4 +max_line_length = 120 +ij_php_align_multiline_chained_methods = true +ij_php_align_phpdoc_comments = true +ij_php_align_phpdoc_param_names = true +ij_php_align_assignments = true +ij_php_align_multiline_array_initializer_expression = true +ij_php_align_key_value_pairs = true +ij_php_align_match_arm_bodies = true +ij_php_phpdoc_use_fqcn = true [*.{yml, yaml, neon, xml, svg, json, js}] indent_size = 2 diff --git a/.github/workflows/qa.yaml b/.github/workflows/qa.yaml index e811477..e90af93 100644 --- a/.github/workflows/qa.yaml +++ b/.github/workflows/qa.yaml @@ -10,8 +10,8 @@ jobs: tests: strategy: matrix: - php: ['7.3','7.4','8.0','8.1'] - runs-on: ubuntu-20.04 + php: [ '7.4','8.0','8.1' ] + runs-on: ubuntu-22.04 steps: - name: Setup PHP diff --git a/README.md b/README.md index 306f9b7..cc1cd35 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,21 @@ Case converter Use this library to convert string between: -| Name | Method | Output example | -| ------------- | --------------- | ----------------- | -| 🐪 Camel case | `toCamel()` | `myNameIsBond` | -| 👨‍🏫 Pascal case | `toPascal()` | `MyNameIsBond` | -| 🐍 Snake case | `toSnake()` | `my_name_is_bond` | -| 👩‍🏫 Ada case | `toAda()` | `My_Name_Is_Bond` | -| Ⓜ️ Macro case | `toMacro()` | `MY_NAME_IS_BOND` | -| 🥙 Kebab case | `toKebab()` | `my-name-is-bond` | -| 🚂 Train case | `toTrain()` | `My-Name-Is-Bond` | -| 🏦 Cobol case | `toCobol()` | `MY-NAME-IS-BOND` | -| 🔡 Lower case | `toLower()` | `my name is bond` | -| 🔠 Upper case | `toUpper()` | `MY NAME IS BOND` | -| 📰 Title case | `toTitle()` | `My Name Is Bond` | -| ✍️ Sentence case | `toSentence()` | `My name is bond` | -| ⚙️ Dot notation | `toDot()` | `my.name.is.bond` | +| Name | Method | Output example | +|-------------------|----------------|-------------------| +| 🐪 Camel case | `toCamel()` | `myNameIsBond` | +| 👨‍🏫 Pascal case | `toPascal()` | `MyNameIsBond` | +| 🐍 Snake case | `toSnake()` | `my_name_is_bond` | +| 👩‍🏫 Ada case | `toAda()` | `My_Name_Is_Bond` | +| Ⓜ️ Macro case | `toMacro()` | `MY_NAME_IS_BOND` | +| 🥙 Kebab case | `toKebab()` | `my-name-is-bond` | +| 🚂 Train case | `toTrain()` | `My-Name-Is-Bond` | +| 🏦 Cobol case | `toCobol()` | `MY-NAME-IS-BOND` | +| 🔡 Lower case | `toLower()` | `my name is bond` | +| 🔠 Upper case | `toUpper()` | `MY NAME IS BOND` | +| 📰 Title case | `toTitle()` | `My Name Is Bond` | +| ✍️ Sentence case | `toSentence()` | `My name is bond` | +| ⚙️ Dot notation | `toDot()` | `my.name.is.bond` | Features: @@ -52,8 +52,8 @@ Of course you can explicitly set the format of input string: echo $hero->fromKebab()->toSnake(); // output: john_connor ``` -You can also use the [provided factory][factory] to instantiate `Convert` class. A list of [all public methods] is also -available. +You can also use the [provided factory][factory] to instantiate `Convert` class. +A list of [all public methods] is also available. i18n ---- @@ -74,7 +74,8 @@ $rus = new Convert('ОЧЕНЬ_ПРИЯТНО'); echo $rus->toCamel(); // output: оченьПриятно ``` -`case-converter` is compatible with _Simple Case-Mapping_ and _Full Case-Mapping_. +`case-converter` is compatible with _Simple Case-Mapping_ and _Full +Case-Mapping_. [Learn more about Case-Mapping][Case-Mapping]. Installation @@ -94,7 +95,7 @@ Contributing - If you liked this project, ⭐ star it on GitHub. [![GitHub Repo stars](https://img.shields.io/github/stars/jawira/case-converter?style=social)](https://github.com/jawira/case-converter) -- Or follow me on Twitter. +- Or follow me on Twitter. [![Twitter Follow](https://img.shields.io/twitter/follow/jawira?style=social)](https://twitter.com/jawira) License diff --git a/composer.json b/composer.json index 5b54a02..6643d8d 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ } ], "require": { - "php": "^7.1 || ^8.0", + "php": ">=7.4", "ext-mbstring": "*" }, "require-dev": { diff --git a/docs/case-mapping.md b/docs/case-mapping.md index 80325b6..2ecf5fc 100644 --- a/docs/case-mapping.md +++ b/docs/case-mapping.md @@ -4,8 +4,8 @@ Case-Mapping Introduction ------------ -> Case mapping or case conversion is a process whereby strings are converted -> to a particular form—uppercase, lowercase, or titlecase—possibly for display +> Case mapping or case conversion is a process whereby strings are converted +> to a particular form—uppercase, lowercase, or titlecase—possibly for display > to the user. PHP always performed _Simple Case-Mapping_, this is map one-to-one character @@ -28,7 +28,7 @@ Please note that _Full Case-Mapping_ is locale dependent: ```php // Turkish (requires appropriate locale) -$tur = new Convert('istambul'); +$tur = new Convert('istambul'); echo $tur->toTrain(); // output: İstanbul ``` diff --git a/src/CaseConverter.php b/src/CaseConverter.php index 41ac1dd..cfba5d8 100644 --- a/src/CaseConverter.php +++ b/src/CaseConverter.php @@ -7,7 +7,6 @@ * * Factory class which returns a Convert object. * - * @package Jawira\CaseConverter * @author Jawira Portugal */ class CaseConverter implements CaseConverterInterface diff --git a/src/CaseConverterException.php b/src/CaseConverterException.php index 30bf9bc..a50abd5 100644 --- a/src/CaseConverterException.php +++ b/src/CaseConverterException.php @@ -2,9 +2,8 @@ namespace Jawira\CaseConverter; -use Exception; +use RuntimeException; -class CaseConverterException extends Exception +class CaseConverterException extends RuntimeException { - } diff --git a/src/CaseConverterInterface.php b/src/CaseConverterInterface.php index ad57d9a..58df113 100644 --- a/src/CaseConverterInterface.php +++ b/src/CaseConverterInterface.php @@ -5,7 +5,6 @@ /** * Interface CaseConverterInterface * - * @package Jawira\CaseConverter * @author Jawira Portugal */ interface CaseConverterInterface diff --git a/src/Convert.php b/src/Convert.php index 7bf5051..a41eb5a 100644 --- a/src/Convert.php +++ b/src/Convert.php @@ -26,7 +26,6 @@ use Jawira\CaseConverter\Split\UnderscoreSplitter; use Jawira\CaseConverter\Split\UppercaseSplitter; use function is_subclass_of; -use function mb_strpos; use function preg_match; /** @@ -77,25 +76,17 @@ * * @see https://softwareengineering.stackexchange.com/questions/322413/bothered-by-an-unknown-letter-case-name * @see http://www.unicode.org/charts/case/ - * @package Jawira\CaseConverter * @author Jawira Portugal */ class Convert { - /** - * @var string Input string to convert - */ - protected $source; + /** @var string Input string to convert */ + protected string $source; - /** - * @var string[] Words extracted from input string - */ - protected $words; + /** @var string[] Words extracted from input string */ + protected array $words; - /** - * @var bool - */ - protected $forceSimpleCaseMapping; + protected bool $forceSimpleCaseMapping; /** * Constructor method @@ -114,17 +105,17 @@ public function __construct(string $source) /** * Handle `to*` methods and `from*` methods * - * @param string $methodName - * @param mixed[] $arguments + * @param string $methodName + * @param mixed[] $arguments * * @return string|\Jawira\CaseConverter\Convert * @throws \Jawira\CaseConverter\CaseConverterException */ public function __call($methodName, $arguments) { - if (0 === mb_strpos($methodName, 'from')) { + if (str_starts_with($methodName, 'from')) { $result = $this->handleSplitterMethod($methodName); - } elseif (0 === mb_strpos($methodName, 'to')) { + } elseif (str_starts_with($methodName, 'to')) { $result = $this->handleGluerMethod($methodName); } else { throw new CaseConverterException("Unknown method: $methodName"); @@ -136,7 +127,6 @@ public function __call($methodName, $arguments) /** * Auto-detect naming convention * - * @return \Jawira\CaseConverter\Convert * @throws \Jawira\CaseConverter\CaseConverterException */ public function fromAuto(): self @@ -187,46 +177,19 @@ public function forceSimpleCaseMapping(): self * * @param string $input String to be analysed * - * @throws \Jawira\CaseConverter\CaseConverterException * @return \Jawira\CaseConverter\Split\Splitter + * @throws \Jawira\CaseConverter\CaseConverterException */ protected function analyse(string $input): Splitter { - switch (true) { - case $this->contains($input, UnderscoreGluer::DELIMITER): - $splittingStrategy = new UnderscoreSplitter($input); - break; - case $this->contains($input, DashGluer::DELIMITER): - $splittingStrategy = new DashSplitter($input); - break; - case $this->contains($input, SpaceGluer::DELIMITER): - $splittingStrategy = new SpaceSplitter($input); - break; - case $this->contains($input, DotNotation::DELIMITER): - $splittingStrategy = new DotSplitter($input); - break; - case $this->isUppercaseWord($input): - $splittingStrategy = new UnderscoreSplitter($input); - break; - default: - $splittingStrategy = new UppercaseSplitter($input); - break; - } - - return $splittingStrategy; - } - - /** - * Return true if $needle is found in $input string - * - * @param string $input String where the search is performed - * @param string $needle Needle - * - * @return bool - */ - protected function contains(string $input, string $needle): bool - { - return is_int(mb_strpos($input, $needle)); + return match (true) { + str_contains($input, UnderscoreGluer::DELIMITER) => new UnderscoreSplitter($input), + str_contains($input, DashGluer::DELIMITER) => new DashSplitter($input), + str_contains($input, SpaceGluer::DELIMITER) => new SpaceSplitter($input), + str_contains($input, DotNotation::DELIMITER) => new DotSplitter($input), + $this->isUppercaseWord($input) => new UnderscoreSplitter($input), + default => new UppercaseSplitter($input), + }; } /** @@ -279,33 +242,14 @@ protected function extractWords(Splitter $splitter): self */ protected function handleSplitterMethod(string $methodName): self { - switch ($methodName) { - case 'fromCamel': - case 'fromPascal': - $splitterName = UppercaseSplitter::class; - break; - case 'fromSnake': - case 'fromAda': - case 'fromMacro': - $splitterName = UnderscoreSplitter::class; - break; - case 'fromKebab': - case 'fromTrain': - case 'fromCobol': - $splitterName = DashSplitter::class; - break; - case 'fromLower': - case 'fromUpper': - case 'fromTitle': - case 'fromSentence': - $splitterName = SpaceSplitter::class; - break; - case 'fromDot': - $splitterName = DotSplitter::class; - break; - default: - throw new CaseConverterException("Unknown method: $methodName"); - } + $splitterName = match ($methodName) { + 'fromCamel', 'fromPascal' => UppercaseSplitter::class, + 'fromSnake', 'fromAda', 'fromMacro' => UnderscoreSplitter::class, + 'fromKebab', 'fromTrain', 'fromCobol' => DashSplitter::class, + 'fromLower', 'fromUpper', 'fromTitle', 'fromSentence' => SpaceSplitter::class, + 'fromDot' => DotSplitter::class, + default => throw new CaseConverterException("Unknown method: $methodName"), + }; $splitter = $this->createSplitter($splitterName, $this->source); $this->extractWords($splitter); @@ -336,49 +280,22 @@ protected function createSplitter(string $className, string $source): Splitter */ protected function handleGluerMethod(string $methodName): string { - switch ($methodName) { - case 'toAda': - $className = AdaCase::class; - break; - case 'toCamel': - $className = CamelCase::class; - break; - case 'toCobol': - $className = CobolCase::class; - break; - case 'toKebab': - $className = KebabCase::class; - break; - case 'toLower': - $className = LowerCase::class; - break; - case 'toMacro': - $className = MacroCase::class; - break; - case 'toPascal': - $className = PascalCase::class; - break; - case 'toSentence': - $className = SentenceCase::class; - break; - case 'toSnake': - $className = SnakeCase::class; - break; - case 'toTitle': - $className = TitleCase::class; - break; - case 'toTrain': - $className = TrainCase::class; - break; - case 'toUpper': - $className = UpperCase::class; - break; - case 'toDot': - $className = DotNotation::class; - break; - default: - throw new CaseConverterException("Unknown method: $methodName"); - } + $className = match ($methodName) { + 'toAda' => AdaCase::class, + 'toCamel' => CamelCase::class, + 'toCobol' => CobolCase::class, + 'toKebab' => KebabCase::class, + 'toLower' => LowerCase::class, + 'toMacro' => MacroCase::class, + 'toPascal' => PascalCase::class, + 'toSentence' => SentenceCase::class, + 'toSnake' => SnakeCase::class, + 'toTitle' => TitleCase::class, + 'toTrain' => TrainCase::class, + 'toUpper' => UpperCase::class, + 'toDot' => DotNotation::class, + default => throw new CaseConverterException("Unknown method: $methodName"), + }; $gluer = $this->createGluer($className, $this->words, $this->forceSimpleCaseMapping); diff --git a/src/Glue/AdaCase.php b/src/Glue/AdaCase.php index 857088f..801cdef 100644 --- a/src/Glue/AdaCase.php +++ b/src/Glue/AdaCase.php @@ -6,8 +6,6 @@ * Class AdaCase * * Outputs string in _Ada case_ format: This_Is_Ada_Case - * - * @package Jawira\CaseConverter\Glue */ class AdaCase extends UnderscoreGluer { diff --git a/src/Glue/CamelCase.php b/src/Glue/CamelCase.php index 9f13b8a..95b7d0b 100644 --- a/src/Glue/CamelCase.php +++ b/src/Glue/CamelCase.php @@ -6,8 +6,6 @@ * Class CamelCase * * Outputs string in _Camel case_ format: thisIsCamelCase - * - * @package Jawira\CaseConverter\Glue */ class CamelCase extends UppercaseGluer { diff --git a/src/Glue/CobolCase.php b/src/Glue/CobolCase.php index 160079a..da0da17 100644 --- a/src/Glue/CobolCase.php +++ b/src/Glue/CobolCase.php @@ -6,8 +6,6 @@ * Class CobolCase * * Outputs string in _Cobol case_ format: THIS-IS-COBOL-CASE - * - * @package Jawira\CaseConverter\Glue */ class CobolCase extends DashGluer { diff --git a/src/Glue/DashGluer.php b/src/Glue/DashGluer.php index 86270a2..adbfeab 100644 --- a/src/Glue/DashGluer.php +++ b/src/Glue/DashGluer.php @@ -4,5 +4,6 @@ abstract class DashGluer extends Gluer { - const DELIMITER = '-'; + /** @internal */ + public const DELIMITER = '-'; } diff --git a/src/Glue/DotNotation.php b/src/Glue/DotNotation.php index 46a4246..c31e8ba 100644 --- a/src/Glue/DotNotation.php +++ b/src/Glue/DotNotation.php @@ -9,7 +9,8 @@ */ class DotNotation extends Gluer { - const DELIMITER = '.'; + /** @internal */ + public const DELIMITER = '.'; /** * Format detected words in _dot notation_ diff --git a/src/Glue/Gluer.php b/src/Glue/Gluer.php index 7a973c8..9c58de0 100644 --- a/src/Glue/Gluer.php +++ b/src/Glue/Gluer.php @@ -12,10 +12,9 @@ /** * Class Gluer * - * A Gluer sub-class allow to export an array of words in a single string + * A Gluer subclass allow to export an array of words in a single string * * @author Jawira Portugal - * @package Jawira\CaseConverter\Glue */ abstract class Gluer { @@ -24,27 +23,27 @@ abstract class Gluer * * This value should never change. */ - const ENCODING = 'UTF-8'; + protected const ENCODING = 'UTF-8'; /** * @var string[] Words extracted from input string */ - protected $words; + protected array $words; /** * @var int MB_CASE_LOWER or MB_CASE_LOWER_SIMPLE */ - protected $lowerCase; + protected int $lowerCase; /** * @var int MB_CASE_UPPER or MB_CASE_UPPER_SIMPLE */ - protected $upperCase; + protected int $upperCase; /** * @var int MB_CASE_TITLE or MB_CASE_TITLE_SIMPLE */ - protected $titleCase; + protected int $titleCase; /** @@ -56,13 +55,9 @@ abstract class Gluer final public function __construct(array $words, bool $forceSimpleCaseMapping) { $this->words = $words; - $this->lowerCase = MB_CASE_LOWER; - $this->upperCase = MB_CASE_UPPER; - $this->titleCase = MB_CASE_TITLE; - - if ($forceSimpleCaseMapping) { - $this->setSimpleCaseMappingConstants(); - } + $this->lowerCase = $forceSimpleCaseMapping ? MB_CASE_LOWER_SIMPLE : MB_CASE_LOWER; + $this->upperCase = $forceSimpleCaseMapping ? MB_CASE_UPPER_SIMPLE : MB_CASE_UPPER; + $this->titleCase = $forceSimpleCaseMapping ? MB_CASE_TITLE_SIMPLE : MB_CASE_TITLE; } /** @@ -72,55 +67,16 @@ final public function __construct(array $words, bool $forceSimpleCaseMapping) */ abstract public function glue(): string; - /** - * Use new constants if available - * - * Since PHP 7.3, new constants are used to specify _simple case mapping_. This method handles these new constants. - * - * Usually you would use: - * - * - MB_CASE_LOWER - * - MB_CASE_UPPER - * - MB_CASE_TITLE - * - * But PHP 7.3 introduced new constants: - * - * - MB_CASE_LOWER_SIMPLE - * - MB_CASE_UPPER_SIMPLE - * - MB_CASE_TITLE_SIMPLE - * - * @see https://www.php.net/manual/en/migration73.constants.php#migration73.constants.mbstring - * @see https://www.php.net/manual/en/migration73.new-features.php#migration73.new-features.mbstring.case-mapping-folding - */ - protected function setSimpleCaseMappingConstants(): self - { - /** @var int $lowerCase */ - $lowerCase = defined('\MB_CASE_LOWER_SIMPLE') ? constant('\MB_CASE_LOWER_SIMPLE') : MB_CASE_LOWER; - /** @var int $upperCase */ - $upperCase = defined('\MB_CASE_UPPER_SIMPLE') ? constant('\MB_CASE_UPPER_SIMPLE') : MB_CASE_UPPER; - /** @var int $titleCase */ - $titleCase = defined('\MB_CASE_TITLE_SIMPLE') ? constant('\MB_CASE_TITLE_SIMPLE') : MB_CASE_TITLE; - - $this->lowerCase = $lowerCase; - $this->upperCase = $upperCase; - $this->titleCase = $titleCase; - - return $this; - } - /** * Implode self::$words array using $glue. * - * @param string $glue Character to glue words. Even if is assumed your are using underscore or dash - * character, this method should be capable to use any character as glue. - * @param int $wordsMode The mode of the conversion. It should be one of `Gluer::$lowerCase`, - * `Gluer::$upperCase` or `Gluer::$titleCase`. - * @param int $firstWordMode Sometimes first word requires special treatment. It should be one of - * `Gluer::$lowerCase`, `Gluer::$upperCase` or `Gluer::$titleCase`. + * @param string $glue Character to glue words. Even if is assumed your are using underscore or dash character, this method should be capable to use any character as glue. + * @param int $wordsMode The mode of the conversion. It should be one of `Gluer::$lowerCase`, `Gluer::$upperCase` or `Gluer::$titleCase`. + * @param null|int $firstWordMode Sometimes first word requires special treatment. It should be one of `Gluer::$lowerCase`, `Gluer::$upperCase` or `Gluer::$titleCase`. * * @return string */ - protected function glueUsingRules(string $glue, int $wordsMode, int $firstWordMode = null): string + protected function glueUsingRules(string $glue, int $wordsMode, ?int $firstWordMode = null): string { $convertedWords = $this->changeWordsCase($this->words, $wordsMode); @@ -149,9 +105,7 @@ protected function changeWordsCase(array $words, int $caseMode): array return mb_convert_case($word, $caseMode, self::ENCODING); }; - $convertedWords = array_map($closure, $words); - - return $convertedWords; + return array_map($closure, $words); } /** diff --git a/src/Glue/SpaceGluer.php b/src/Glue/SpaceGluer.php index b78bd78..44a978a 100644 --- a/src/Glue/SpaceGluer.php +++ b/src/Glue/SpaceGluer.php @@ -4,5 +4,6 @@ abstract class SpaceGluer extends Gluer { - const DELIMITER = ' '; + /** @internal */ + public const DELIMITER = ' '; } diff --git a/src/Glue/UnderscoreGluer.php b/src/Glue/UnderscoreGluer.php index 6304ae9..21046b0 100644 --- a/src/Glue/UnderscoreGluer.php +++ b/src/Glue/UnderscoreGluer.php @@ -4,5 +4,6 @@ abstract class UnderscoreGluer extends Gluer { - const DELIMITER = '_'; + /** @internal */ + public const DELIMITER = '_'; } diff --git a/src/Glue/UppercaseGluer.php b/src/Glue/UppercaseGluer.php index 6c26503..57bac00 100644 --- a/src/Glue/UppercaseGluer.php +++ b/src/Glue/UppercaseGluer.php @@ -4,5 +4,6 @@ abstract class UppercaseGluer extends Gluer { - const DELIMITER = ''; + /** @internal */ + public const DELIMITER = ''; } diff --git a/src/Split/DashSplitter.php b/src/Split/DashSplitter.php index 0a5be47..1f547b3 100644 --- a/src/Split/DashSplitter.php +++ b/src/Split/DashSplitter.php @@ -6,7 +6,8 @@ class DashSplitter extends Splitter { - const PATTERN = '#' . DashGluer::DELIMITER . '+#u'; + /** @internal */ + public const PATTERN = '#' . DashGluer::DELIMITER . '+#u'; /** * @return string[] diff --git a/src/Split/DotSplitter.php b/src/Split/DotSplitter.php index 0048d78..853c8ce 100644 --- a/src/Split/DotSplitter.php +++ b/src/Split/DotSplitter.php @@ -3,11 +3,11 @@ namespace Jawira\CaseConverter\Split; use Jawira\CaseConverter\Glue\DotNotation; -use Jawira\CaseConverter\Glue\SpaceGluer; class DotSplitter extends Splitter { - const PATTERN = '#\\' . DotNotation::DELIMITER . '+#u'; + /** @internal */ + public const PATTERN = '#\\' . DotNotation::DELIMITER . '+#u'; /** * @return string[] diff --git a/src/Split/SpaceSplitter.php b/src/Split/SpaceSplitter.php index d33e110..eaa6e32 100644 --- a/src/Split/SpaceSplitter.php +++ b/src/Split/SpaceSplitter.php @@ -6,7 +6,8 @@ class SpaceSplitter extends Splitter { - const PATTERN = '#' . SpaceGluer::DELIMITER . '+#u'; + /** @internal */ + public const PATTERN = '#' . SpaceGluer::DELIMITER . '+#u'; /** * @return string[] diff --git a/src/Split/Splitter.php b/src/Split/Splitter.php index e1cc20b..46119ba 100644 --- a/src/Split/Splitter.php +++ b/src/Split/Splitter.php @@ -7,17 +7,16 @@ /** * Class Splitter * - * A Splitter sub-class allows to read the words contained in a string + * A Splitter subclass allows to read the words contained in a string * * @author Jawira Portugal - * @package Jawira\CaseConverter\Split */ abstract class Splitter { /** * @var string Words extracted from input string */ - protected $inputString; + protected string $inputString; final public function __construct(string $inputString) { @@ -32,7 +31,7 @@ final public function __construct(string $inputString) abstract public function split(): array; /** - * This is an utility method, typically this method is used by to split a string based on pattern. + * This is a utility method, typically this method is used by to split a string based on pattern. * * @param string $inputString * @param string $pattern @@ -45,7 +44,7 @@ protected function splitUsingPattern(string $inputString, string $pattern): arra $words = preg_split($pattern, $inputString, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); if ($words === false) { - throw new CaseConverterException("Error while processing '{$this->inputString}'"); // @codeCoverageIgnore + throw new CaseConverterException("Error while processing $this->inputString"); // @codeCoverageIgnore } return $words; diff --git a/src/Split/UnderscoreSplitter.php b/src/Split/UnderscoreSplitter.php index 5524076..7d3d3bd 100644 --- a/src/Split/UnderscoreSplitter.php +++ b/src/Split/UnderscoreSplitter.php @@ -6,7 +6,7 @@ class UnderscoreSplitter extends Splitter { - const PATTERN = '#' . UnderscoreGluer::DELIMITER . '+#u'; + public const PATTERN = '#' . UnderscoreGluer::DELIMITER . '+#u'; /** * @return string[] diff --git a/src/Split/UppercaseSplitter.php b/src/Split/UppercaseSplitter.php index e8c8af6..3683268 100644 --- a/src/Split/UppercaseSplitter.php +++ b/src/Split/UppercaseSplitter.php @@ -5,7 +5,7 @@ class UppercaseSplitter extends Splitter { // language=PhpRegExp - const PATTERN = '#(?=\p{Lu}{1})#u'; + public const PATTERN = '#(?=\p{Lu}{1})#u'; /** * Splits $words using Uppercase letters. diff --git a/tests/phpunit/CaseConverterTest.php b/tests/phpunit/CaseConverterTest.php index 8e0f39e..adade50 100644 --- a/tests/phpunit/CaseConverterTest.php +++ b/tests/phpunit/CaseConverterTest.php @@ -11,7 +11,6 @@ class CaseConverterTest extends TestCase * * @covers \Jawira\CaseConverter\Convert::__construct * @covers \Jawira\CaseConverter\Convert::analyse - * @covers \Jawira\CaseConverter\Convert::contains * @covers \Jawira\CaseConverter\Convert::extractWords * @covers \Jawira\CaseConverter\Convert::fromAuto * @covers \Jawira\CaseConverter\Convert::getSource diff --git a/tests/phpunit/ConvertTest.php b/tests/phpunit/ConvertTest.php index 6804c26..b055e0d 100644 --- a/tests/phpunit/ConvertTest.php +++ b/tests/phpunit/ConvertTest.php @@ -85,19 +85,18 @@ public function isUppercaseWordProvider() * * @dataProvider analyseProvider * - * @param bool[] $containsReturnValues Return values for `contains()` * @param bool $isUppercaseWordReturn Return value for `isUppercaseWord()` * @param string $expected Expected result * @param string $input Input string * * @throws \ReflectionException */ - public function testAnalyse(array $containsReturnValues, bool $isUppercaseWordReturn, string $expected, string $input) + public function testAnalyse(bool $isUppercaseWordReturn, string $expected, string $input) { // Disabling constructor with one stub method $stub = $this->getMockBuilder(Convert::class) ->disableOriginalConstructor() - ->setMethods(['isUppercaseWord', 'contains']) + ->setMethods(['isUppercaseWord']) ->getMock(); // Configuring expectation @@ -105,10 +104,6 @@ public function testAnalyse(array $containsReturnValues, bool $isUppercaseWordRe ->method('isUppercaseWord') ->willReturn($isUppercaseWordReturn); - // Configuring expectation - $stub->expects($this->atLeastOnce()) - ->method('contains') - ->willReturnOnConsecutiveCalls(...$containsReturnValues); // Removing protected for analyse method $reflection = new ReflectionObject($stub); @@ -123,30 +118,30 @@ public function testAnalyse(array $containsReturnValues, bool $isUppercaseWordRe public function analyseProvider() { return [ - 'Underscore 1' => [[true, false, false, false], false, UnderscoreSplitter::class, 'hola_mundo'], - 'Underscore 2' => [[true, false, false, false], false, UnderscoreSplitter::class, 'HELLO_WORLD'], - 'Underscore 3' => [[false, false, false, false], true, UnderscoreSplitter::class, 'Ñ'], - 'Underscore 4' => [[false, false, false, false], true, UnderscoreSplitter::class, 'HELLO'], - 'Underscore 5' => [[true, false, false, false], false, UnderscoreSplitter::class, '_'], - 'Underscore 6' => [[true, false, false, false], false, UnderscoreSplitter::class, '_____'], - 'Uppercase 1' => [[false, false, false, false], false, UppercaseSplitter::class, ''], - 'Uppercase 2' => [[false, false, false, false], false, UppercaseSplitter::class, 'ñ'], - 'Uppercase 3' => [[false, false, false, false], false, UppercaseSplitter::class, 'one'], - 'Uppercase 4' => [[false, false, false, false], false, UppercaseSplitter::class, 'helloWorld'], - 'Dash 1' => [[false, true, false, false], false, DashSplitter::class, 'hello-World'], - 'Dash 2' => [[false, true, false, false], false, DashSplitter::class, 'my-name-is-bond'], - 'Dash 3' => [[false, true, false, false], false, DashSplitter::class, '-my-name-is-bond-'], - 'Dash 4' => [[false, true, false, false], false, DashSplitter::class, '-'], - 'Dash 5' => [[false, true, false, false], false, DashSplitter::class, '------'], - 'Space 1' => [[false, false, true, false], false, SpaceSplitter::class, 'Hola mundo'], - 'Space 2' => [[false, false, true, false], false, SpaceSplitter::class, 'Mi nombre es bond'], - 'Space 3' => [[false, false, true, false], false, SpaceSplitter::class, 'Formule courte spéciale été'], - 'Space 4' => [[false, false, true, false], false, SpaceSplitter::class, ' '], - 'Space 5' => [[false, false, true, false], false, SpaceSplitter::class, ' '], - 'Dot 1' => [[false, false, false, true], false, DotSplitter::class, 'one.two'], - 'Dot 2' => [[false, false, false, true], false, DotSplitter::class, '.hello.'], - 'Dot 3' => [[false, false, false, true], false, DotSplitter::class, '.'], - 'Dot 4' => [[false, false, false, true], false, DotSplitter::class, '........'], + 'Underscore 1' => [false, UnderscoreSplitter::class, 'hola_mundo'], + 'Underscore 2' => [false, UnderscoreSplitter::class, 'HELLO_WORLD'], + 'Underscore 3' => [true, UnderscoreSplitter::class, 'Ñ'], + 'Underscore 4' => [true, UnderscoreSplitter::class, 'HELLO'], + 'Underscore 5' => [false, UnderscoreSplitter::class, '_'], + 'Underscore 6' => [false, UnderscoreSplitter::class, '_____'], + 'Uppercase 1' => [false, UppercaseSplitter::class, ''], + 'Uppercase 2' => [false, UppercaseSplitter::class, 'ñ'], + 'Uppercase 3' => [false, UppercaseSplitter::class, 'one'], + 'Uppercase 4' => [false, UppercaseSplitter::class, 'helloWorld'], + 'Dash 1' => [false, DashSplitter::class, 'hello-World'], + 'Dash 2' => [false, DashSplitter::class, 'my-name-is-bond'], + 'Dash 3' => [false, DashSplitter::class, '-my-name-is-bond-'], + 'Dash 4' => [false, DashSplitter::class, '-'], + 'Dash 5' => [false, DashSplitter::class, '------'], + 'Space 1' => [false, SpaceSplitter::class, 'Hola mundo'], + 'Space 2' => [false, SpaceSplitter::class, 'Mi nombre es bond'], + 'Space 3' => [false, SpaceSplitter::class, 'Formule courte spéciale été'], + 'Space 4' => [false, SpaceSplitter::class, ' '], + 'Space 5' => [false, SpaceSplitter::class, ' '], + 'Dot 1' => [false, DotSplitter::class, 'one.two'], + 'Dot 2' => [false, DotSplitter::class, '.hello.'], + 'Dot 3' => [false, DotSplitter::class, '.'], + 'Dot 4' => [false, DotSplitter::class, '........'], ]; }