GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b3ee5f...a83cd4 )
by Brad
06:08 queued 03:01
created

FooGallery_LoremIpsum::output()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 4
dl 0
loc 30
rs 8.5066
c 0
b 0
f 0
1
<?php
2
/**
3
 * Lorem Ipsum Generator
4
 *
5
 * PHP version 5.3+
6
 *
7
 * Licensed under The MIT License.
8
 * Redistribution of these files must retain the above copyright notice.
9
 *
10
 * @author    Josh Sherman <[email protected]>
11
 * @copyright Copyright 2014, 2015, 2016, 2017, 2018 Josh Sherman
12
 * @license   http://www.opensource.org/licenses/mit-license.html
13
 * @link      https://github.com/joshtronic/php-loremipsum
14
 */
15
16
class FooGallery_LoremIpsum
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
	/**
19
	 * First
20
	 *
21
	 * Whether or not we should be starting the string with "Lorem ipsum..."
22
	 *
23
	 * @access private
24
	 * @var    boolean
25
	 */
26
	private $first = true;
27
28
	/**
29
	 * Words
30
	 *
31
	 * A lorem ipsum vocabulary of sorts. Not a complete list as I'm unsure if
32
	 * a complete list exists and if so, where to get it.
33
	 *
34
	 * @access private
35
	 * @var    array
36
	 */
37
	public $words = array(
38
		// Lorem ipsum...
39
		'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit',
40
41
		// and the rest of the vocabulary
42
		'a', 'ac', 'accumsan', 'ad', 'aenean', 'aliquam', 'aliquet', 'ante',
43
		'aptent', 'arcu', 'at', 'auctor', 'augue', 'bibendum', 'blandit',
44
		'class', 'commodo', 'condimentum', 'congue', 'consequat', 'conubia',
45
		'convallis', 'cras', 'cubilia', 'curabitur', 'curae', 'cursus',
46
		'dapibus', 'diam', 'dictum', 'dictumst', 'dignissim', 'dis', 'donec',
47
		'dui', 'duis', 'efficitur', 'egestas', 'eget', 'eleifend', 'elementum',
48
		'enim', 'erat', 'eros', 'est', 'et', 'etiam', 'eu', 'euismod', 'ex',
49
		'facilisi', 'facilisis', 'fames', 'faucibus', 'felis', 'fermentum',
50
		'feugiat', 'finibus', 'fringilla', 'fusce', 'gravida', 'habitant',
51
		'habitasse', 'hac', 'hendrerit', 'himenaeos', 'iaculis', 'id',
52
		'imperdiet', 'in', 'inceptos', 'integer', 'interdum', 'justo',
53
		'lacinia', 'lacus', 'laoreet', 'lectus', 'leo', 'libero', 'ligula',
54
		'litora', 'lobortis', 'luctus', 'maecenas', 'magna', 'magnis',
55
		'malesuada', 'massa', 'mattis', 'mauris', 'maximus', 'metus', 'mi',
56
		'molestie', 'mollis', 'montes', 'morbi', 'mus', 'nam', 'nascetur',
57
		'natoque', 'nec', 'neque', 'netus', 'nibh', 'nisi', 'nisl', 'non',
58
		'nostra', 'nulla', 'nullam', 'nunc', 'odio', 'orci', 'ornare',
59
		'parturient', 'pellentesque', 'penatibus', 'per', 'pharetra',
60
		'phasellus', 'placerat', 'platea', 'porta', 'porttitor', 'posuere',
61
		'potenti', 'praesent', 'pretium', 'primis', 'proin', 'pulvinar',
62
		'purus', 'quam', 'quis', 'quisque', 'rhoncus', 'ridiculus', 'risus',
63
		'rutrum', 'sagittis', 'sapien', 'scelerisque', 'sed', 'sem', 'semper',
64
		'senectus', 'sociosqu', 'sodales', 'sollicitudin', 'suscipit',
65
		'suspendisse', 'taciti', 'tellus', 'tempor', 'tempus', 'tincidunt',
66
		'torquent', 'tortor', 'tristique', 'turpis', 'ullamcorper', 'ultrices',
67
		'ultricies', 'urna', 'ut', 'varius', 'vehicula', 'vel', 'velit',
68
		'venenatis', 'vestibulum', 'vitae', 'vivamus', 'viverra', 'volutpat',
69
		'vulputate',
70
	);
71
72
	/**
73
	 * Word
74
	 *
75
	 * Generates a single word of lorem ipsum.
76
	 *
77
	 * @access public
78
	 * @param  mixed  $tags string or array of HTML tags to wrap output with
79
	 * @return string generated lorem ipsum word
80
	 */
81
	public function word($tags = false)
