Completed
Pull Request — master (#1)
by Harry
02:24
created

Wrapper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 22.33 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 23
loc 103
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getWidth() 0 4 1
A setWidth() 0 5 1
A wrap() 12 12 3
A trim() 11 11 2
C chunk() 0 28 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Graze\BufferedConsole\Wrap;
4
5
use Graze\BufferedConsole\Terminal\DimensionsInterface;
6
7
class Wrapper
8
{
9
    /** @var int */
10
    private $width = DimensionsInterface::DEFAULT_WIDTH;
11
12
    /**
13
     * Wrapper constructor.
14
     *
15
     * @param int $width
16
     */
17
    public function __construct($width)
18
    {
19
        $this->width = $width;
20
    }
21
22
    /**
23
     * @return int
24
     */
25
    public function getWidth()
26
    {
27
        return $this->width;
28
    }
29
30
    /**
31
     * @param int $width
32
     *
33
     * @return $this
34
     */
35
    public function setWidth($width)
36
    {
37
        $this->width = $width;
38
        return $this;
39
    }
40
41
    /**
42
     * @param string|string[] $input
43
     *
44
     * @return string[]
45
     */
46 View Code Duplication
    public function wrap($input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $input = (array) $input;
49
50
        $out = [];
51
        foreach ($input as $line) {
52
            foreach ($this->chunk($line) as $new) {
53
                $out[] = $new;
54
            }
55
        }
56
        return $out;
57
    }
58
59
    /**
60
     * @param string|string[] $input
61
     *
62
     * @return string[]
63
     */
64 View Code Duplication
    public function trim($input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        $input = (array) $input;
67
68
        $out = [];
69
        foreach ($input as $line) {
70
            $chunk = $this->chunk($line);
71
            $out[] = reset($chunk);
72
        }
73
        return $out;
74
    }
75
76
    /**
77
     * @param string $line
78
     *
79
     * @return string[]
80
     */
81
    private function chunk($line)
82
    {
83
        if (mb_strlen($line) <= $this->width) {
84
            return [$line];
85
        }
86
        $stripped = strip_tags($line);
87
        $offset = 0;
88
        $out = [];
89
90
        // create a stripped tags version of the string
91
        // loop through both and only move the counter if both characters are equal
92
        // yield when we get to <width> for the stripped tags version
93
        for ($i = 0, $j = 0; $i <= mb_strlen($stripped); $j++) {
94
            if (mb_substr($stripped, $i, 1) === mb_substr($line, $j, 1)) {
95
                if ($i >= $this->width && $i % $this->width == 0) {
96
                    $out[] = mb_substr($line, $offset, $j - $offset);
97
                    $offset = $j;
98
                }
99
                $i++;
100
            }
101
        }
102
103
        // return any remaining entries
104
        if ($offset != $j - 1) {
105
            $out[] = mb_substr($line, $offset);
106
        }
107
        return $out;
108
    }
109
}
110