Completed
Push — master ( 910c9d...a4afb1 )
by Harry
02:15
created

Wrapper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 19.19 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 19
loc 99
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() 10 10 3
A trim() 9 9 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
 * This file is part of graze/buffered-console.
4
 *
5
 * Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/buffered-console/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/buffered-console
12
 */
13
14
namespace Graze\BufferedConsole\Wrap;
15
16
use Graze\BufferedConsole\Terminal\DimensionsInterface;
17
18
class Wrapper
19
{
20
    /** @var int */
21
    private $width = DimensionsInterface::DEFAULT_WIDTH;
22
23
    /**
24
     * Wrapper constructor.
25
     *
26
     * @param int $width
27
     */
28
    public function __construct($width)
29
    {
30
        $this->width = $width;
31
    }
32
33
    /**
34
     * @return int
35
     */
36
    public function getWidth()
37
    {
38
        return $this->width;
39
    }
40
41
    /**
42
     * @param int $width
43
     *
44
     * @return $this
45
     */
46
    public function setWidth($width)
47
    {
48
        $this->width = $width;
49
        return $this;
50
    }
51
52
    /**
53
     * @param string[] $input
54
     *
55
     * @return string[]
56
     */
57 View Code Duplication
    public function wrap(array $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...
58
    {
59
        $out = [];
60
        foreach ($input as $line) {
61
            foreach ($this->chunk($line) as $new) {
62
                $out[] = $new;
63
            }
64
        }
65
        return $out;
66
    }
67
68
    /**
69
     * @param string[] $input
70
     *
71
     * @return string[]
72
     */
73 View Code Duplication
    public function trim(array $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...
74
    {
75
        $out = [];
76
        foreach ($input as $line) {
77
            $chunk = $this->chunk($line);
78
            $out[] = reset($chunk);
79
        }
80
        return $out;
81
    }
82
83
    /**
84
     * @param string $line
85
     *
86
     * @return string[]
87
     */
88
    private function chunk($line)
89
    {
90
        if (mb_strlen($line) <= $this->width) {
91
            return [$line];
92
        }
93
        $stripped = strip_tags($line);
94
        $offset = 0;
95
        $out = [];
96
97
        // create a stripped tags version of the string
98
        // loop through both and only move the counter if both characters are equal
99
        // yield when we get to <width> for the stripped tags version
100
        for ($i = 0, $j = 0; $i <= mb_strlen($stripped); $j++) {
101
            if (mb_substr($stripped, $i, 1) === mb_substr($line, $j, 1)) {
102
                if ($i >= $this->width && $i % $this->width == 0) {
103
                    $out[] = mb_substr($line, $offset, $j - $offset);
104
                    $offset = $j;
105
                }
106
                $i++;
107
            }
108
        }
109
110
        // return any remaining entries
111
        if ($offset != $j - 1) {
112
            $out[] = mb_substr($line, $offset);
113
        }
114
        return $out;
115
    }
116
}
117