82
	{
83
		return $this->words(1, $tags);
84
	}
85
86
	/**
87
	 * Words Array
88
	 *
89
	 * Generates an array of lorem ipsum words.
90
	 *
91
	 * @access public
92
	 * @param  integer $count how many words to generate
93
	 * @param  mixed   $tags string or array of HTML tags to wrap output with
94
	 * @return array   generated lorem ipsum words
95
	 */
96
	public function wordsArray($count = 1, $tags = false)
97
	{
98
		return $this->words($count, $tags, true);
99
	}
100
101
	/**
102
	 * Words
103
	 *
104
	 * Generates words of lorem ipsum.
105
	 *
106
	 * @access public
107
	 * @param  integer $count how many words to generate
108
	 * @param  mixed   $tags string or array of HTML tags to wrap output with
109
	 * @param  boolean $array whether an array or a string should be returned
110
	 * @return mixed   string or array of generated lorem ipsum words
111
	 */
112
	public function words($count = 1, $tags = false, $array = false)
113
	{
114
		$words      = array();
115
		$word_count = 0;
116
117
		// Shuffles and appends the word list to compensate for count
118
		// arguments that exceed the size of our vocabulary list
119
		while ($word_count < $count) {
120
			$shuffle = true;
121
122
			while ($shuffle) {
123
				$this->shuffle();
124
125
				// Checks that the last word of the list and the first word of
126
				// the list that's about to be appended are not the same
127
				if (!$word_count || $words[$word_count - 1] != $this->words[0]) {
128
					$words      = array_merge($words, $this->words);
129
					$word_count = count($words);
130
					$shuffle    = false;
131
				}
132
			}
133
		}
134
135
		$words = array_slice($words, 0, $count);
136
137
		return $this->output($words, $tags, $array);
138
	}
139
140
	/**
141
	 * Sentence
142
	 *
143
	 * Generates a full sentence of lorem ipsum.
144
	 *
145
	 * @access public
146
	 * @param  mixed  $tags string or array of HTML tags to wrap output with
147
	 * @return string generated lorem ipsum sentence
148
	 */
149
	public function sentence($tags = false)
150
	{
151
		return $this->sentences(1, $tags);
152
	}
153
154
	/**
155
	 * Sentences Array
156
	 *
157
	 * Generates an array of lorem ipsum sentences.
158
	 *
159
	 * @access public
160
	 * @param  integer $count how many sentences to generate
161
	 * @param  mixed   $tags string or array of HTML tags to wrap output with
162
	 * @return array   generated lorem ipsum sentences
163
	 */
164
	public function sentencesArray($count = 1, $tags = false)
165
	{
166
		return $this->sentences($count, $tags, true);
167
	}
168
169
	/**
170
	 * Sentences
171
	 *
172
	 * Generates sentences of lorem ipsum.
173
	 *
174
	 * @access public
175
	 * @param  integer $count how many sentences to generate
176
	 * @param  mixed   $tags string or array of HTML tags to wrap output with
177
	 * @param  boolean $array whether an array or a string should be returned
178
	 * @return mixed   string or array of generated lorem ipsum sentences
179
	 */
180
	public function sentences($count = 1, $tags = false, $array = false)
181
	{
182
		$sentences = array();
183
184
		for ($i = 0; $i < $count; $i++) {
185
			$sentences[] = $this->wordsArray($this->gauss(24.46, 5.08));
186
		}
187
188
		$this->punctuate($sentences);
189
190
		return $this->output($sentences, $tags, $array);
191
	}
192
193
	/**
194
	 * Paragraph
195
	 *
196
	 * Generates a full paragraph of lorem ipsum.
197
	 *
198
	 * @access public
199
	 * @param  mixed  $tags string or array of HTML tags to wrap output with
200
	 * @return string generated lorem ipsum paragraph
201
	 */
202
	public function paragraph($tags = false)
203
	{
204
		return $this->paragraphs(1, $tags);
205
	}
206
207
	/**
208
	 * Paragraph Array
209
	 *
210
	 * Generates an array of lorem ipsum paragraphs.
211
	 *
212
	 * @access public
213
	 * @param  integer $count how many paragraphs to generate
214
	 * @param  mixed   $tags string or array of HTML tags to wrap output with
215
	 * @return array   generated lorem ipsum paragraphs
216
	 */
217
	public function paragraphsArray($count = 1, $tags = false)
218
	{
219
		return $this->paragraphs($count, $tags, true);
220
	}
221
222
	/**
223
	 * Paragraphss
224
	 *
225
	 * Generates paragraphs of lorem ipsum.
226
	 *
227
	 * @access public
228
	 * @param  integer $count how many paragraphs to generate
229
	 * @param  mixed   $tags string or array of HTML tags to wrap output with
230
	 * @param  boolean $array whether an array or a string should be returned
231
	 * @return mixed   string or array of generated lorem ipsum paragraphs
232
	 */
