Completed
Push — 2.x ( 8cf368 )
by Fabian
13:26
created

PhpMergeBase::postMergeAlter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the php-merge package.
4
 *
5
 * (c) Fabian Bircher <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PhpMerge\internal;
12
13
use PhpMerge\PhpMergeInterface;
14
15
/**
16
 * Class PhpMergeBase
17
 *
18
 * The base class implementing only the simplest logic which is common to all
19
 * implementations.
20
 *
21
 * @package    PhpMerge
22
 * @author     Fabian Bircher <[email protected]>
23
 * @copyright  Fabian Bircher <[email protected]>
24
 * @license    https://opensource.org/licenses/MIT
25
 * @version    Release: @package_version@
26
 * @link       http://github.com/bircher/php-merge
27
 * @internal   This class is not part of the public api.
28
 */
29
abstract class PhpMergeBase implements PhpMergeInterface
30
{
31
32
    /**
33
     * Merge obvious cases when only one text changes..
34
     *
35
     * @param string $base
36
     *   The original text.
37
     * @param string $remote
38
     *   The first variant text.
39
     * @param string $local
40
     *   The second variant text.
41
     *
42
     * @return string|null
43
     *   The merge result or null if the merge is not obvious.
44
     */
45 21
    protected static function simpleMerge(string $base, string $remote, string $local)
46
    {
47
        // Skip complex merging if there is nothing to do.
48 21
        if ($base == $remote) {
49 4
            return $local;
50
        }
51 19
        if ($base == $local) {
52 2
            return $remote;
53
        }
54 17
        if ($remote == $local) {
55 2
            return $remote;
56
        }
57
        // Return nothing and let sub-classes deal with it.
58 15
        return null;
59
    }
60
61
    /**
62
     * Split it line-by-line.
63
     *
64
     * @param string $input
65
     *
66
     * @return array
67
     */
68 12
    protected static function splitStringByLines(string $input): array
69
    {
70 12
        return \preg_split('/(.*\R)/', $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
71
    }
72
73
    /**
74
     * @param $text
75
     * @return string
76
     */
77 15
    protected static function preMergeAlter($text) : string
78
    {
79
        // Append new lines so that conflicts at the end of the text work.
80 15
        return $text . "\nthe\nend";
81
    }
82
83
    /**
84
     * @param $text
85
     * @return bool|string
86
     */
87 15
    protected static function postMergeAlter($text) : string
88
    {
89
        // Remove the appended lines.
90 15
        return (string) substr($text, 0, -8);
91
    }
92
}
93