ConsoleDiff::firstDifference()   C
last analyzed

Complexity

Conditions 12
Paths 10

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12.0104

Importance

Changes 0
Metric Value
cc 12
eloc 19
nc 10
nop 2
dl 0
loc 32
rs 6.9666
c 0
b 0
f 0
ccs 23
cts 24
cp 0.9583
crap 12.0104

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Diff;
15
16
use Graze\DiffRenderer\Terminal\ANSI;
17
use Graze\DiffRenderer\Terminal\CursorInterface;
18
19
class ConsoleDiff extends FirstDiff
20
{
21
    const REPLACEMENT_CHAR = "\6";
22
23
    /** @var CursorInterface */
24
    private $cursor;
25
26
    /**
27
     * ConsoleDiff constructor.
28
     *
29
     * @param CursorInterface|null $cursor The cursor used to move around the screen
30
     */
31 39
    public function __construct(CursorInterface $cursor = null)
32
    {
33 39
        $this->cursor = $cursor ?: new ANSI();
34 39
    }
35
36
    /**
37
     * Loop through each line, and produce a difference report of what to write out to the terminal
38
     *
39
     * Assumes the beginning of old and new are the same line
40
     *
41
     * @param string[] $old
42
     * @param string[] $new
43
     *
44
     * @return array [[:col, :str]]
45
     */
46 13
    public function lines(array $old, array $new)
47
    {
48 13
        $diff = parent::lines($old, $new);
49
50 13
        $len = count($new);
51 13
        for ($i = 0; $i < $len; $i++) {
52 13
            if (isset($diff[$i]) && !is_null($new[$i]) && $diff[$i]['col'] > 0) {
53 12
                $styling = $this->cursor->getCurrentFormatting(mb_substr($new[$i], 0, $diff[$i]['col']));
54 12
                $diff[$i]['col'] = mb_strlen($this->cursor->filter(mb_substr($new[$i], 0, $diff[$i]['col'])));
55 12
                if (mb_strlen($styling) > 0) {
56 4
                    $diff[$i]['str'] = $styling . $diff[$i]['str'];
57 4
                }
58 12
            }
59 13
        }
60
61 13
        return $diff;
62
    }
63
64
    /**
65
     * Looks through a line to find the character to of the first difference between the 2 strings.
66
     *
67
     * If no difference is found, returns -1
68
     *
69
     * @param string $old
70
     * @param string $new
71
     *
72
     * @return int
73
     */
74 20
    public function firstDifference($old, $new)
75
    {
76
        // loop through old and new character by character and compare
77 20
        $oldLen = mb_strlen($old);
78 20
        $newLen = mb_strlen($new);
79
80 20
        if ($oldLen === 0) {
81
            return 0;
82
        }
83
84 20
        $oldStripped = $this->cursor->filter($old, static::REPLACEMENT_CHAR);
85 20
        $newStripped = $this->cursor->filter($new, static::REPLACEMENT_CHAR);
86 20
        $lastReal = 0;
87
88 20
        for ($i = 0; $i < $oldLen && $i < $newLen; $i++) {
89 20
            if (mb_substr($old, $i, 1) !== mb_substr($new, $i, 1)) {
90 15
                if (($i > 0)
91 15
                    && ((mb_substr($oldStripped, $i - 1, 1) === static::REPLACEMENT_CHAR)
92 10
                        || (mb_substr($newStripped, $i - 1, 1) === static::REPLACEMENT_CHAR)
93 10
                    )
94 15
                ) {
95 7
                    return $lastReal > 0 ? $lastReal + 1 : 0;
96
                }
97 12
                return $i;
98 19
            } elseif (mb_substr($oldStripped, $i, 1) !== static::REPLACEMENT_CHAR) {
99 17
                $lastReal = $i;
100 17
            }
101 19
        }
102 8
        if ($i < $oldLen || $i < $newLen) {
103 5
            return $i;
104
        }
105 7
        return -1;
106
    }
107
}
108