233
	public function paragraphs($count = 1, $tags = false, $array = false)
234
	{
235
		$paragraphs = array();
236
237
		for ($i = 0; $i < $count; $i++) {
238
			$paragraphs[] = $this->sentences($this->gauss(5.8, 1.93));
239
		}
240
241
		return $this->output($paragraphs, $tags, $array, "\n\n");
242
	}
243
244
	/**
245
	 * Gaussian Distribution
246
	 *
247
	 * This is some smart kid stuff. I went ahead and combined the N(0,1) logic
248
	 * with the N(m,s) logic into this single function. Used to calculate the
249
	 * number of words in a sentence, the number of sentences in a paragraph
250
	 * and the distribution of commas in a sentence.
251
	 *
252
	 * @access private
253
	 * @param  double  $mean average value
254
	 * @param  double  $std_dev stadnard deviation
255
	 * @return double  calculated distribution
256
	 */
257
	private function gauss($mean, $std_dev)
258
	{
259
		$x = mt_rand() / mt_getrandmax();
260
		$y = mt_rand() / mt_getrandmax();
261
		$z = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
262
263
		return $z * $std_dev + $mean;
264
	}
265
266
	/**
267
	 * Shuffle
268
	 *
269
	 * Shuffles the words, forcing "Lorem ipsum..." at the beginning if it is
270
	 * the first time we are generating the text.
271
	 *
272
	 * @access private
273
	 */
274
	private function shuffle()
275
	{
276
		if ($this->first) {
277
			$this->first = array_slice($this->words, 0, 8);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_slice($this->words, 0, 8) of type array is incompatible with the declared type boolean of property $first.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
278
			$this->words = array_slice($this->words, 8);
279
280
			shuffle($this->words);
281
282
			$this->words = $this->first + $this->words;
283
284
			$this->first = false;
285
		} else {
286
			shuffle($this->words);
287
		}
288
	}
289
290
	/**
291
	 * Punctuate
292
	 *
293
	 * Applies punctuation to a sentence. This includes a period at the end,
294
	 * the injection of commas as well as capitalizing the first letter of the
295
	 * first word of the sentence.
296
	 *
297
	 * @access private
298
	 * @param  array   $sentences the sentences we would like to punctuate
299
	 */
300
	private function punctuate(&$sentences)
301
	{
302
		foreach ($sentences as $key => $sentence) {
303
			$words = count($sentence);
304
305
			// Only worry about commas on sentences longer than 4 words
306
			if ($words > 4) {
307
				$mean    = log($words, 6);
308
				$std_dev = $mean / 6;
309
				$commas  = round($this->gauss($mean, $std_dev));
310
311
				for ($i = 1; $i <= $commas; $i++) {
312
					$word = round($i * $words / ($commas + 1));
313
314
					if ($word < ($words - 1) && $word > 0) {
315
						$sentence[$word] .= ',';
316
					}
317
				}
318
			}
319
320
			$sentences[$key] = ucfirst(implode(' ', $sentence) . '.');
321
		}
322
	}
323
324
	/**
325
	 * Output
326
	 *
327
	 * Does the rest of the processing of the strings. This includes wrapping
328
	 * the strings in HTML tags, handling transformations with the ability of
329
	 * back referencing and determining if the passed array should be converted
330
	 * into a string or not.
331
	 *
332
	 * @access private
333
	 * @param  array   $strings an array of generated strings
334
	 * @param  mixed   $tags string or array of HTML tags to wrap output with
335
	 * @param  boolean $array whether an array or a string should be returned
336
	 * @param  string  $delimiter the string to use when calling implode()
337
	 * @return mixed   string or array of generated lorem ipsum text
338
	 */
339
	private function output($strings, $tags, $array, $delimiter = ' ')
340
	{
341
		if ($tags) {
342
			if (!is_array($tags)) {
343
				$tags = array($tags);
344
			} else {
345
				// Flips the array so we can work from the inside out
346
				$tags = array_reverse($tags);
347
			}
348
349
			foreach ($strings as $key => $string) {
350
				foreach ($tags as $tag) {
351
					// Detects / applies back reference
352
					if ($tag[0] == '<') {
353
						$string = str_replace('$1', $string, $tag);
354
					} else {
355
						$string = sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
356
					}
357
358
					$strings[$key] = $string;
359
				}
360
			}
361
		}
362
363
		if (!$array) {
364
			$strings = implode($delimiter, $strings);
365
		}
366
367
		return $strings;
368
	}
369
}
370