1
|
|
|
<?php |
2
|
|
|
namespace Cowsayphp; |
3
|
|
|
|
4
|
|
|
abstract class AbstractAnimal implements AnimalInterface |
5
|
|
|
{ |
6
|
|
|
protected $character; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Make the animal speak. |
10
|
|
|
* @param $text string A string you want the animal says |
11
|
|
|
* @return string The animal speaks... |
12
|
|
|
*/ |
13
|
|
|
public function say($text) |
14
|
|
|
{ |
15
|
|
|
$message = $this->getSpeechBubble($text); |
16
|
|
|
$animal = str_replace('{{bubble}}', $message, $this->character); |
17
|
|
|
return $animal; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Obtain the message as array wrapping the text |
22
|
|
|
* @param $text |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
public function getMessageLines($text) |
26
|
|
|
{ |
27
|
|
|
$message = $text; |
28
|
|
|
$wrapLength = 40; |
29
|
|
|
// wrap the message to max chars |
30
|
|
|
$message = wordwrap($message, $wrapLength - 2); |
31
|
|
|
// split into array of lines |
32
|
|
|
return explode("\n", $message); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Find the longest line and get the line length |
37
|
|
|
* @param array $lines |
38
|
|
|
* @return int |
39
|
|
|
*/ |
40
|
|
|
public function getMaxLineLength(array $lines) |
41
|
|
|
{ |
42
|
|
|
$lineLength = 0; |
43
|
|
|
// find the longest line |
44
|
|
|
foreach ($lines as $line) { |
45
|
|
|
$currentLineLength = strlen($line); |
46
|
|
|
if ($currentLineLength > $lineLength) { |
47
|
|
|
$lineLength = $currentLineLength; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
return $lineLength; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Obtain the speech bubble. |
55
|
|
|
* @param $text |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getSpeechBubble($text) |
59
|
|
|
{ |
60
|
|
|
$lines = $this->getMessageLines($text); |
61
|
|
|
$lineLength = $this->getMaxLineLength($lines); |
62
|
|
|
$text = ''; |
|
|
|
|
63
|
|
|
$numberOfLines = count($lines); |
64
|
|
|
$firstLine = str_pad(array_shift($lines), $lineLength); |
65
|
|
|
if ($numberOfLines === 1) { |
66
|
|
|
$text = "< {$firstLine} >"; |
67
|
|
|
} else { |
68
|
|
|
$lastLine = str_pad(array_pop($lines), $lineLength); |
69
|
|
|
$text = "/ {$firstLine} \\\n"; |
70
|
|
|
foreach ($lines as $line) { |
71
|
|
|
$line = str_pad($line, $lineLength); |
72
|
|
|
$text .= "| {$line} |\n"; |
73
|
|
|
} |
74
|
|
|
$text .= "\\ {$lastLine} /"; |
75
|
|
|
} |
76
|
|
|
return $text; |
77
|
|
|
} |
78
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.