From 63a52c853bff27d00e23d9506e25b6f1c3b21007 Mon Sep 17 00:00:00 2001 From: Roy Verrips Date: Wed, 5 Jan 2022 23:21:49 -0600 Subject: [PATCH 1/5] PHP 8.1: Implicit incompatible float to int conversion is deprecated In PHP 8.1 floats are not implicitly converted to integers as before. The array_slice on line 139 requires an integer as the third parameter (the $count variable) so added line 117 to do the conversion of $count. --- src/LoremIpsum.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/LoremIpsum.php b/src/LoremIpsum.php index a48304b..487610b 100644 --- a/src/LoremIpsum.php +++ b/src/LoremIpsum.php @@ -114,6 +114,7 @@ class LoremIpsum */ public function words($count = 1, $tags = false, $array = false) { + $count = (int) $count; $words = array(); $word_count = 0; From 6e973db52189f16c76d6ee9702fac526daadae5f Mon Sep 17 00:00:00 2001 From: Roy Verrips Date: Fri, 7 Jan 2022 11:52:46 -0600 Subject: [PATCH 2/5] Add phpstan with config set to max --- composer.json | 3 ++- phpstan.neon | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index da7781c..0ff53d6 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "php": ">=5.3" }, "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^9.0" + "phpunit/phpunit": "^4.8.36 || ^9.0", + "phpstan/phpstan": "^1.3" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..0292a3b --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,14 @@ +parameters: + + paths: + - src + + # The level 8 is the highest level + level: max + + excludePaths: + - ./*/*/FileToBeExcluded.php + + checkMissingIterableValueType: true + + #editorUrl: 'vscode://file/%%file%%:%%line%%' From 7cebbfc06ae116993314f801bd10365321fa0596 Mon Sep 17 00:00:00 2001 From: Roy Verrips Date: Fri, 7 Jan 2022 15:00:48 -0600 Subject: [PATCH 3/5] Refactors to satisfy phpstan level 8 --- src/LoremIpsum.php | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/LoremIpsum.php b/src/LoremIpsum.php index 487610b..5085b4f 100644 --- a/src/LoremIpsum.php +++ b/src/LoremIpsum.php @@ -24,7 +24,7 @@ class LoremIpsum * Whether or not we should be starting the string with "Lorem ipsum..." * * @access private - * @var boolean + * @var mixed */ private $first = true; @@ -35,7 +35,7 @@ class LoremIpsum * a complete list exists and if so, where to get it. * * @access private - * @var array + * @var array */ private $words = array( // Lorem ipsum... @@ -83,7 +83,7 @@ class LoremIpsum */ public function word($tags = false) { - return $this->words(1, $tags); + return strval($this->words(1, $tags)); } /** @@ -94,7 +94,7 @@ class LoremIpsum * @access public * @param integer $count how many words to generate * @param mixed $tags string or array of HTML tags to wrap output with - * @return array generated lorem ipsum words + * @return mixed generated lorem ipsum words */ public function wordsArray($count = 1, $tags = false) { @@ -152,7 +152,7 @@ class LoremIpsum */ public function sentence($tags = false) { - return $this->sentences(1, $tags); + return strval($this->sentences(1, $tags)); } /** @@ -163,7 +163,7 @@ class LoremIpsum * @access public * @param integer $count how many sentences to generate * @param mixed $tags string or array of HTML tags to wrap output with - * @return array generated lorem ipsum sentences + * @return mixed generated lorem ipsum sentences */ public function sentencesArray($count = 1, $tags = false) { @@ -205,7 +205,7 @@ class LoremIpsum */ public function paragraph($tags = false) { - return $this->paragraphs(1, $tags); + return strval($this->paragraphs(1, $tags)); } /** @@ -216,15 +216,18 @@ class LoremIpsum * @access public * @param integer $count how many paragraphs to generate * @param mixed $tags string or array of HTML tags to wrap output with - * @return array generated lorem ipsum paragraphs + * @return array generated lorem ipsum paragraphs */ public function paragraphsArray($count = 1, $tags = false) { + // The $array parameter set to true means an array is returned. + // Return type is mixed, we should probably cast to array. + // @phpstan-ignore-next-line return $this->paragraphs($count, $tags, true); } /** - * Paragraphss + * Paragraphs * * Generates paragraphs of lorem ipsum. * @@ -239,10 +242,13 @@ class LoremIpsum $paragraphs = array(); for ($i = 0; $i < $count; $i++) { - $paragraphs[] = $this->sentences($this->gauss(5.8, 1.93)); + $paragraphs[] = strval($this->sentences($this->gauss(5.8, 1.93))); } - return $this->output($paragraphs, $tags, $array, "\n\n"); + if ($array) { + return $this->output($paragraphs, $tags, $array, "\n\n"); + } + return strval($this->output($paragraphs, $tags, false, "\n\n")); } /** @@ -256,7 +262,7 @@ class LoremIpsum * @access private * @param double $mean average value * @param double $std_dev stadnard deviation - * @return double calculated distribution + * @return int calculated distribution */ private function gauss($mean, $std_dev) { @@ -264,7 +270,7 @@ class LoremIpsum $y = mt_rand() / mt_getrandmax(); $z = sqrt(-2 * log($x)) * cos(2 * pi() * $y); - return $z * $std_dev + $mean; + return intval($z * $std_dev + $mean); } /** @@ -274,6 +280,7 @@ class LoremIpsum * the first time we are generating the text. * * @access private + * @return void */ private function shuffle() { @@ -299,18 +306,19 @@ class LoremIpsum * first word of the sentence. * * @access private - * @param array $sentences the sentences we would like to punctuate + * @param array $sentences the sentences we would like to punctuate + * @return void */ private function punctuate(&$sentences) { foreach ($sentences as $key => $sentence) { - $words = count($sentence); + $words = count($sentences); // Only worry about commas on sentences longer than 4 words if ($words > 4) { $mean = log($words, 6); $std_dev = $mean / 6; - $commas = round($this->gauss($mean, $std_dev)); + $commas = $this->gauss($mean, $std_dev); for ($i = 1; $i <= $commas; $i++) { $word = round($i * $words / ($commas + 1)); @@ -334,7 +342,7 @@ class LoremIpsum * into a string or not. * * @access private - * @param array $strings an array of generated strings + * @param array $strings an array of generated strings * @param mixed $tags string or array of HTML tags to wrap output with * @param boolean $array whether an array or a string should be returned * @param string $delimiter the string to use when calling implode() @@ -373,4 +381,3 @@ class LoremIpsum return $strings; } } - From a84cdd8917e90f096ed1978d4b760ca77d803aaf Mon Sep 17 00:00:00 2001 From: Roy Verrips Date: Fri, 7 Jan 2022 18:19:11 -0600 Subject: [PATCH 4/5] Remove phpstan from dependancies - want to keep this compatible with php 7.0 as per github build steps --- composer.json | 63 +++++++++++++++++++++++----------------------- src/LoremIpsum.php | 3 +-- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/composer.json b/composer.json index 0ff53d6..8141871 100644 --- a/composer.json +++ b/composer.json @@ -1,35 +1,34 @@ { - "name": "joshtronic/php-loremipsum", - "description": "Lorem ipsum generator in PHP without dependencies", - "version": "2.0.0", - "type": "library", - "keywords": [ - "lorem", - "ipsum", - "generator" - ], - "homepage": "https://github.com/joshtronic/php-loremipsum", - "license": "MIT", - "authors": [{ - "name": "Josh Sherman", - "email": "hello@joshtronic.com", - "homepage": "https://joshtronic.com" - }], - "require": { - "php": ">=5.3" - }, - "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^9.0", - "phpstan/phpstan": "^1.3" - }, - "autoload": { - "psr-4": { - "joshtronic\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "joshtronic\\Tests\\": "tests/" + "name": "joshtronic/php-loremipsum", + "description": "Lorem ipsum generator in PHP without dependencies", + "version": "2.0.0", + "type": "library", + "keywords": [ + "lorem", + "ipsum", + "generator" + ], + "homepage": "https://github.com/joshtronic/php-loremipsum", + "license": "MIT", + "authors": [{ + "name": "Josh Sherman", + "email": "hello@joshtronic.com", + "homepage": "https://joshtronic.com" + }], + "require": { + "php": ">=5.3" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.36 || ^9.0" + }, + "autoload": { + "psr-4": { + "joshtronic\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "joshtronic\\Tests\\": "tests/" + } } } -} diff --git a/src/LoremIpsum.php b/src/LoremIpsum.php index 5085b4f..863b8d5 100644 --- a/src/LoremIpsum.php +++ b/src/LoremIpsum.php @@ -312,8 +312,7 @@ class LoremIpsum private function punctuate(&$sentences) { foreach ($sentences as $key => $sentence) { - $words = count($sentences); - + $words = count($sentence); // Only worry about commas on sentences longer than 4 words if ($words > 4) { $mean = log($words, 6); From 6d7f8cff6a092a53d66b5237edbe97d398b14f88 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 23 Jan 2022 17:27:44 -0600 Subject: [PATCH 5/5] chore: bump version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8141871..a28a4b3 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "joshtronic/php-loremipsum", "description": "Lorem ipsum generator in PHP without dependencies", - "version": "2.0.0", + "version": "2.1.0", "type": "library", "keywords": [ "lorem",