Refactors to satisfy phpstan level 8

This commit is contained in:
Roy Verrips 2022-01-07 15:00:48 -06:00 committed by Josh Sherman
parent 6e973db521
commit 7cebbfc06a

View file

@ -24,7 +24,7 @@ class LoremIpsum
* Whether or not we should be starting the string with "Lorem ipsum..." * Whether or not we should be starting the string with "Lorem ipsum..."
* *
* @access private * @access private
* @var boolean * @var mixed
*/ */
private $first = true; private $first = true;
@ -35,7 +35,7 @@ class LoremIpsum
* a complete list exists and if so, where to get it. * a complete list exists and if so, where to get it.
* *
* @access private * @access private
* @var array * @var array<string>
*/ */
private $words = array( private $words = array(
// Lorem ipsum... // Lorem ipsum...
@ -83,7 +83,7 @@ class LoremIpsum
*/ */
public function word($tags = false) public function word($tags = false)
{ {
return $this->words(1, $tags); return strval($this->words(1, $tags));
} }
/** /**
@ -94,7 +94,7 @@ class LoremIpsum
* @access public * @access public
* @param integer $count how many words to generate * @param integer $count how many words to generate
* @param mixed $tags string or array of HTML tags to wrap output with * @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) public function wordsArray($count = 1, $tags = false)
{ {
@ -152,7 +152,7 @@ class LoremIpsum
*/ */
public function sentence($tags = false) public function sentence($tags = false)
{ {
return $this->sentences(1, $tags); return strval($this->sentences(1, $tags));
} }
/** /**
@ -163,7 +163,7 @@ class LoremIpsum
* @access public * @access public
* @param integer $count how many sentences to generate * @param integer $count how many sentences to generate
* @param mixed $tags string or array of HTML tags to wrap output with * @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) public function sentencesArray($count = 1, $tags = false)
{ {
@ -205,7 +205,7 @@ class LoremIpsum
*/ */
public function paragraph($tags = false) public function paragraph($tags = false)
{ {
return $this->paragraphs(1, $tags); return strval($this->paragraphs(1, $tags));
} }
/** /**
@ -216,15 +216,18 @@ class LoremIpsum
* @access public * @access public
* @param integer $count how many paragraphs to generate * @param integer $count how many paragraphs to generate
* @param mixed $tags string or array of HTML tags to wrap output with * @param mixed $tags string or array of HTML tags to wrap output with
* @return array generated lorem ipsum paragraphs * @return array<string> generated lorem ipsum paragraphs
*/ */
public function paragraphsArray($count = 1, $tags = false) 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); return $this->paragraphs($count, $tags, true);
} }
/** /**
* Paragraphss * Paragraphs
* *
* Generates paragraphs of lorem ipsum. * Generates paragraphs of lorem ipsum.
* *
@ -239,10 +242,13 @@ class LoremIpsum
$paragraphs = array(); $paragraphs = array();
for ($i = 0; $i < $count; $i++) { 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 * @access private
* @param double $mean average value * @param double $mean average value
* @param double $std_dev stadnard deviation * @param double $std_dev stadnard deviation
* @return double calculated distribution * @return int calculated distribution
*/ */
private function gauss($mean, $std_dev) private function gauss($mean, $std_dev)
{ {
@ -264,7 +270,7 @@ class LoremIpsum
$y = mt_rand() / mt_getrandmax(); $y = mt_rand() / mt_getrandmax();
$z = sqrt(-2 * log($x)) * cos(2 * pi() * $y); $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. * the first time we are generating the text.
* *
* @access private * @access private
* @return void
*/ */
private function shuffle() private function shuffle()
{ {
@ -299,18 +306,19 @@ class LoremIpsum
* first word of the sentence. * first word of the sentence.
* *
* @access private * @access private
* @param array $sentences the sentences we would like to punctuate * @param array<string> $sentences the sentences we would like to punctuate
* @return void
*/ */
private function punctuate(&$sentences) private function punctuate(&$sentences)
{ {
foreach ($sentences as $key => $sentence) { foreach ($sentences as $key => $sentence) {
$words = count($sentence); $words = count($sentences);
// Only worry about commas on sentences longer than 4 words // Only worry about commas on sentences longer than 4 words
if ($words > 4) { if ($words > 4) {
$mean = log($words, 6); $mean = log($words, 6);
$std_dev = $mean / 6; $std_dev = $mean / 6;
$commas = round($this->gauss($mean, $std_dev)); $commas = $this->gauss($mean, $std_dev);
for ($i = 1; $i <= $commas; $i++) { for ($i = 1; $i <= $commas; $i++) {
$word = round($i * $words / ($commas + 1)); $word = round($i * $words / ($commas + 1));
@ -334,7 +342,7 @@ class LoremIpsum
* into a string or not. * into a string or not.
* *
* @access private * @access private
* @param array $strings an array of generated strings * @param array<string> $strings an array of generated strings
* @param mixed $tags string or array of HTML tags to wrap output with * @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 boolean $array whether an array or a string should be returned
* @param string $delimiter the string to use when calling implode() * @param string $delimiter the string to use when calling implode()
@ -373,4 +381,3 @@ class LoremIpsum
return $strings; return $strings;
} }
} }