Completed
Pull Request — master (#9)
by Harry
02:43
created

Wrapper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 21.59 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 19
loc 88
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getWidth() 0 4 1
A wrap() 10 10 3
A trim() 9 9 2
C chunk() 0 26 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/console-diff-renderer.
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/console-diff-renderer/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/console-diff-renderer
12
 */
13
14
namespace Graze\DiffRenderer\Wrap;
15
16
use Graze\DiffRenderer\Terminal\TerminalInterface;
17
18
class Wrapper
19
{
20
    const REPLACEMENT_CHAR = "\6";
21
22
    /** @var TerminalInterface */
23
    private $terminal;
24
25
    /**
26
     * Wrapper constructor.
27
     *
28
     * @param TerminalInterface $terminal
29
     */
30
    public function __construct(TerminalInterface $terminal)
31
    {
32
        $this->terminal = $terminal;
33
    }
34
35
    /**
36
     * @return int
37
     */
38
    public function getWidth()
39
    {
40
        return $this->terminal->getWidth();
41
    }
42
43
    /**
44
     * @param string[] $input
45
     *
46
     * @return string[]
47
     */
48 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...
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[] $input
61
     *
62
     * @return string[]
63
     */
64 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...
65
    {
66
        $out = [];
67
        foreach ($input as $line) {
68
            $chunk = $this->chunk($line);
69
            $out[] = reset($chunk);
70
        }
71
        return $out;
72
    }
73
74
    /**
75
     * @param string $line
76
     *
77
     * @return string[]
78
     */
79
    private function chunk($line)
80
    {
81
        $width = $this->getWidth();
82
        if (mb_strlen($line) <= $width) {
83
            return [$line];
84
        }
85
        $stripped = $this->terminal->filter($line, static::REPLACEMENT_CHAR);
86
        $offset = 0;
87
        $out = [];
88
89
        for ($i = 0, $j = 0; $i <= mb_strlen($stripped); $i++) {
90
            if (mb_substr($stripped, $i, 1) != static::REPLACEMENT_CHAR) {
91
                if ($j > 0 && $j % $width == 0) {
92
                    $out[] = mb_substr($line, $offset, $i - $offset);
93
                    $offset = $i;
94
                }
95
                $j++;
96
            }
97
        }
98
99
        // return any remaining entries
100
        if ($offset != $i - 1) {
101
            $out[] = mb_substr($line, $offset);
102
        }
103
        return $out;
104
    }
105
}
106