Completed
Push — master ( f86360...4e757b )
by Peter
03:09
created

Tabs::trim()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 6.7272
cc 7
eloc 17
nc 24
nop 1
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Zamm\Helpers;
10
11
/**
12
 * Tabs
13
 *
14
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
15
 */
16
class Tabs
17
{
18
19
	public static function trim(&$lines)
20
	{
21
		$string = false;
22
		if(is_string($lines))
23
		{
24
			$string = true;
25
			$lines = explode(PHP_EOL, $lines);
26
		}
27
		// Trim empty tabs columns
28
		$minTabs = 666;
29
		foreach ($lines as $line)
30
		{
31
			$matches = [];
32
			preg_match("~^\t+~", $line, $matches);
33
			if (!isset($matches[0]))
34
			{
35
				continue;
36
			}
37
			$minTabs = min([$minTabs, strlen($matches[0])]);
38
		}
39
		if ($minTabs < 666)
40
		{
41
			foreach ($lines as &$line)
42
			{
43
				$line = preg_replace("~^\t{{$minTabs}}~", '', $line);
44
			}
45
		}
46
		if($string)
47
		{
48
			$lines = implode(PHP_EOL, $lines);
49
		}
50
	}
51
52
}
53