GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( ef6a72...d39843 )
by Denis
03:25
created

ExcelHelper::escapeCellFormat()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 5.0864
cc 14
eloc 20
nc 9
nop 1

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
namespace Ellumilel\Helpers;
3
4
/**
5
 * Class PHPExcelHelper
6
 * @package Ellumilel\Helpers
7
 */
8
class ExcelHelper
9
{
10
    /**
11
     * @link http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx
12
     * XFE1048577
13
     */
14
    const EXCEL_MAX_ROW = 1048576;
15
    const EXCEL_MAX_RANGE = 2147483647;
16
    const EXCEL_MAX_COL = 16384;
17
18
    /**
19
     * @param string $dateInput
20
     *
21
     * @return mixed
22
     */
23
    public static function convertDateTime($dateInput)
24
    {
25
        $epoch = 1900;
26
        $norm = 300;
27
        $year = $month = $day = $offset = 0;
28
        $dateTime = $dateInput;
29
        if (preg_match("/(\d{4})\-(\d{2})\-(\d{2})/", $dateTime, $matches)) {
30
            $year = $matches[1];
31
            $month = $matches[2];
32
            $day = $matches[3];
33
        }
34
        $seconds = self::getSeconds($dateTime);
35
        if ("$year-$month-$day" == '1899-12-31' || "$year-$month-$day" == '1900-01-00') {
36
            return $seconds;
37
        }
38
        if ("$year-$month-$day" == '1900-02-29') {
39
            return 60 + $seconds;
40
        }
41
        $range = $year - $epoch;
42
        // check leapDay
43
        $leap = (new \DateTime($dateInput))->format('L');
44
        $mDays = [31, ($leap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
45
        if (self::checkEpoch($year, $month, $day, $mDays[$month - 1])) {
46
            return 0;
47
        }
48
        $days = $day + ($range * 365) + array_sum(array_slice($mDays, 0, $month - 1));
49
        $days += intval(($range) / 4) - intval(($range + $offset) / 100);
50
        $days += intval(($range + $offset + $norm) / 400) - intval($leap);
51
        if ($days > 59) {
52
            $days++;
53
        }
54
55
        return $days + $seconds;
56
    }
57
58
    /**
59
     * @param $year
60
     * @param $month
61
     * @param $day
62
     * @param $dayCheck
63
     *
64
     * @return bool
65
     */
66
    private function checkEpoch($year, $month, $day, $dayCheck)
67
    {
68
        if (($year < 1900 || $year > 9999) || ($month < 1 || $month > 12) || $day < 1 || $day > $dayCheck) {
69
            return true;
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * @param $val
77
     *
78
     * @return mixed
79
     */
80
    public static function xmlspecialchars($val)
81
    {
82
        return str_replace("'", "&#39;", htmlspecialchars($val));
83
    }
84
85
    /**
86
     * @param int $rowNumber
87
     * @param int $columnNumber
88
     *
89
     * @return string Cell label/coordinates (A1, C3, AA42)
90
     */
91
    public static function xlsCell($rowNumber, $columnNumber)
92
    {
93
        $n = $columnNumber;
94
        for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) {
95
            $r = chr($n % 26 + 0x41).$r;
96
        }
97
98
        return $r.($rowNumber + 1);
99
    }
100
101
    /**
102
     * @link https://msdn.microsoft.com/ru-RU/library/aa365247%28VS.85%29.aspx
103
     *
104
     * @param string $filename
105
     *
106
     * @return mixed
107
     */
108
    public static function checkFilename($filename)
109
    {
110
        $invalidCharacter = array_merge(
111
            array_map('chr', range(0, 31)),
112
            ['<', '>', '?', '"', ':', '|', '\\', '/', '*', '&']
113
        );
114
115
        return str_replace($invalidCharacter, '', $filename);
116
    }
117
118
    /**
119
     * @todo  check escaping
120
     *
121
     * @param string $cellFormat
122
     *
123
     * @return string
124
     */
125
    public static function escapeCellFormat($cellFormat)
126
    {
127
        $ignoreUntil = '';
128
        $escaped = '';
129
        for ($i = 0, $ix = strlen($cellFormat); $i < $ix; $i++) {
130
            $c = $cellFormat[$i];
131
            if ($ignoreUntil == '' && $c == '[') {
132
                $ignoreUntil = ']';
133
            } else {
134
                if ($ignoreUntil == '' && $c == '"') {
135
                    $ignoreUntil = '"';
136
                } else {
137
                    if ($ignoreUntil == $c) {
138
                        $ignoreUntil = '';
139
                    }
140
                }
141
            }
142
            if ($ignoreUntil == '' &&
143
                ($c == ' ' || $c == '-' || $c == '(' || $c == ')') &&
144
                ($i == 0 || $cellFormat[$i - 1] != '_')
145
            ) {
146
                $escaped .= "\\".$c;
147
            } else {
148
                $escaped .= $c;
149
            }
150
        }
151
152
        return $escaped;
153
    }
154
155
    /**
156
     * @param $dateTime
157
     *
158
     * @return float|int
159
     */
160
    private function getSeconds($dateTime)
161
    {
162
        $seconds = 0;
163
164
        if (preg_match("/(\d{2}):(\d{2}):(\d{2})/", $dateTime, $matches)) {
165
            $seconds = ($matches[1] * 60 * 60 + $matches[2] * 60 + $matches[3]) / 86400;
166
        }
167
168
        return $seconds;
169
    }
170
}
171