From 8181383a8a67c73e5d0e16475d72cf60a8f28a78 Mon Sep 17 00:00:00 2001 From: neveldo Date: Sun, 20 Oct 2024 18:00:17 +0200 Subject: [PATCH] chore: update PHP version, update dependencies, improve code quality --- .gitattributes | 5 +- .gitignore | 3 +- Dockerfile | 12 ++ README.md | 52 ++++--- composer.json | 10 +- phpunit.xml.dist | 6 - sample/src/api.php | 5 +- sample/src/run.php | 38 +++--- .../ExpressionLanguage/ExpressionLanguage.php | 6 +- src/TextGenerator/Tag/TagReplacer.php | 64 ++++----- .../Tag/TagReplacerInterface.php | 36 +++-- .../TextFunction/ChooseFunction.php | 25 ++-- .../TextFunction/CoalesceFunction.php | 22 +-- .../TextFunction/ExprFunction.php | 30 ++--- .../TextFunction/FilterFunction.php | 82 +++++------ .../TextFunction/FunctionInterface.php | 12 +- src/TextGenerator/TextFunction/IfFunction.php | 31 ++--- .../TextFunction/LoopFunction.php | 36 ++--- .../ProbabilityRandomFunction.php | 42 +++--- .../TextFunction/RandomFunction.php | 26 +--- .../ReproducableRandomFunction.php | 99 ++++++-------- .../TextFunction/RmnaFunction.php | 26 ++-- .../TextFunction/SetFunction.php | 25 ++-- .../TextFunction/ShuffleFunction.php | 28 ++-- src/TextGenerator/TextGenerator.php | 127 +++++++----------- tests/TextGenerator/Tag/TagReplacerTest.php | 54 ++++---- .../TextFunction/ChooseFunctionTest.php | 42 +++--- .../TextFunction/CoalesceFunctionTest.php | 37 +++-- .../TextFunction/ExprFunctionTest.php | 30 +++-- .../TextFunction/FilterFunctionTest.php | 46 ++++--- .../TextFunction/IfFunctionTest.php | 42 +++--- .../TextFunction/LoopFunctionTest.php | 78 ++++++----- .../ProbabilityRandomFunctionTest.php | 52 +++---- .../TextFunction/RandomFunctionTest.php | 29 ++-- .../TextFunction/SetFunctionTest.php | 21 +-- .../TextFunction/ShuffleFunctionTest.php | 46 ++++--- tests/TextGenerator/TextGeneratorTest.php | 44 +++--- 37 files changed, 641 insertions(+), 728 deletions(-) create mode 100644 Dockerfile diff --git a/.gitattributes b/.gitattributes index ca794a7..bbd545f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,4 @@ -/tests/ export-ignore \ No newline at end of file +/tests/ export-ignore +/sample/ export-ignore +/phpunit.xml.dist export-ignore +/Dockerfile export-ignore \ No newline at end of file diff --git a/.gitignore b/.gitignore index 63e1b93..fabae1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor/ .idea/ .DS_Store -composer.lock \ No newline at end of file +composer.lock +.phpunit.result.cache \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e069f9f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM php:8.2-cli + +RUN apt-get update && apt-get install -y \ + unzip \ + git \ + && rm -rf /var/lib/apt/lists/* + +COPY --from=composer:2 /usr/bin/composer /usr/bin/composer + +WORKDIR /app + +CMD ["bash"] \ No newline at end of file diff --git a/README.md b/README.md index 2c49f93..1393fc4 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ Data : [ [ - 'my_choice' => 2, + 'my_choice' => '2', ] ] @@ -206,7 +206,7 @@ Data : [ [ 'my_tag1' => '', - 'my_tag2' => null, + 'my_tag2' => '', 'my_tag3' => 'Hello', 'my_tag4' => 'Hi', ] @@ -270,26 +270,26 @@ Data : 'nationality' => 'American', 'birth_city' => 'Hollywood', 'birth_country' => 'US', - 'awards_number' => 1, - 'nominations_number' => 6, - 'movies_number' => 37, + 'awards_number' => '1', + 'nominations_number' => '6', + 'movies_number' => '37', 'first_movie_name' => 'Critters 3', - 'first_movie_year' => 1991, + 'first_movie_year' => '1991', 'famous_movie_name' => 'Titanic', - 'famous_movie_year' => 1997, + 'famous_movie_year' => '1997', 'famous_movie_earn' => '2185372302', 'other_famous_movies' => [ [ 'name' => 'Catch Me If You Can', - 'year' => 2002 + 'year' => '2002' ], [ 'name' => 'Shutter Island', - 'year' => 2010 + 'year' => '2010' ], [ 'name' => 'Inception', - 'year' => 2010 + 'year' => '2010' ], ] ], @@ -297,31 +297,31 @@ Data : 'firstname' => 'Jodie', 'lastname' => 'Foster', 'birthdate' => 'November 19, 1962', - 'age' => 51, + 'age' => '51', 'sex' => 'f', 'nationality' => 'American', 'birth_city' => 'Los Angeles', 'birth_country' => 'US', - 'awards_number' => 2, - 'nominations_number' => 4, - 'movies_number' => 75, + 'awards_number' => '2', + 'nominations_number' => '4', + 'movies_number' => '75', 'first_movie_name' => 'My Sister Hank', - 'first_movie_year' => 1972, + 'first_movie_year' => '1972', 'famous_movie_name' => 'Taxi Driver', - 'famous_movie_year' => 1976, + 'famous_movie_year' => '1976', 'famous_movie_earn' => '28262574', 'other_famous_movies' => [ [ 'name' => 'The Silence of the Lambs', - 'year' => 1991 + 'year' => '1991' ], [ 'name' => 'Contact', - 'year' => null // Empty values are skipped by the parser + 'year' => '' // Empty values are skipped by the parser ], [ 'name' => 'The Accused', - 'year' => 1988 + 'year' => '1988' ], ] ], @@ -340,3 +340,17 @@ You can extend the TextGenerator capabilities by adding your own text funtions. ## Install $ composer require neveldo/text-generator + +## Running tests and linters + +``` +docker build -t text-generator . +docker run -it --rm -v $(pwd):/app text-generator +``` + +``` +composer install +vendor/bin/phpunit --display-deprecations +vendor/bin/phpstan analyse src tests sample -l 9 +vendor/bin/php-cs-fixer fix src +``` \ No newline at end of file diff --git a/composer.json b/composer.json index 7443090..cf5b0d8 100644 --- a/composer.json +++ b/composer.json @@ -4,12 +4,14 @@ "type": "library", "license": "MIT", "require": { - "php": ">=8.1.0", - "symfony/expression-language": "^3.0|^4.0", - "ruafozy/mersenne-twister": "^2" + "php": ">=8.2.0", + "symfony/expression-language": "^6.0|^7.0", + "ruafozy/mersenne-twister": "^2.0" }, "require-dev": { - "phpunit/phpunit": "^6.0" + "phpunit/phpunit": "^11.0", + "phpstan/phpstan": "^1.12", + "friendsofphp/php-cs-fixer": "^3.64" }, "autoload": { "psr-4" : { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fad2b9c..79b2f74 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,14 +2,8 @@ tests - - - ./src - - \ No newline at end of file diff --git a/sample/src/api.php b/sample/src/api.php index 55e78ee..e08d91f 100644 --- a/sample/src/api.php +++ b/sample/src/api.php @@ -46,13 +46,14 @@ $textGenerator->compile($_POST['template']); try { - $data = json_decode($_POST['data'], true); + /** @var array> */ + $data = json_decode((string) $_POST['data'], true); foreach($data as $row) { $output['result'][] = $textGenerator->generate($row); } - } catch (\Exception $e) { + } catch (Exception $e) { $output['error'] = $e->getMessage(); } } diff --git a/sample/src/run.php b/sample/src/run.php index 3160ccf..02f07fb 100644 --- a/sample/src/run.php +++ b/sample/src/run.php @@ -31,31 +31,31 @@ 'firstname' => 'Leonardo', 'lastname' => 'DiCaprio', 'birthdate' => 'November 11, 1974', - 'age' => 41, + 'age' => '41', 'sex' => 'm', 'nationality' => 'American', 'birth_city' => 'Hollywood', 'birth_country' => 'US', - 'awards_number' => 1, - 'nominations_number' => 6, - 'movies_number' => 37, + 'awards_number' => '1', + 'nominations_number' => '6', + 'movies_number' => '37', 'first_movie_name' => 'Critters 3', - 'first_movie_year' => 1991, + 'first_movie_year' => '1991', 'famous_movie_name' => 'Titanic', - 'famous_movie_year' => 1997, + 'famous_movie_year' => '1997', 'famous_movie_earn' => '2185372302', 'other_famous_movies' => [ [ 'name' => 'Catch Me If You Can', - 'year' => 2002 + 'year' => '2002' ], [ 'name' => 'Shutter Island', - 'year' => 2010 + 'year' => '2010' ], [ 'name' => 'Inception', - 'year' => 2010 + 'year' => '2010' ], ] ], @@ -63,31 +63,31 @@ 'firstname' => 'Jodie', 'lastname' => 'Foster', 'birthdate' => 'November 19, 1962', - 'age' => 51, + 'age' => '51', 'sex' => 'f', 'nationality' => 'American', 'birth_city' => 'Los Angeles', 'birth_country' => 'US', - 'awards_number' => 2, - 'nominations_number' => 4, - 'movies_number' => 75, + 'awards_number' => '2', + 'nominations_number' => '4', + 'movies_number' => '75', 'first_movie_name' => 'My Sister Hank', - 'first_movie_year' => 1972, + 'first_movie_year' => '1972', 'famous_movie_name' => 'Taxi Driver', - 'famous_movie_year' => 1976, + 'famous_movie_year' => '1976', 'famous_movie_earn' => '28262574', 'other_famous_movies' => [ [ 'name' => 'The Silence of the Lambs', - 'year' => 1991 + 'year' => '1991' ], [ 'name' => 'Contact', - 'year' => null // Empty values are skipped by the parser + 'year' => '' // Empty values are skipped by the parser ], [ 'name' => 'The Accused', - 'year' => 1988 + 'year' => '1988' ], ] ], @@ -98,4 +98,4 @@ foreach($data as $row) { echo $textGenerator->generate($row) . "\n\n"; -} \ No newline at end of file +} diff --git a/src/TextGenerator/ExpressionLanguage/ExpressionLanguage.php b/src/TextGenerator/ExpressionLanguage/ExpressionLanguage.php index 2d92144..e83b76b 100644 --- a/src/TextGenerator/ExpressionLanguage/ExpressionLanguage.php +++ b/src/TextGenerator/ExpressionLanguage/ExpressionLanguage.php @@ -3,9 +3,9 @@ namespace Neveldo\TextGenerator\ExpressionLanguage; /** - * Class TagReplacerInterface + * Class ExpressionLanguage * Handle tags replacement within a text - * @package Neveldo\TextGenerator\Tag + * @package Neveldo\TextGenerator\ExpressionLanguage */ class ExpressionLanguage extends \Symfony\Component\ExpressionLanguage\ExpressionLanguage { @@ -14,4 +14,4 @@ protected function registerFunctions() // Prevent from registering constant() function by default,it could be a security issue ... return; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/Tag/TagReplacer.php b/src/TextGenerator/Tag/TagReplacer.php index 473be2a..efb963c 100644 --- a/src/TextGenerator/Tag/TagReplacer.php +++ b/src/TextGenerator/Tag/TagReplacer.php @@ -12,107 +12,98 @@ class TagReplacer implements TagReplacerInterface /** * @const string the empty tag */ - const EMPTY_TAG = '[EMPTY]'; + public const EMPTY_TAG = '[EMPTY]'; /** - * @var array tags, format : ['tag_name' => 'value', ...] + * @var array>> tags, format : ['tag_name' => 'value', ...] */ - private $tags = []; + private array $tags = []; /** - * @var array escaped tags to use for text replacement + * @var array escaped tags to use for text replacement * format : ['@tag_name' => 'value', ...] */ - private $escapedTags = []; + private array $escapedTags = []; /** - * @var array reversed tags to use for text replacement + * @var array reversed tags to use for text replacement * format : ['@tag_name' => 'tag_name', ...] */ - private $sanitizedTagNames = []; + private array $sanitizedTagNames = []; /** * Initialize the tags list - * @param array $tags, format : ['tag_name' => 'value', ...] + * @param array>> $tags, format : ['tag_name' => 'value', ...] */ - public function setTags(array $tags) + public function setTags(array $tags): void { $this->tags = $this->escapedTags = []; - foreach($tags as $name => $value) { + foreach ($tags as $name => $value) { $this->addTag($name, $value); } } /** * Add a tag to the collection - * @param string $name the tag name - * @param string $value the tag value + * @param string|array> $value */ - public function addTag($name, $value) + public function addTag(string $name, string|array $value): void { $this->tags[$name] = $value; $this->sanitizedTagNames['@' . $name] = $name; // Replace empty values by [EMPTY_TAG] in order to be able to remove easily // the parts that contains this tag later to avoid inconsistent sentences - if ($value === null || $value === '') { + if ($value === '') { $value = $this->getEmptyTag(); } - if (is_scalar($value)) { + if (is_string($value)) { $this->escapedTags['@' . $name] = $value; } } /** * Replace tags by the matching values within the content - * @param string $content - * @return string */ - public function replace($content) + public function replace(string $content): string { return strtr($content, $this->escapedTags); } /** * Replace tags by the matching sanitized tag names - * @param string $content - * @return string */ - public function sanitizeTagNames($content) + public function sanitizeTagNames(string $content): string { return strtr($content, $this->sanitizedTagNames); } /** - * Return a tag by its name - * @param $name ex : '@tag_name' - * @return string|array + * Return a tag by its name. ex : '@tag_name' + * @return string|array>|null */ - public function getTag($name) + public function getTag(string $name): string|array|null { - $name = mb_substr($name, 1); - if (isset($this->tags[$name])) { - return $this->tags[$name]; - } - return null; + $name = mb_substr((string) $name, 1); + return $this->tags[$name] ?? null; } /** * Return the array of available tags - * @return array + * @return array>> */ - public function getTags() + public function getTags(): array { return $this->tags; } /** * Return the array of escaped tags - * @return array + * @return array */ - public function getEscapedTags() + public function getEscapedTags(): array { return $this->escapedTags; } @@ -120,7 +111,8 @@ public function getEscapedTags() /** * @return string the empty tag */ - public function getEmptyTag() { + public function getEmptyTag(): string + { return self::EMPTY_TAG; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/Tag/TagReplacerInterface.php b/src/TextGenerator/Tag/TagReplacerInterface.php index 9c8612a..35e7f7a 100644 --- a/src/TextGenerator/Tag/TagReplacerInterface.php +++ b/src/TextGenerator/Tag/TagReplacerInterface.php @@ -11,52 +11,46 @@ interface TagReplacerInterface { /** * Initialize the tags list - * @param array $tags, format : ['tag_name' => 'value', ...] + * @param array>> $tags, format : ['tag_name' => 'value', ...] */ - public function setTags(array $tags); + public function setTags(array $tags): void; /** * Add a tag to the collection - * @param string $name the tag name - * @param string $value the tag value + * @param string|array> $value */ - public function addTag($name, $value); + public function addTag(string $name, string|array $value): void; /** * Replace tags by the matching values within the content - * @param string $content - * @return string */ - public function replace($content); + public function replace(string $content): string; /** * Replace tags by the matching sanitized tag names - * @param string $content - * @return string */ - public function sanitizeTagNames($content); + public function sanitizeTagNames(string $content): string; /** - * Return a tag by its name - * @param $name - * @return string|array + * Return a tag by its name. ex : '@tag_name' + * @return string|array>|null */ - public function getTag($name); + public function getTag(string $name): string|array|null; /** * Return the array of available tags - * @return array + * @return array>> */ - public function getTags(); + public function getTags(): array; /** * Return the array of escaped tags - * @return array + * @return array */ - public function getEscapedTags(); + public function getEscapedTags(): array; /** * @return string the empty tag */ - public function getEmptyTag(); -} \ No newline at end of file + public function getEmptyTag(): string; +} diff --git a/src/TextGenerator/TextFunction/ChooseFunction.php b/src/TextGenerator/TextFunction/ChooseFunction.php index a06d318..7df469f 100644 --- a/src/TextGenerator/TextFunction/ChooseFunction.php +++ b/src/TextGenerator/TextFunction/ChooseFunction.php @@ -2,6 +2,7 @@ namespace Neveldo\TextGenerator\TextFunction; +use InvalidArgumentException; use Neveldo\TextGenerator\Tag\TagReplacer; use Neveldo\TextGenerator\Tag\TagReplacerInterface; @@ -15,30 +16,20 @@ */ class ChooseFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * ChooseFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle choose function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments * @return string */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { if (count($arguments) < 2) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("ChooseFunction expect at least two parameters, %d given.", count($arguments)) ); } @@ -47,7 +38,7 @@ public function execute(array $arguments, array $originalArguments) if ($index !== 0 && isset($arguments[$index]) - && (mb_strpos($arguments[$index], $this->tagReplacer->getEmptyTag()) === false) + && (mb_strpos((string) $arguments[$index], $this->tagReplacer->getEmptyTag()) === false) ) { return $arguments[$index]; } @@ -55,4 +46,4 @@ public function execute(array $arguments, array $originalArguments) return TagReplacer::EMPTY_TAG; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/CoalesceFunction.php b/src/TextGenerator/TextFunction/CoalesceFunction.php index 0cad68e..e7452a0 100644 --- a/src/TextGenerator/TextFunction/CoalesceFunction.php +++ b/src/TextGenerator/TextFunction/CoalesceFunction.php @@ -12,29 +12,19 @@ */ class CoalesceFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * ChooseFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle coalesce function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments * @return string */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { - foreach($arguments as $argument) { + foreach ($arguments as $argument) { if ($argument !== $this->tagReplacer->getEmptyTag() && $argument !== null && $argument !== '' @@ -46,4 +36,4 @@ public function execute(array $arguments, array $originalArguments) return TagReplacer::EMPTY_TAG; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/ExprFunction.php b/src/TextGenerator/TextFunction/ExprFunction.php index 1ea7343..e749a71 100644 --- a/src/TextGenerator/TextFunction/ExprFunction.php +++ b/src/TextGenerator/TextFunction/ExprFunction.php @@ -2,6 +2,9 @@ namespace Neveldo\TextGenerator\TextFunction; +use InvalidArgumentException; +use Exception; +use Error; use Neveldo\TextGenerator\Tag\TagReplacer; use Neveldo\TextGenerator\Tag\TagReplacerInterface; use Neveldo\TextGenerator\ExpressionLanguage\ExpressionLanguage; @@ -15,45 +18,34 @@ */ class ExprFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * ExprFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle Expr function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments * @return string * @throw InvalidArgumentException if the number of arguments is not valid */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { if (count($arguments) !== 1) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("ExprFunction expect exactly one parameter, %d given.", count($arguments)) ); } try { + /** @phpstan-ignore return.type */ return (new ExpressionLanguage())->evaluate( $this->tagReplacer->sanitizeTagNames($originalArguments[0]), $this->tagReplacer->getTags() ); - } catch (\Exception $e) { - return TagReplacer::EMPTY_TAG; - } catch (\Error $e) { + } catch (Exception|Error) { return TagReplacer::EMPTY_TAG; } } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/FilterFunction.php b/src/TextGenerator/TextFunction/FilterFunction.php index e295a8e..663f16c 100644 --- a/src/TextGenerator/TextFunction/FilterFunction.php +++ b/src/TextGenerator/TextFunction/FilterFunction.php @@ -2,6 +2,9 @@ namespace Neveldo\TextGenerator\TextFunction; +use DateTime; +use InvalidArgumentException; +use Exception; use Neveldo\TextGenerator\Tag\TagReplacer; use Neveldo\TextGenerator\Tag\TagReplacerInterface; @@ -14,23 +17,12 @@ class FilterFunction implements FunctionInterface { /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * @var array array of available filters + * @var array array of available filters */ protected $availableFilters = []; - /** - * FilterFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; - // Init defaultfilters $this->availableFilters = [ 'round' => [ @@ -62,12 +54,12 @@ public function __construct(TagReplacerInterface $tagReplacer) 'maxArgs' => 2 ], 'number' => [ - 'function' => function($value, $decimals = 0, $decPoint = '.', $thousands_sep = ',') { + 'function' => function ($value, $decimals = '0', $decPoint = '.', $thousandsSep = ',') { if (!is_numeric($value)) { return $value; } - return number_format($value, $decimals, $decPoint, $thousands_sep); + return number_format((float) $value, (int) $decimals, $decPoint, $thousandsSep); }, 'minArgs' => 1, @@ -85,23 +77,17 @@ public function __construct(TagReplacerInterface $tagReplacer) 'maxArgs' => 1 ], 'lowerfirst' => [ - 'function' => function($value) { - return mb_strtolower(mb_substr($value, 0, 1)) . mb_substr($value, 1, mb_strlen($value) - 1); - }, + 'function' => fn ($value): string => mb_strtolower(mb_substr((string) $value, 0, 1)) . mb_substr((string) $value, 1, mb_strlen((string) $value) - 1), 'minArgs' => 1, 'maxArgs' => 1 ], 'upperfirst' => [ - 'function' => function($value) { - return mb_strtoupper(mb_substr($value, 0, 1)) . mb_substr($value, 1, mb_strlen($value) - 1); - }, + 'function' => fn ($value): string => mb_strtoupper(mb_substr((string) $value, 0, 1)) . mb_substr((string) $value, 1, mb_strlen((string) $value) - 1), 'minArgs' => 1, 'maxArgs' => 1 ], 'upperwords' => [ - 'function' => function($value) { - return mb_convert_case($value, MB_CASE_TITLE); - }, + 'function' => fn ($value): string => mb_convert_case((string) $value, MB_CASE_TITLE), 'minArgs' => 1, 'maxArgs' => 1 ], @@ -121,23 +107,19 @@ public function __construct(TagReplacerInterface $tagReplacer) 'maxArgs' => 3 ], 'timestamp' => [ - 'function' => function($format, $timestamp = null) { - if (isset($timestamp)) { - $timestamp = (int) $timestamp; - } else { - $timestamp = time(); - } + 'function' => function ($format, $timestamp = null): string { + $timestamp = isset($timestamp) ? (int) $timestamp : time(); return date($format, $timestamp); }, 'minArgs' => 1, 'maxArgs' => 2 ], 'date' => [ - 'function' => function($date, $fromFormat, $toFormat) { - $datetime = \DateTime::createFromFormat($fromFormat, $date); + 'function' => function ($date, $fromFormat, $toFormat): string { + $datetime = DateTime::createFromFormat($fromFormat, $date); if ($datetime === false) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("Wrong date format %s for the date %s.", $fromFormat, $date) ); } @@ -151,21 +133,21 @@ public function __construct(TagReplacerInterface $tagReplacer) /** * Handle Filter function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments * @return string * @throw InvalidArgumentException if the number of arguments is not valid */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { if (count($arguments) < 2) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("FilterFunction expect at least two parameters, %d given.", count($arguments)) ); } if (!isset($this->availableFilters[$arguments[0]])) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("The filter %s is not available.", $arguments[0]) ); } @@ -175,7 +157,7 @@ public function execute(array $arguments, array $originalArguments) array_shift($arguments); if (count($arguments) < $filter['minArgs']) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("Filter %s expect at least %d parameters, %d given.", $filterName, $filter['minArgs'], (count($arguments))) ); } @@ -185,30 +167,26 @@ public function execute(array $arguments, array $originalArguments) } if (isset($filter['maxArgs']) && count($arguments) > $filter['maxArgs']) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("Filter %s expect maximum %d parameters, %d given.", $filterName, $filter['maxArgs'], (count($arguments))) ); } - if (is_callable($filter['function'])) { - try { - return call_user_func_array($filter['function'], $arguments); - } catch (\Exception $e) { - return TagReplacer::EMPTY_TAG; - } + try { + /** @phpstan-ignore return.type */ + return call_user_func_array($filter['function'], $arguments); + } catch (Exception) { + return TagReplacer::EMPTY_TAG; } - - return TagReplacer::EMPTY_TAG; } /** * Add a filter - * @param $name filter name - * @param array $filter filter conf array + * @param array{'function':callable,'minArgs':int,'maxArgs'?:int} $filter filter conf array */ - public function addFilter($name, array $filter) + public function addFilter(string $name, array $filter): void { $this->availableFilters[$name] = $filter; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/FunctionInterface.php b/src/TextGenerator/TextFunction/FunctionInterface.php index 8f275df..e37ec2b 100644 --- a/src/TextGenerator/TextFunction/FunctionInterface.php +++ b/src/TextGenerator/TextFunction/FunctionInterface.php @@ -4,15 +4,15 @@ /** * Interface FunctionInterface - * Interface for parsers + * Interface for text functions * @package Neveldo\TextGenerator\TextFunction */ interface FunctionInterface { /** - * Execute the parser - * @param array $arguments - * @return string + * Execute the function + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments */ - public function execute(array $arguments, array $originalArguments); -} \ No newline at end of file + public function execute(array $arguments, array $originalArguments): string; +} diff --git a/src/TextGenerator/TextFunction/IfFunction.php b/src/TextGenerator/TextFunction/IfFunction.php index 42ff6f1..db95597 100644 --- a/src/TextGenerator/TextFunction/IfFunction.php +++ b/src/TextGenerator/TextFunction/IfFunction.php @@ -2,6 +2,7 @@ namespace Neveldo\TextGenerator\TextFunction; +use InvalidArgumentException; use Neveldo\TextGenerator\Tag\TagReplacer; use Neveldo\TextGenerator\Tag\TagReplacerInterface; use Neveldo\TextGenerator\ExpressionLanguage\ExpressionLanguage; @@ -22,43 +23,31 @@ */ class IfFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * IfFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle If function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments * @return string * @throw InvalidArgumentException if the number of arguments is not valid */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { if (count($arguments) !== 2 && count($arguments) !== 3) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("IfFunction expect exactly two (condition, then statement) or three (condition, then statement, else statement) parameters, %d given.", count($arguments)) ); } - $language = new ExpressionLanguage(); + $expressionLanguage = new ExpressionLanguage(); - if ($language->evaluate($this->tagReplacer->sanitizeTagNames($originalArguments[0]), $this->tagReplacer->getTags())) { + if ($expressionLanguage->evaluate($this->tagReplacer->sanitizeTagNames($originalArguments[0]), $this->tagReplacer->getTags())) { return $arguments[1]; - } else if (isset($arguments[2])) { - return $arguments[2]; } - return TagReplacer::EMPTY_TAG; + return $arguments[2] ?? TagReplacer::EMPTY_TAG; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/LoopFunction.php b/src/TextGenerator/TextFunction/LoopFunction.php index 56e3060..3130a5a 100644 --- a/src/TextGenerator/TextFunction/LoopFunction.php +++ b/src/TextGenerator/TextFunction/LoopFunction.php @@ -2,6 +2,7 @@ namespace Neveldo\TextGenerator\TextFunction; +use InvalidArgumentException; use Neveldo\TextGenerator\Tag\TagReplacer; use Neveldo\TextGenerator\Tag\TagReplacerInterface; @@ -24,30 +25,19 @@ */ class LoopFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * LoopFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** - * Handle Random function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments - * @return string + * Handle Loop function + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { if (count($arguments) !== 6) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("Loop expect exactly six parameters, %d given.", count($arguments)) ); } @@ -59,16 +49,14 @@ public function execute(array $arguments, array $originalArguments) } $loopStrings = []; - foreach($loopData as $tags) { + foreach ($loopData as $tags) { $tagReplacer = clone $this->tagReplacer; $tagReplacer->setTags($tags); $loopStrings[] = $tagReplacer->replace($arguments[5]); } // Remove empty strings and arguments that contain empty tags - $loopStrings = array_filter($loopStrings, function($item) { - return ($item !== '' && mb_strpos($item, $this->tagReplacer->getEmptyTag()) === false); - }); + $loopStrings = array_filter($loopStrings, fn ($item): bool => $item !== '' && mb_strpos($item, $this->tagReplacer->getEmptyTag()) === false); // Parse argument 1 : number of items to loop on ('*' to loop on all elements) $limit = count($loopStrings); @@ -78,7 +66,7 @@ public function execute(array $arguments, array $originalArguments) } // Parse argument 2 : shuffle the items (true/false) - if (mb_strtolower($arguments[2]) === 'true') { + if (mb_strtolower((string) $arguments[2]) === 'true') { shuffle($loopStrings); } @@ -91,11 +79,11 @@ public function execute(array $arguments, array $originalArguments) if ($i < $limit - 2) { $result .= $arguments[3]; - } else if ($i < $limit - 1) { + } elseif ($i < $limit - 1) { $result .= $arguments[4]; } } return $result; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/ProbabilityRandomFunction.php b/src/TextGenerator/TextFunction/ProbabilityRandomFunction.php index e70d560..a3ec277 100644 --- a/src/TextGenerator/TextFunction/ProbabilityRandomFunction.php +++ b/src/TextGenerator/TextFunction/ProbabilityRandomFunction.php @@ -2,6 +2,7 @@ namespace Neveldo\TextGenerator\TextFunction; +use InvalidArgumentException; use Neveldo\TextGenerator\Tag\TagReplacer; use Neveldo\TextGenerator\Tag\TagReplacerInterface; @@ -16,40 +17,27 @@ */ class ProbabilityRandomFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * ProbabilityRandomFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle prandom function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments - * @return string + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { if (count($arguments) < 1) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("ProbabilityRandomFunction expect at least one parameter, %d given.", count($arguments)) ); } // Remove arguments that contain empty tags - $arguments = array_filter($arguments, function($item) { - return (mb_strpos($item, $this->tagReplacer->getEmptyTag()) === false); - }); + $arguments = array_filter($arguments, fn ($item): bool => mb_strpos((string) $item, $this->tagReplacer->getEmptyTag()) === false); - if (count($arguments) === 0) { + if ($arguments === []) { return TagReplacer::EMPTY_TAG; } @@ -57,21 +45,21 @@ public function execute(array $arguments, array $originalArguments) $probabilities = []; $optionId = 0; - foreach($arguments as $argument) { + foreach ($arguments as $argument) { - if (mb_strpos($argument, ':') === false) { + if (mb_strpos((string) $argument, ':') === false) { continue; } - $probability = (int) mb_substr($argument, 0, mb_strpos($argument, ':')); + $probability = (int) mb_substr((string) $argument, 0, mb_strpos((string) $argument, ':')); if ($probability <= 0) { continue; } $value = ''; - if (mb_strpos($argument, ':') + 1 < mb_strlen($argument)) { - $value = mb_substr($argument, mb_strpos($argument, ':') + 1); + if (mb_strpos((string) $argument, ':') + 1 < mb_strlen((string) $argument)) { + $value = mb_substr((string) $argument, mb_strpos((string) $argument, ':') + 1); } $options[$optionId] = [ @@ -83,10 +71,10 @@ public function execute(array $arguments, array $originalArguments) ++$optionId; } - if (count($options) === 0) { + if ($options === []) { return TagReplacer::EMPTY_TAG; } return $options[$probabilities[array_rand($probabilities)]]['value']; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/RandomFunction.php b/src/TextGenerator/TextFunction/RandomFunction.php index 0e9e2db..371a280 100644 --- a/src/TextGenerator/TextFunction/RandomFunction.php +++ b/src/TextGenerator/TextFunction/RandomFunction.php @@ -15,37 +15,25 @@ */ class RandomFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * RandomFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle Random function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { // Remove arguments that contain empty tags - $arguments = array_filter($arguments, function($item) { - return (mb_strpos($item, $this->tagReplacer->getEmptyTag()) === false); - }); + $arguments = array_filter($arguments, fn ($item): bool => mb_strpos((string) $item, $this->tagReplacer->getEmptyTag()) === false); - if (count($arguments) === 0) { + if ($arguments === []) { return TagReplacer::EMPTY_TAG; } return $arguments[array_rand($arguments)]; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/ReproducableRandomFunction.php b/src/TextGenerator/TextFunction/ReproducableRandomFunction.php index 859d1b8..2a652da 100644 --- a/src/TextGenerator/TextFunction/ReproducableRandomFunction.php +++ b/src/TextGenerator/TextFunction/ReproducableRandomFunction.php @@ -1,55 +1,44 @@ -tagReplacer = $tagReplacer; - } - - /** - * Handle Reproducable Random function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments - */ - public function execute(array $arguments, array $originalArguments) - { - // Remove arguments that contain empty tags - $arguments = array_filter($arguments, function($item) { - return (mb_strpos($item, $this->tagReplacer->getEmptyTag()) === false); - }); - - if (count($arguments) === 0) { - return TagReplacer::EMPTY_TAG; - } - - $seed = array_shift($arguments); - $twister = new \mersenne_twister\twister(md5($seed)); - $key = $twister->rangeint(0, sizeof($arguments) - 1); - - return $arguments[$key]; - } - -} \ No newline at end of file + $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments + */ + public function execute(array $arguments, array $originalArguments): string + { + // Remove arguments that contain empty tags + $arguments = array_filter($arguments, fn ($item): bool => mb_strpos((string) $item, $this->tagReplacer->getEmptyTag()) === false); + + if ($arguments === []) { + return TagReplacer::EMPTY_TAG; + } + + $seed = array_shift($arguments); + $twister = new twister(md5((string) $seed)); + $key = $twister->rangeint(0, count($arguments) - 1); + + return $arguments[$key]; + } + +} diff --git a/src/TextGenerator/TextFunction/RmnaFunction.php b/src/TextGenerator/TextFunction/RmnaFunction.php index b6400b8..d998dff 100644 --- a/src/TextGenerator/TextFunction/RmnaFunction.php +++ b/src/TextGenerator/TextFunction/RmnaFunction.php @@ -5,7 +5,7 @@ use Neveldo\TextGenerator\Tag\TagReplacerInterface; /** - * Class c + * Class RmnaFunction * 'rmna' function : return the argument only if it does not contain any empty values * Examples : * #rmna{one @possible_not_available_tag two} @@ -14,29 +14,19 @@ */ class RmnaFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * RandomFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle rmna function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { - if (count($arguments) === 0 - || mb_strpos($arguments[0], $this->tagReplacer->getEmptyTag()) !== false + if ($arguments === [] + || mb_strpos((string) $arguments[0], $this->tagReplacer->getEmptyTag()) !== false ) { return ''; } @@ -44,4 +34,4 @@ public function execute(array $arguments, array $originalArguments) return $arguments[0]; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/SetFunction.php b/src/TextGenerator/TextFunction/SetFunction.php index 5edf34d..b9165de 100644 --- a/src/TextGenerator/TextFunction/SetFunction.php +++ b/src/TextGenerator/TextFunction/SetFunction.php @@ -2,6 +2,7 @@ namespace Neveldo\TextGenerator\TextFunction; +use InvalidArgumentException; use Neveldo\TextGenerator\Tag\TagReplacerInterface; /** @@ -14,36 +15,26 @@ */ class SetFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * SetFunction constructor. - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle set function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { if (count($arguments) !== 2) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("SetFunction expect exactly two parameters (tag name and tag value), %d given.", count($arguments)) ); } - $this->tagReplacer->addTag(mb_substr(trim($originalArguments[0]), 1), $arguments[1]); + $this->tagReplacer->addTag(mb_substr(trim((string) $originalArguments[0]), 1), $arguments[1]); return ''; } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextFunction/ShuffleFunction.php b/src/TextGenerator/TextFunction/ShuffleFunction.php index 39a15f6..1b04901 100644 --- a/src/TextGenerator/TextFunction/ShuffleFunction.php +++ b/src/TextGenerator/TextFunction/ShuffleFunction.php @@ -2,6 +2,7 @@ namespace Neveldo\TextGenerator\TextFunction; +use InvalidArgumentException; use Neveldo\TextGenerator\Tag\TagReplacerInterface; /** @@ -15,30 +16,19 @@ */ class ShuffleFunction implements FunctionInterface { - /** - * @var TagReplacerInterface Tag Replacer service - */ - private $tagReplacer; - - /** - * ShuffleFunction constructors - * @param TagReplacerInterface $tagReplacer - */ - public function __construct(TagReplacerInterface $tagReplacer) + public function __construct(private readonly TagReplacerInterface $tagReplacer) { - $this->tagReplacer = $tagReplacer; } /** * Handle Shuffle function - * @param array $arguments list of arguments where tags have been replaced by their values - * @param array $originalArguments list of original arguments - * @return string + * @param array $arguments list of arguments where tags have been replaced by their values + * @param array $originalArguments list of original arguments */ - public function execute(array $arguments, array $originalArguments) + public function execute(array $arguments, array $originalArguments): string { if (count($arguments) < 2) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( sprintf("ShuffleFunction expect at least two parameters, %d given.", count($arguments)) ); } @@ -48,13 +38,11 @@ public function execute(array $arguments, array $originalArguments) $arguments = array_map("trim", $arguments); // Remove empty arguments and arguments that contain empty tags - $arguments = array_filter($arguments, function($item) { - return ($item !== '' && mb_strpos($item, $this->tagReplacer->getEmptyTag()) === false); - }); + $arguments = array_filter($arguments, fn ($item): bool => $item !== '' && mb_strpos($item, $this->tagReplacer->getEmptyTag()) === false); shuffle($arguments); return implode($separator, $arguments); } -} \ No newline at end of file +} diff --git a/src/TextGenerator/TextGenerator.php b/src/TextGenerator/TextGenerator.php index 202e5bc..abf5c2e 100644 --- a/src/TextGenerator/TextGenerator.php +++ b/src/TextGenerator/TextGenerator.php @@ -2,6 +2,7 @@ namespace Neveldo\TextGenerator; +use InvalidArgumentException; use Neveldo\TextGenerator\TextFunction\ChooseFunction; use Neveldo\TextGenerator\TextFunction\CoalesceFunction; use Neveldo\TextGenerator\TextFunction\ExprFunction; @@ -11,7 +12,7 @@ use Neveldo\TextGenerator\TextFunction\LoopFunction; use Neveldo\TextGenerator\TextFunction\ProbabilityRandomFunction; use Neveldo\TextGenerator\TextFunction\RandomFunction; -use Neveldo\TextGenerator\TextFunction\ReproducableRandomFunction; +use Neveldo\TextGenerator\TextFunction\ReproducableRandomFunction; use Neveldo\TextGenerator\TextFunction\RmnaFunction; use Neveldo\TextGenerator\TextFunction\SetFunction; use Neveldo\TextGenerator\TextFunction\ShuffleFunction; @@ -28,36 +29,24 @@ class TextGenerator /** * @var FunctionInterface[] collection of function */ - private $functions = []; + private array $functions = []; - /** - * @var TagReplacerInterface - */ - private $tagReplacer; - - /** - * @var string the template to handle - */ - private $template; + private TagReplacerInterface $tagReplacer; /** * @var string the compiled template */ - private $compiledTemplate; + private string $compiledTemplate; /** - * @var array sorted execution stack to run for generating a text + * @var array sorted execution stack to run for generating a text */ - private $executionStack = []; + private array $executionStack = []; - /** - * TextGenerator constructor. - * @param TagReplacerInterface|null $tagReplacer - */ public function __construct(TagReplacerInterface $tagReplacer = null) { // Init the tag replacer - if ($tagReplacer === null) { + if (!$tagReplacer instanceof TagReplacerInterface) { $tagReplacer = new TagReplacer(); } $this->tagReplacer = $tagReplacer; @@ -81,13 +70,13 @@ public function __construct(TagReplacerInterface $tagReplacer = null) /** * Generate an automated text from data - * @param array $data that will feed the tags within the template + * @param array>> $data that will feed the tags within the template * @return string the generated text * @throw Exception */ - public function generate(array $data) + public function generate(array $data): string { - if ($this->compiledTemplate === null) { + if ($this->compiledTemplate === '') { return ''; } @@ -97,7 +86,7 @@ public function generate(array $data) // Execute the functions stack starting with the deepest functions and ending // with the shallowest ones - foreach($this->executionStack as $statement) { + foreach ($this->executionStack as $statement) { $openingTag = '[' . $statement['id'] . ']'; $closingTag = '[/' . $statement['id'] . ']'; @@ -116,8 +105,8 @@ public function generate(array $data) $text = $this->substringReplace( $text, $this->getFunction($statement['function'])->execute($parsedArguments, $originalArguments), - mb_strpos($text, $openingTag), - mb_strpos($text, $closingTag) + mb_strlen($closingTag) - mb_strpos($text, $openingTag) + (int) mb_strpos($text, $openingTag), + (int) mb_strpos($text, $closingTag) + mb_strlen($closingTag) - mb_strpos($text, $openingTag) ); } @@ -133,13 +122,8 @@ public function generate(array $data) /** * substr_replace that works with multibytes strings * @see http://php.net/manual/en/function.substr-replace.php - * @param $str - * @param $replacement - * @param $start - * @param $length - * @return string */ - protected function substringReplace($str, $replacement, $start, $length) + protected function substringReplace(string $str, string $replacement, int $start, int $length): string { return mb_substr($str, 0, $start) . $replacement . mb_substr($str, $start + $length); } @@ -147,13 +131,10 @@ protected function substringReplace($str, $replacement, $start, $length) /** * Prepare the template by parsing the function calls within it * @param string $template The template to compile - * @return $this * @throw \InvalidArgumentException if the template contains unknown functions */ - public function compile($template) + public function compile(string $template): static { - $this->template = $template; - $template = $this->parseIndentations($template); $data = $this->compileTemplate($template); @@ -165,13 +146,12 @@ public function compile($template) /** * Sort the function calls tree from left to right and from bottom to up - * @param array $statements - * @param null|int $parent parent statement ID - * @return array + * @param array $statements + * @return array */ - public function getSortedStatements(array $statements, $parent = null): array + public function getSortedStatements(array $statements): array { - if (count($statements) === 0) { + if ($statements === []) { return []; } @@ -211,28 +191,25 @@ public function getSortedStatements(array $statements, $parent = null): array $statement = $linkedStatements[$statement['next']]; $sortedStatements[] = $statement; } - + /** @phpstan-ignore return.type */ return $sortedStatements; } /** - * Remove the ;; followed by any space characters from the - * template - * @param string $template - * @return string + * Remove the ;; followed by any space characters from the template */ - public function parseIndentations($template) + public function parseIndentations(string $template): string { - return preg_replace('/;;\s+/m', '', $template); + return (string) preg_replace('/;;\s+/m', '', $template); } /** * Parse the template to compute the execution stack * @param string $template - * @return array Array that contains compiledTemplate and executionStack outputs + * @return array{'compiledTemplate': string, 'executionStack': array} Array that contains compiledTemplate and executionStack outputs * @throw \InvalidArgumentException if the template contains unknown functions */ - protected function compileTemplate($template) + protected function compileTemplate(string $template): array { $beginFunctionChar = '#'; $beginArgsChar = '{'; @@ -242,7 +219,9 @@ protected function compileTemplate($template) $template = preg_split('//u', $template, -1, PREG_SPLIT_NO_EMPTY); $compiledTemplate = ''; + /** @®var array */ $executionStack = []; + /** @var array */ $unclosedFunctionsStack = []; $callingFunction = false; @@ -252,7 +231,7 @@ protected function compileTemplate($template) $currentCharIndex = 0; $currentStackIndex = 0; - while(!$parsingEnded) { + while (!$parsingEnded) { if (!isset($template[$currentCharIndex])) { $parsingEnded = true; continue; @@ -263,13 +242,13 @@ protected function compileTemplate($template) if ($template[$currentCharIndex] === $beginArgsChar && mb_strlen($callingFunctionName) !== 0) { // End of function name if (!in_array($callingFunctionName, array_keys($this->functions))) { - throw new \InvalidArgumentException(sprintf("Error : function '%s' doesn't exist.", $callingFunctionName)); + throw new InvalidArgumentException(sprintf("Error : function '%s' doesn't exist.", $callingFunctionName)); } $compiledTemplate .= '[' . $currentStackIndex . ']'; $parent = null; - if (count($unclosedFunctionsStack) !== 0) { + if ($unclosedFunctionsStack !== []) { $parent = $unclosedFunctionsStack[count($unclosedFunctionsStack) - 1]; } @@ -294,21 +273,19 @@ protected function compileTemplate($template) $callingFunction = false; $callingFunctionName = ''; } - } else { - if ($template[$currentCharIndex] === $beginFunctionChar) { - // Begin of new function call - $callingFunction = true; - $callingFunctionName = ''; - } elseif ($template[$currentCharIndex] === $endArgsChar) { - // End of function call - if (($lastUnclosedFunction = array_pop($unclosedFunctionsStack)) !== null) { - $compiledTemplate .= '[/' . $lastUnclosedFunction . ']'; - } else { - $compiledTemplate .= $template[$currentCharIndex]; - } + } elseif ($template[$currentCharIndex] === $beginFunctionChar) { + // Begin of new function call + $callingFunction = true; + $callingFunctionName = ''; + } elseif ($template[$currentCharIndex] === $endArgsChar) { + // End of function call + if (($lastUnclosedFunction = array_pop($unclosedFunctionsStack)) !== null) { + $compiledTemplate .= '[/' . $lastUnclosedFunction . ']'; } else { $compiledTemplate .= $template[$currentCharIndex]; } + } else { + $compiledTemplate .= $template[$currentCharIndex]; } $currentCharIndex++; } @@ -325,7 +302,7 @@ protected function compileTemplate($template) * @param FunctionInterface $function The text function * @return $this */ - public function registerFunction($name, FunctionInterface $function) + public function registerFunction(string $name, FunctionInterface $function): static { $this->functions[$name] = $function; return $this; @@ -333,24 +310,22 @@ public function registerFunction($name, FunctionInterface $function) /** * Get a function from its name - * @param $name * @return FunctionInterface * @throw \InvalidArgumentException if the function doesn't exist */ - public function getFunction($name) + public function getFunction(string $name): FunctionInterface { if (!array_key_exists($name, $this->functions)) { - throw new \InvalidArgumentException(sprintf("Error : function '%s' doesn't exist.", $name)); + throw new InvalidArgumentException(sprintf("Error : function '%s' doesn't exist.", $name)); } return $this->functions[$name]; } /** * Set the tag replacer - * @param TagReplacerInterface $tagReplacer * @return $this */ - public function setTagReplacer(TagReplacerInterface $tagReplacer) + public function setTagReplacer(TagReplacerInterface $tagReplacer): static { $this->tagReplacer = $tagReplacer; return $this; @@ -365,24 +340,18 @@ public function getTagReplacer() return $this->tagReplacer; } - /** - * @return string - */ public function getCompiledTemplate(): string { return $this->compiledTemplate; } - /** - * @param string $compiledTemplate - */ public function setCompiledTemplate(string $compiledTemplate): void { $this->compiledTemplate = $compiledTemplate; } /** - * @return array + * @return array sorted execution stack to run for generating a text */ public function getExecutionStack(): array { @@ -390,10 +359,10 @@ public function getExecutionStack(): array } /** - * @param array $executionStack + * @param array $executionStack sorted execution stack to run for generating a text */ public function setExecutionStack(array $executionStack): void { $this->executionStack = $executionStack; } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/Tag/TagReplacerTest.php b/tests/TextGenerator/Tag/TagReplacerTest.php index 1d76bd4..3f62abb 100644 --- a/tests/TextGenerator/Tag/TagReplacerTest.php +++ b/tests/TextGenerator/Tag/TagReplacerTest.php @@ -2,46 +2,48 @@ namespace Neveldo\TextGenerator\Tag; -class TagReplacerTest extends \PHPUnit\Framework\TestCase +use PHPUnit\Framework\TestCase; + +class TagReplacerTest extends TestCase { - public function testSetTags() + public function testSetTags(): void { - $tr = new TagReplacer(); - $tr->setTags(['tag1' => 'val1', 'tag2' => 'val2']); - $this->assertEquals(['tag1' => 'val1', 'tag2' => 'val2'], $tr->getTags()); - $this->assertEquals(['@tag1' => 'val1', '@tag2' => 'val2'], $tr->getEscapedTags()); + $tagReplacer = new TagReplacer(); + $tagReplacer->setTags(['tag1' => 'val1', 'tag2' => 'val2']); + $this->assertEquals(['tag1' => 'val1', 'tag2' => 'val2'], $tagReplacer->getTags()); + $this->assertEquals(['@tag1' => 'val1', '@tag2' => 'val2'], $tagReplacer->getEscapedTags()); } - public function testSetTagsWithArrayTag() + public function testSetTagsWithArrayTag(): void { - $tr = new TagReplacer(); - $tr->setTags(['tag1' => 'val1', 'tag2' => ['sub_tag' => 'sub_val']]); - $this->assertEquals(['tag1' => 'val1', 'tag2' => ['sub_tag' => 'sub_val']], $tr->getTags()); - $this->assertEquals(['@tag1' => 'val1'], $tr->getEscapedTags()); + $tagReplacer = new TagReplacer(); + $tagReplacer->setTags(['tag1' => 'val1', 'tag2' => [['sub_tag' => 'sub_val']]]); + $this->assertEquals(['tag1' => 'val1', 'tag2' => [['sub_tag' => 'sub_val']]], $tagReplacer->getTags()); + $this->assertEquals(['@tag1' => 'val1'], $tagReplacer->getEscapedTags()); } - public function testSetTagsWithEmptyTag() + public function testSetTagsWithEmptyTag(): void { - $tr = new TagReplacer(); - $tr->setTags(['tag1' => '', 'tag2' => 'val1', 'tag3' => null]); - $this->assertEquals(['tag1' => '', 'tag2' => 'val1', 'tag3' => null], $tr->getTags()); - $this->assertEquals(['@tag1' => $tr->getEmptyTag(), '@tag2' => 'val1', '@tag3' => $tr->getEmptyTag()], $tr->getEscapedTags()); + $tagReplacer = new TagReplacer(); + $tagReplacer->setTags(['tag1' => '', 'tag2' => 'val1', 'tag3' => '']); + $this->assertEquals(['tag1' => '', 'tag2' => 'val1', 'tag3' => ''], $tagReplacer->getTags()); + $this->assertEquals(['@tag1' => $tagReplacer->getEmptyTag(), '@tag2' => 'val1', '@tag3' => $tagReplacer->getEmptyTag()], $tagReplacer->getEscapedTags()); } - public function testReplaceRegularTags() + public function testReplaceRegularTags(): void { - $tr = new TagReplacer(); - $tr->setTags(['tag1' => 'val1', 'tag2' => 'val2']); - $result = $tr->replace("test1@tag1test2 @tag2 test3 @tag3 tag2 test4"); + $tagReplacer = new TagReplacer(); + $tagReplacer->setTags(['tag1' => 'val1', 'tag2' => 'val2']); + $result = $tagReplacer->replace("test1@tag1test2 @tag2 test3 @tag3 tag2 test4"); $this->assertEquals("test1val1test2 val2 test3 @tag3 tag2 test4", $result); } - public function testReplaceTagsWithEmptyTag() + public function testReplaceTagsWithEmptyTag(): void { - $tr = new TagReplacer(); - $tr->setTags(['tag1' => '', 'tag2' => 'val1', 'tag3' => null]); + $tagReplacer = new TagReplacer(); + $tagReplacer->setTags(['tag1' => '', 'tag2' => 'val1', 'tag3' => '']); - $result = $tr->replace("test @tag1 test @tag2 test @tag3"); - $this->assertEquals("test " . $tr->getEmptyTag() . " test val1 test " . $tr->getEmptyTag(), $result); + $result = $tagReplacer->replace("test @tag1 test @tag2 test @tag3"); + $this->assertEquals("test " . $tagReplacer->getEmptyTag() . " test val1 test " . $tagReplacer->getEmptyTag(), $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/ChooseFunctionTest.php b/tests/TextGenerator/TextFunction/ChooseFunctionTest.php index 5d46788..958e1ae 100644 --- a/tests/TextGenerator/TextFunction/ChooseFunctionTest.php +++ b/tests/TextGenerator/TextFunction/ChooseFunctionTest.php @@ -2,48 +2,54 @@ namespace Neveldo\TextGenerator\Tag; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\ChooseFunction; -class ChooseFunctionTest extends \PHPUnit\Framework\TestCase +class ChooseFunctionTest extends TestCase { - public function setUp() { + private TagReplacer $tagReplacer; + private ChooseFunction $chooseFunction; + + public function setUp(): void + { $this->tagReplacer = new TagReplacer(); - $this->function = new ChooseFunction($this->tagReplacer); + $this->chooseFunction = new ChooseFunction($this->tagReplacer); } - public function testWithZeroArgument() + public function testWithZeroArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([], []); + $this->expectException(InvalidArgumentException::class); + $this->chooseFunction->execute([], []); } - public function testWithOneArgument() + public function testWithOneArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([''], ['']); + $this->expectException(InvalidArgumentException::class); + $this->chooseFunction->execute([''], ['']); } - public function testSimpleChoose() + public function testSimpleChoose(): void { - $result = $this->function->execute([2, 'test1','test2', 'test3'], [2, 'test1','test2', 'test3']); + $result = $this->chooseFunction->execute(['2', 'test1','test2', 'test3'], ['2', 'test1','test2', 'test3']); $this->assertEquals('test2', $result); } - public function testSimpleChoose2() + public function testSimpleChoose2(): void { - $result = $this->function->execute(['3', 'test1','test2', 'test3'], ['3', 'test1','test2', 'test3']); + $result = $this->chooseFunction->execute(['3', 'test1','test2', 'test3'], ['3', 'test1','test2', 'test3']); $this->assertEquals('test3', $result); } - public function testChooseWithUnexistantArg() + public function testChooseWithUnexistantArg(): void { - $result = $this->function->execute([0, 'test1','test2', 'test3'], [0, 'test1','test2', 'test3']); + $result = $this->chooseFunction->execute(['0', 'test1','test2', 'test3'], ['0', 'test1','test2', 'test3']); $this->assertEquals('[EMPTY]', $result); } - public function testChooseWithUnexistantArg2() + public function testChooseWithUnexistantArg2(): void { - $result = $this->function->execute([7, 'test1','test2', 'test3'], [7, 'test1','test2', 'test3']); + $result = $this->chooseFunction->execute(['7', 'test1','test2', 'test3'], ['7', 'test1','test2', 'test3']); $this->assertEquals('[EMPTY]', $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/CoalesceFunctionTest.php b/tests/TextGenerator/TextFunction/CoalesceFunctionTest.php index 78c9b92..e692330 100644 --- a/tests/TextGenerator/TextFunction/CoalesceFunctionTest.php +++ b/tests/TextGenerator/TextFunction/CoalesceFunctionTest.php @@ -2,45 +2,44 @@ namespace Neveldo\TextGenerator\Tag; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\CoalesceFunction; -class CoalesceFunctionTest extends \PHPUnit\Framework\TestCase +class CoalesceFunctionTest extends TestCase { - public function setUp() { - $this->tagReplacer = new TagReplacer(); - $this->function = new CoalesceFunction($this->tagReplacer); - } + private TagReplacer $tagReplacer; + private CoalesceFunction $coalesceFunction; - public function testWithZeroArgument() + public function setUp(): void { - $result = $this->function->execute([], []); - $this->assertEquals('[EMPTY]', $result); + $this->tagReplacer = new TagReplacer(); + $this->coalesceFunction = new CoalesceFunction($this->tagReplacer); } - public function testWithOneEmptyArgument() + public function testWithZeroArgument(): void { - $result = $this->function->execute([''], ['']); + $result = $this->coalesceFunction->execute([], []); $this->assertEquals('[EMPTY]', $result); } - public function testWithOneNullArgument() + public function testWithOneEmptyArgument(): void { - $result = $this->function->execute([null], [null]); + $result = $this->coalesceFunction->execute([''], ['']); $this->assertEquals('[EMPTY]', $result); } - public function testWithOneRegularArgument() + public function testWithOneRegularArgument(): void { - $result = $this->function->execute(['Hello'], ['Hello']); + $result = $this->coalesceFunction->execute(['Hello'], ['Hello']); $this->assertEquals('Hello', $result); } - public function testEmptyAndRegularsArguments() + public function testEmptyAndRegularsArguments(): void { - $result = $this->function->execute( - [$this->tagReplacer->getEmptyTag(), null, '', 'value1', 'value2'], - [$this->tagReplacer->getEmptyTag(), null, '', 'value1', 'value2'] + $result = $this->coalesceFunction->execute( + [$this->tagReplacer->getEmptyTag(), '', 'value1', 'value2'], + [$this->tagReplacer->getEmptyTag(), '', 'value1', 'value2'] ); $this->assertEquals('value1', $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/ExprFunctionTest.php b/tests/TextGenerator/TextFunction/ExprFunctionTest.php index e4e0250..e0497ca 100644 --- a/tests/TextGenerator/TextFunction/ExprFunctionTest.php +++ b/tests/TextGenerator/TextFunction/ExprFunctionTest.php @@ -2,30 +2,36 @@ namespace Neveldo\TextGenerator\Tag; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\ExprFunction; -class ExprFunctionTest extends \PHPUnit\Framework\TestCase +class ExprFunctionTest extends TestCase { - public function setUp() { + private TagReplacer $tagReplacer; + private ExprFunction $exprFunction; + + public function setUp(): void + { $this->tagReplacer = new TagReplacer(); - $this->function = new ExprFunction($this->tagReplacer); + $this->exprFunction = new ExprFunction($this->tagReplacer); } - public function testWithZeroArgument() + public function testWithZeroArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([], []); + $this->expectException(InvalidArgumentException::class); + $this->exprFunction->execute([], []); } - public function testWithTownArgument() + public function testWithTownArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute(['', ''], ['', '']); + $this->expectException(InvalidArgumentException::class); + $this->exprFunction->execute(['', ''], ['', '']); } - public function testSimpleExpression() + public function testSimpleExpression(): void { - $result = $this->function->execute(['2 + 1'], ['2 + 1']); + $result = $this->exprFunction->execute(['2 + 1'], ['2 + 1']); $this->assertEquals('3', $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/FilterFunctionTest.php b/tests/TextGenerator/TextFunction/FilterFunctionTest.php index a5f64af..bae3288 100644 --- a/tests/TextGenerator/TextFunction/FilterFunctionTest.php +++ b/tests/TextGenerator/TextFunction/FilterFunctionTest.php @@ -2,48 +2,54 @@ namespace Neveldo\TextGenerator\Tag; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\FilterFunction; -class FilterFunctionTest extends \PHPUnit\Framework\TestCase +class FilterFunctionTest extends TestCase { - public function setUp() { + private TagReplacer $tagReplacer; + private FilterFunction $filterFunction; + + public function setUp(): void + { $this->tagReplacer = new TagReplacer(); - $this->function = new FilterFunction($this->tagReplacer); + $this->filterFunction = new FilterFunction($this->tagReplacer); } - public function testWithZeroArgument() + public function testWithZeroArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([], []); + $this->expectException(InvalidArgumentException::class); + $this->filterFunction->execute([], []); } - public function testWithOneArgument() + public function testWithOneArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute(['round'], ['round']); + $this->expectException(InvalidArgumentException::class); + $this->filterFunction->execute(['round'], ['round']); } - public function testRound() + public function testRound(): void { - $result = $this->function->execute(['round', '3.44444'], ['round', '3.44444']); + $result = $this->filterFunction->execute(['round', '3.44444'], ['round', '3.44444']); $this->assertEquals(3, $result); } - public function testRound2() + public function testRound2(): void { - $result = $this->function->execute(['round', '3.44444', 1], ['round', '3.44444', 1]); + $result = $this->filterFunction->execute(['round', '3.44444', '1'], ['round', '3.44444', '1']); $this->assertEquals(3.4, $result); } - public function testUnexistantFilter() + public function testUnexistantFilter(): void { - $this->expectException(\InvalidArgumentException::class); - $result = $this->function->execute(['unexistant', '3.44444', 1], ['unexistant', '3.44444', 1]); + $this->expectException(InvalidArgumentException::class); + $this->filterFunction->execute(['unexistant', '3.44444', '1'], ['unexistant', '3.44444', '1']); } - public function testTooManyParams() + public function testTooManyParams(): void { - $this->expectException(\InvalidArgumentException::class); - $result = $this->function->execute(['round', '3.44444', 1, '3.44444', 1, '3.44444', 1, '3.44444', 1], ['round', '3.44444', 1, '3.44444', 1, '3.44444', 1, '3.44444', 1]); + $this->expectException(InvalidArgumentException::class); + $this->filterFunction->execute(['round', '3.44444', '1', '3.44444', '1', '3.44444', '1', '3.44444', '1'], ['round', '3.44444', '1', '3.44444', '1', '3.44444', '1', '3.44444', '1']); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/IfFunctionTest.php b/tests/TextGenerator/TextFunction/IfFunctionTest.php index b5ad9d1..70b1def 100644 --- a/tests/TextGenerator/TextFunction/IfFunctionTest.php +++ b/tests/TextGenerator/TextFunction/IfFunctionTest.php @@ -2,48 +2,54 @@ namespace Neveldo\TextGenerator\Tag; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\IfFunction; -class IfFunctionTest extends \PHPUnit\Framework\TestCase +class IfFunctionTest extends TestCase { - public function setUp() { + private TagReplacer $tagReplacer; + private IfFunction $ifFunction; + + public function setUp(): void + { $this->tagReplacer = new TagReplacer(); - $this->function = new IfFunction($this->tagReplacer); + $this->ifFunction = new IfFunction($this->tagReplacer); } - public function testWithZeroArgument() + public function testWithZeroArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([], []); + $this->expectException(InvalidArgumentException::class); + $this->ifFunction->execute([], []); } - public function testWithOneArgument() + public function testWithOneArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([''], ['']); + $this->expectException(InvalidArgumentException::class); + $this->ifFunction->execute([''], ['']); } - public function testConditionTrue() + public function testConditionTrue(): void { - $result = $this->function->execute(['2 > 1', 'ok', 'notok'], ['2 > 1', 'ok', 'notok']); + $result = $this->ifFunction->execute(['2 > 1', 'ok', 'notok'], ['2 > 1', 'ok', 'notok']); $this->assertEquals('ok', $result); } - public function testConditionFalse() + public function testConditionFalse(): void { - $result = $this->function->execute(['2 < 1', 'ok', 'notok'], ['2 < 1', 'ok', 'notok']); + $result = $this->ifFunction->execute(['2 < 1', 'ok', 'notok'], ['2 < 1', 'ok', 'notok']); $this->assertEquals('notok', $result); } - public function testConditionTrueWithNoElse() + public function testConditionTrueWithNoElse(): void { - $result = $this->function->execute(['2 > 1', 'ok'], ['2 > 1', 'ok']); + $result = $this->ifFunction->execute(['2 > 1', 'ok'], ['2 > 1', 'ok']); $this->assertEquals('ok', $result); } - public function testConditionFalseWithNoElse() + public function testConditionFalseWithNoElse(): void { - $result = $this->function->execute(['2 < 1', 'ok'], ['2 < 1', 'ok']); + $result = $this->ifFunction->execute(['2 < 1', 'ok'], ['2 < 1', 'ok']); $this->assertEquals('[EMPTY]', $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/LoopFunctionTest.php b/tests/TextGenerator/TextFunction/LoopFunctionTest.php index 474cba5..19b24cf 100644 --- a/tests/TextGenerator/TextFunction/LoopFunctionTest.php +++ b/tests/TextGenerator/TextFunction/LoopFunctionTest.php @@ -2,13 +2,31 @@ namespace Neveldo\TextGenerator\Tag; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\LoopFunction; -class LoopFunctionTest extends \PHPUnit\Framework\TestCase +class LoopFunctionTest extends TestCase { - public function setUp() { + private TagReplacer $tagReplacer; + private LoopFunction $loopFunction; + + /** @var array>> **/ + private array $oneElementTag; + + /** @var array>> **/ + private array $twoElementsTag; + + /** @var array>> **/ + private array $threeElementsTag; + + /** @var array>> **/ + private array $fourElementsTag; + + public function setUp(): void + { $this->tagReplacer = new TagReplacer(); - $this->function = new LoopFunction($this->tagReplacer); + $this->loopFunction = new LoopFunction($this->tagReplacer); $this->oneElementTag = [ 'loop_tag' => [ [ @@ -67,61 +85,61 @@ public function setUp() { ]; } - public function testWith5Arguments() + public function testWith5Arguments(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute(['','','','',''], ['','','','','']); + $this->expectException(InvalidArgumentException::class); + $this->loopFunction->execute(['','','','',''], ['','','','','']); } - public function testWith7Arguments() + public function testWith7Arguments(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute(['','','','','', '', ''], ['','','','','', '', '']); + $this->expectException(InvalidArgumentException::class); + $this->loopFunction->execute(['','','','','', '', ''], ['','','','','', '', '']); } - public function testRegularLoopOnOneElement() + public function testRegularLoopOnOneElement(): void { $this->tagReplacer->setTags($this->oneElementTag); - $result = $this->function->execute(['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('test1 - test2', $result); } - public function testRegularLoopOnTwoElements() + public function testRegularLoopOnTwoElements(): void { $this->tagReplacer->setTags($this->twoElementsTag); - $result = $this->function->execute(['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('test1 - test2 and test21 - test22', $result); } - public function testRegularLoopOnThreeElements() + public function testRegularLoopOnThreeElements(): void { $this->tagReplacer->setTags($this->threeElementsTag); - $result = $this->function->execute(['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('test1 - test2, test21 - test22 and test31 - test32', $result); } - public function testRegularLoopOnFourElementsMaxZero() + public function testRegularLoopOnFourElementsMaxZero(): void { $this->tagReplacer->setTags($this->fourElementsTag); - $result = $this->function->execute(['@loop_tag', 0, false, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', 0, false, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '0', 'false', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '0', 'false', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('', $result); } - public function testRegularLoopOnFourElementsMaxThree() + public function testRegularLoopOnFourElementsMaxThree(): void { $this->tagReplacer->setTags($this->fourElementsTag); - $result = $this->function->execute(['@loop_tag', 3, false, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', 3, false, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '3', 'false', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '3', 'false', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('test1 - test2, test21 - test22 and test31 - test32', $result); } - public function testRegularLoopOnFourElementsMaxFive() + public function testRegularLoopOnFourElementsMaxFive(): void { $this->tagReplacer->setTags($this->fourElementsTag); - $result = $this->function->execute(['@loop_tag', 5, false, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', 5, false, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '5', 'false', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '5', 'false', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('test1 - test2, test21 - test22, test31 - test32 and test41 - test42', $result); } - public function testRandomLoopWithTwoElements() + public function testRandomLoopWithTwoElements(): void { $this->tagReplacer->setTags([ 'loop_tag' => [ @@ -135,20 +153,20 @@ public function testRandomLoopWithTwoElements() ] ] ]); - $result = $this->function->execute(['@loop_tag', '*', true, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', true, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '*', 'true', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', 'true', ', ', ' and ', '@var1 - @var2']); $this->assertContains($result, ['test21 - test22 and test1 - test2', 'test1 - test2 and test21 - test22']); } - public function testLoopWithAScalar() + public function testLoopWithAScalar(): void { $this->tagReplacer->setTags([ 'loop_tag' => 'not_an_array' ]); - $result = $this->function->execute(['@loop_tag', '*', true, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', true, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '*', 'true', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', 'true', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('[EMPTY]', $result); } - public function testRegularLoopWithATagThatContainsAnEmptyTag() + public function testRegularLoopWithATagThatContainsAnEmptyTag(): void { $this->tagReplacer->setTags([ 'loop_tag' => [ @@ -162,11 +180,11 @@ public function testRegularLoopWithATagThatContainsAnEmptyTag() ] ] ]); - $result = $this->function->execute(['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('test1 - test2', $result); } - public function testRegularLoopWithATagThatContainsAnEmptyValue() + public function testRegularLoopWithATagThatContainsAnEmptyValue(): void { $this->tagReplacer->setTags([ 'loop_tag' => [ @@ -180,7 +198,7 @@ public function testRegularLoopWithATagThatContainsAnEmptyValue() ] ] ]); - $result = $this->function->execute(['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', false, ', ', ' and ', '@var1 - @var2']); + $result = $this->loopFunction->execute(['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2'], ['@loop_tag', '*', 'false', ', ', ' and ', '@var1 - @var2']); $this->assertEquals('test1 - test2', $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/ProbabilityRandomFunctionTest.php b/tests/TextGenerator/TextFunction/ProbabilityRandomFunctionTest.php index 1a07a29..9e0bd4e 100644 --- a/tests/TextGenerator/TextFunction/ProbabilityRandomFunctionTest.php +++ b/tests/TextGenerator/TextFunction/ProbabilityRandomFunctionTest.php @@ -2,66 +2,72 @@ namespace Neveldo\TextGenerator\Tag; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\ProbabilityRandomFunction; -class ProbabilityRandomFunctionTest extends \PHPUnit\Framework\TestCase +class ProbabilityRandomFunctionTest extends TestCase { - public function setUp() { + private TagReplacer $tagReplacer; + private ProbabilityRandomFunction $probabilityRandomFunction; + + public function setUp(): void + { $this->tagReplacer = new TagReplacer(); - $this->function = new ProbabilityRandomFunction($this->tagReplacer); + $this->probabilityRandomFunction = new ProbabilityRandomFunction($this->tagReplacer); } - public function testWithZeroArgument() + public function testWithZeroArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([], []); + $this->expectException(InvalidArgumentException::class); + $this->probabilityRandomFunction->execute([], []); } - public function testWithOneArgument() + public function testWithOneArgument(): void { - $result = $this->function->execute(['1:test'], ['1:test']); + $result = $this->probabilityRandomFunction->execute(['1:test'], ['1:test']); $this->assertEquals('test', $result); } - public function testWithTwoArguments() + public function testWithTwoArguments(): void { - $result = $this->function->execute(['2:test1', '8:test2'], ['2:test1', '8:test2']); + $result = $this->probabilityRandomFunction->execute(['2:test1', '8:test2'], ['2:test1', '8:test2']); $this->assertContains($result, ['test1', 'test2']); } - public function testWithStringThatContainsEmptyTag() + public function testWithStringThatContainsEmptyTag(): void { - $result = $this->function->execute(['9:test1' . $this->tagReplacer->getEmptyTag(), '1:test2'], ['9:test1' . $this->tagReplacer->getEmptyTag(), '1:test2']); + $result = $this->probabilityRandomFunction->execute(['9:test1' . $this->tagReplacer->getEmptyTag(), '1:test2'], ['9:test1' . $this->tagReplacer->getEmptyTag(), '1:test2']); $this->assertEquals('test2', $result); } - public function testWithStringThatContainsEmptyTag2() + public function testWithStringThatContainsEmptyTag2(): void { - $result = $this->function->execute(['9:test1' . $this->tagReplacer->getEmptyTag()], ['9:test1' . $this->tagReplacer->getEmptyTag()]); + $result = $this->probabilityRandomFunction->execute(['9:test1' . $this->tagReplacer->getEmptyTag()], ['9:test1' . $this->tagReplacer->getEmptyTag()]); $this->assertEquals('[EMPTY]', $result); } - public function testWithWrongProbabilities() + public function testWithWrongProbabilities(): void { - $result = $this->function->execute(['2:test1', 'test2'], ['2:test1', 'test2']); + $result = $this->probabilityRandomFunction->execute(['2:test1', 'test2'], ['2:test1', 'test2']); $this->assertContains($result, ['test1']); } - public function testWithWrongProbabilities2() + public function testWithWrongProbabilities2(): void { - $result = $this->function->execute(['test1', 'test2'], ['test1', 'test2']); + $result = $this->probabilityRandomFunction->execute(['test1', 'test2'], ['test1', 'test2']); $this->assertEquals('[EMPTY]', $result); } - public function testWithWrongProbabilities3() + public function testWithWrongProbabilities3(): void { - $result = $this->function->execute(['xx:test1', 'yy:test2'], ['xx:test1', 'yy:test2']); + $result = $this->probabilityRandomFunction->execute(['xx:test1', 'yy:test2'], ['xx:test1', 'yy:test2']); $this->assertEquals('[EMPTY]', $result); } - public function testWithWrongProbabilities4() + public function testWithWrongProbabilities4(): void { - $result = $this->function->execute(['-5:test1', '-8:test2'], ['-5:test1', '-8:test2']); + $result = $this->probabilityRandomFunction->execute(['-5:test1', '-8:test2'], ['-5:test1', '-8:test2']); $this->assertEquals('[EMPTY]', $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/RandomFunctionTest.php b/tests/TextGenerator/TextFunction/RandomFunctionTest.php index 995d12f..35b32df 100644 --- a/tests/TextGenerator/TextFunction/RandomFunctionTest.php +++ b/tests/TextGenerator/TextFunction/RandomFunctionTest.php @@ -2,36 +2,41 @@ namespace Neveldo\TextGenerator\Tag; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\RandomFunction; -class RandomFunctionTest extends \PHPUnit\Framework\TestCase +class RandomFunctionTest extends TestCase { - public function setUp() { + private TagReplacer $tagReplacer; + private RandomFunction $randomFunction; + + public function setUp(): void + { $this->tagReplacer = new TagReplacer(); - $this->function = new RandomFunction($this->tagReplacer); + $this->randomFunction = new RandomFunction($this->tagReplacer); } - public function testWithZeroArgument() + public function testWithZeroArgument(): void { - $result = $this->function->execute([], []); + $result = $this->randomFunction->execute([], []); $this->assertEquals('[EMPTY]', $result); } - public function testWithOneArgument() + public function testWithOneArgument(): void { - $result = $this->function->execute(['test'], ['test']); + $result = $this->randomFunction->execute(['test'], ['test']); $this->assertEquals('test', $result); } - public function testWithTwoArguments() + public function testWithTwoArguments(): void { - $result = $this->function->execute(['test1', 'test2'], ['test1', 'test2']); + $result = $this->randomFunction->execute(['test1', 'test2'], ['test1', 'test2']); $this->assertContains($result, ['test1', 'test2']); } - public function testWithStringThatContainsEmptyTag() + public function testWithStringThatContainsEmptyTag(): void { - $result = $this->function->execute(['test1' . $this->tagReplacer->getEmptyTag(), 'test2'], ['test1' . $this->tagReplacer->getEmptyTag(), 'test2']); + $result = $this->randomFunction->execute(['test1' . $this->tagReplacer->getEmptyTag(), 'test2'], ['test1' . $this->tagReplacer->getEmptyTag(), 'test2']); $this->assertEquals('test2', $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextFunction/SetFunctionTest.php b/tests/TextGenerator/TextFunction/SetFunctionTest.php index 7ca6a6c..839ae9d 100644 --- a/tests/TextGenerator/TextFunction/SetFunctionTest.php +++ b/tests/TextGenerator/TextFunction/SetFunctionTest.php @@ -2,27 +2,32 @@ namespace Neveldo\TextGenerator; -class SetFunctionTest extends \PHPUnit\Framework\TestCase +use PHPUnit\Framework\TestCase; + +class SetFunctionTest extends TestCase { - public function setUp() { + private TextGenerator $textGenerator; + + public function setUp(): void + { $this->textGenerator = new TextGenerator(); } - public function testSimpleAssignement() + public function testSimpleAssignement(): void { $this->textGenerator->compile("#set{@test|test 123.}@test"); $result = $this->textGenerator->generate([]); $this->assertEquals('test 123.', $result); } - public function testAssignementWithAnotherTag() + public function testAssignementWithAnotherTag(): void { $this->textGenerator->compile("#set{@test2|@test1 ipsum}@test2"); $result = $this->textGenerator->generate(['test1' => 'Lorem']); $this->assertEquals('Lorem ipsum', $result); } - public function testAssignementWithAnotherTag2() + public function testAssignementWithAnotherTag2(): void { $this->textGenerator->compile("#set{@test1|Lorem}#set{@test2|@test1 ipsum}@test2"); @@ -30,21 +35,21 @@ public function testAssignementWithAnotherTag2() $this->assertEquals('Lorem ipsum', $result); } - public function testTagOverwritting() + public function testTagOverwritting(): void { $this->textGenerator->compile("#set{@test|Lorem2}@test"); $result = $this->textGenerator->generate(['test' => 'Lorem1']); $this->assertEquals('Lorem2', $result); } - public function testTagOverwritting2() + public function testTagOverwritting2(): void { $this->textGenerator->compile("#set{@test|Lorem2}#set{@test|Lorem3}@test"); $result = $this->textGenerator->generate([]); $this->assertEquals('Lorem3', $result); } - public function testAssignementWithFunctionCall() + public function testAssignementWithFunctionCall(): void { $this->textGenerator->compile("#set{@test|#if{sex == 'm'|he|she}}@test"); $result = $this->textGenerator->generate(['sex' => 'f']); diff --git a/tests/TextGenerator/TextFunction/ShuffleFunctionTest.php b/tests/TextGenerator/TextFunction/ShuffleFunctionTest.php index d11263f..402e081 100644 --- a/tests/TextGenerator/TextFunction/ShuffleFunctionTest.php +++ b/tests/TextGenerator/TextFunction/ShuffleFunctionTest.php @@ -2,54 +2,60 @@ namespace Neveldo\TextGenerator\Tag; +use InvalidArgumentException; +use PHPUnit\Framework\TestCase; use Neveldo\TextGenerator\TextFunction\ShuffleFunction; -class ShuffleFunctionTest extends \PHPUnit\Framework\TestCase +class ShuffleFunctionTest extends TestCase { - public function setUp() { + private TagReplacer $tagReplacer; + private ShuffleFunction $shuffleFunction; + + public function setUp(): void + { $this->tagReplacer = new TagReplacer(); - $this->function = new ShuffleFunction($this->tagReplacer); + $this->shuffleFunction = new ShuffleFunction($this->tagReplacer); } - public function testWithZeroArgument() + public function testWithZeroArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([], []); + $this->expectException(InvalidArgumentException::class); + $this->shuffleFunction->execute([], []); } - public function testWithOneArgument() + public function testWithOneArgument(): void { - $this->expectException(\InvalidArgumentException::class); - $this->function->execute([','], [',']); + $this->expectException(InvalidArgumentException::class); + $this->shuffleFunction->execute([','], [',']); } - public function testWithOneString() + public function testWithOneString(): void { - $result = $this->function->execute([',', 'test'], [',', 'test']); + $result = $this->shuffleFunction->execute([',', 'test'], [',', 'test']); $this->assertEquals('test', $result); } - public function testWithTwoStrings() + public function testWithTwoStrings(): void { - $result = $this->function->execute([',', 'test1', 'test2'], [',', 'test1', 'test2']); + $result = $this->shuffleFunction->execute([',', 'test1', 'test2'], [',', 'test1', 'test2']); $this->assertContains($result, ['test1,test2', 'test2,test1']); } - public function testWithTwoStringsAndEmptySeparator() + public function testWithTwoStringsAndEmptySeparator(): void { - $result = $this->function->execute(['', 'test1', 'test2'], ['', 'test1', 'test2']); + $result = $this->shuffleFunction->execute(['', 'test1', 'test2'], ['', 'test1', 'test2']); $this->assertContains($result, ['test1test2', 'test2test1']); } - public function testWithOneEmptyString() + public function testWithOneEmptyString(): void { - $result = $this->function->execute([',', 'test1', ''], [',', 'test1', '']); + $result = $this->shuffleFunction->execute([',', 'test1', ''], [',', 'test1', '']); $this->assertEquals('test1', $result); } - public function testWithStringThatContainsEmptyTag() + public function testWithStringThatContainsEmptyTag(): void { - $result = $this->function->execute([',', 'test1' . $this->tagReplacer->getEmptyTag(), 'test2'], [',', 'test1' . $this->tagReplacer->getEmptyTag(), 'test2']); + $result = $this->shuffleFunction->execute([',', 'test1' . $this->tagReplacer->getEmptyTag(), 'test2'], [',', 'test1' . $this->tagReplacer->getEmptyTag(), 'test2']); $this->assertEquals('test2', $result); } -} \ No newline at end of file +} diff --git a/tests/TextGenerator/TextGeneratorTest.php b/tests/TextGenerator/TextGeneratorTest.php index ce83b52..ec48539 100644 --- a/tests/TextGenerator/TextGeneratorTest.php +++ b/tests/TextGenerator/TextGeneratorTest.php @@ -2,15 +2,18 @@ namespace Neveldo\TextGenerator; -echo "hihihihih"; +use PHPUnit\Framework\TestCase; -class TextGeneratorTest extends \PHPUnit\Framework\TestCase +class TextGeneratorTest extends TestCase { - public function setUp() { + private TextGenerator $textGenerator; + + public function setUp(): void + { $this->textGenerator = new TextGenerator(); } - public function testTagsReplacement() + public function testTagsReplacement(): void { $this->textGenerator->compile("Hello @firstname @lastname."); $result = $this->textGenerator->generate([ @@ -20,7 +23,7 @@ public function testTagsReplacement() $this->assertEquals('Hello John Doe.', $result); } - public function testTagsReplacementWithEmptyTag() + public function testTagsReplacementWithEmptyTag(): void { $this->textGenerator->compile("Hello @firstname @lastname."); $result = $this->textGenerator->generate([ @@ -30,62 +33,63 @@ public function testTagsReplacementWithEmptyTag() $this->assertEquals('Hello John .', $result); } - public function testRandom() + public function testRandom(): void { $this->textGenerator->compile("#random{Throughout|During|All along}"); $result = $this->textGenerator->generate([]); $this->assertContains($result, ['Throughout', 'During', 'All along']); } - public function testRandom2() + public function testRandom2(): void { $this->textGenerator->compile("#random{Throughout|}"); $result = $this->textGenerator->generate([]); $this->assertContains($result, ['Throughout', '']); } - public function testRandomWithEmptyArg() + public function testRandomWithEmptyArg(): void { $this->textGenerator->compile("#random{Throughout|test" . $this->textGenerator->getTagReplacer()->getEmptyTag() . "test}"); $result = $this->textGenerator->generate([]); $this->assertEquals($result, 'Throughout'); } - public function testShuffle() + public function testShuffle(): void { $this->textGenerator->compile("#shuffle{, |test1|test2}"); $result = $this->textGenerator->generate([]); $this->assertContains($result, ['test1, test2', 'test2, test1']); } - public function testShuffleWithEmptyArg() + public function testShuffleWithEmptyArg(): void { $this->textGenerator->compile("#shuffle{, |test1||test" . $this->textGenerator->getTagReplacer()->getEmptyTag() . "test|}"); $result = $this->textGenerator->generate([]); $this->assertEquals($result, 'test1'); } - public function testIf() + public function testIf(): void { $this->textGenerator->compile("#if{@sex == 'm'|actor|actress}"); $result = $this->textGenerator->generate(['sex' => 'm']); $this->assertEquals($result, 'actor'); } - public function testElse() + public function testElse(): void { $this->textGenerator->compile("#if{@sex == 'm'|actor|actress}"); $result = $this->textGenerator->generate(['sex' => 'f']); $this->assertEquals($result, 'actress'); } - public function testElseWithNoElse() { + public function testElseWithNoElse(): void + { $this->textGenerator->compile("#if{@sex == 'm'|actor}"); $result = $this->textGenerator->generate(['sex' => 'f']); $this->assertEquals($result, ''); } - public function testLoopWithThreeElements() + public function testLoopWithThreeElements(): void { $this->textGenerator->compile("#loop{@loop_tag|*|false|, | and |@var1 - @var2}"); $result = $this->textGenerator->generate([ @@ -108,7 +112,7 @@ public function testLoopWithThreeElements() $this->assertEquals($result, 'test1 - test2, test21 - test22 and test31 - test32'); } - public function testRandomLoopWithTwoElements() + public function testRandomLoopWithTwoElements(): void { $this->textGenerator->compile("#loop{@loop_tag|*|true|, | and |@var1 - @var2}"); $result = $this->textGenerator->generate([ @@ -126,14 +130,14 @@ public function testRandomLoopWithTwoElements() $this->assertContains($result, ['test21 - test22 and test1 - test2', 'test1 - test2 and test21 - test22']); } - public function testNestedFunctions() + public function testNestedFunctions(): void { $this->textGenerator->compile("#shuffle{, |one|#random{two|three}}"); $result = $this->textGenerator->generate([]); $this->assertContains($result, ['one, two', 'one, three', 'two, one', 'three, one']); } - public function testIdentation() + public function testIdentation(): void { $this->textGenerator->compile( "Test1 ;; Test2 ;; @@ -144,19 +148,19 @@ public function testIdentation() $this->assertEquals('Test1 Test2 Test3Test4', $result); } - public function testCoalesce() + public function testCoalesce(): void { $this->textGenerator->compile("#coalesce{@my_tag1|@my_tag2|@my_tag3|@my_tag4}"); $result = $this->textGenerator->generate([ 'my_tag1' => '', - 'my_tag2' => null, + 'my_tag2' => '', 'my_tag3' => 'Hello', 'my_tag4' => 'Hi', ]); $this->assertEquals($result, 'Hello'); } - public function testImbricatedFunctions() + public function testImbricatedFunctions(): void { $this->textGenerator->compile("Test imbricated set/filter#set{@my_tag1|#filter{round|3.55|1}0}. Test imbricated if/filter : #if{@my_tag1 == 3.6|ok #filter{round|@my_tag1} #filter{round|@my_tag1|1}|notok}."); $result = $this->textGenerator->generate([]);