Completed
Push — master ( 06fafe...34abd7 )
by Marcos Sigueros
14:42
created

AbstractAnimal::getSpeechBubble()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 16
nc 2
nop 1
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 = '';
0 ignored issues
show
Unused Code introduced by
$text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
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
}