Completed
Push — master ( fa8877...7435ca )
by Sebastian
02:00
created

Util::normalizeLineEndings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
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 SebastianFeldmann\Cli.
4
 *
5
 * (c) Sebastian Feldmann <[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
namespace SebastianFeldmann\Cli\Output;
11
12
/**
13
 * Class Util
14
 *
15
 * @package SebastianFeldmann\Cli
16
 * @author  Sebastian Feldmann <[email protected]>
17
 * @link    https://github.com/sebastianfeldmann/cli
18
 * @since   Class available since Release 2.1.0
19
 */
20
class Util
21
{
22
    /**
23
     * Remove empty entries at the end of an array.
24
     *
25
     * @param  array $lines
26
     * @return array
27
     */
28 8
    public static function trimEmptyLines(array $lines) : array
29
    {
30 8
        for ($last = count($lines) - 1; $last > -1; $last--) {
31 8
            if (!empty($lines[$last])) {
32 6
                return $lines;
33
            }
34 4
            unset($lines[$last]);
35
        }
36 2
        return $lines;
37
    }
38
39
    /**
40
     * Replaces all 'known' line endings with unix \n line endings
41
     *
42
     * @param  string $text
43
     * @return string
44
     */
45 5
    public static function normalizeLineEndings(string $text): string
46
    {
47 5
        return preg_replace('~(BSR_ANYCRLF)*\R~', "\n", $text);
48
    }
49
}
50