Passed
Push — master ( 2e5f1b...1edd03 )
by Igor
02:55
created

Helper::makeSomeDataFile()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 24
nc 7
nop 2
dl 0
loc 39
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace ClickHouseDB\Example;
4
5
class Helper
6
{
7
    public static function init()
8
    {
9
        date_default_timezone_set('Europe/Moscow');
10
        error_reporting( E_ALL );
11
        ini_set('display_errors',1);
12
    }
13
14
        /**
15
     * @param $file_name
16
     * @param int $from_id
17
     * @param int $to_id
18
     */
19
    public static function makeListSitesKeysDataFile($file_name, $from_id = 1000, $to_id = 20000)
20
    {
21
        @unlink($file_name);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

21
        /** @scrutinizer ignore-unhandled */ @unlink($file_name);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
22
23
        $handle = fopen($file_name, 'w');
24
        $rows = 0;
25
26
        for ($f = $from_id; $f < $to_id; $f++) {
27
            $j['site_id'] = $f;
28
            $j['site_hash'] = md5($f);
29
30
            fputcsv($handle, $j);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $j does not seem to be defined for all execution paths leading up to this point.
Loading history...
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            fputcsv(/** @scrutinizer ignore-type */ $handle, $j);
Loading history...
31
            $rows = $rows + 1;
32
        }
33
34
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
35
36
        echo "Created file  [$file_name]: $rows rows...\n";
37
    }
38
39
40
    /**
41
     * @param $size
42
     * @param string $unit
43
     * @return string
44
     */
45
    public static function humanFileSize($size, $unit = '')
46
    {
47
        if ((!$unit && $size >= 1 << 30) || $unit == 'GB') {
48
            return number_format($size / (1 << 30), 2) . ' GB';
49
        }
50
        if ((!$unit && $size >= 1 << 20) || $unit == 'MB') {
51
            return number_format($size / (1 << 20), 2) . ' MB';
52
        }
53
        if ((!$unit && $size >= 1 << 10) || $unit == 'KB') {
54
            return number_format($size / (1 << 10), 2) . ' KB';
55
        }
56
57
        return number_format($size) . ' bytes';
58
    }
59
60
    /**
61
     * @param $file_name
62
     * @param int $size
63
     */
64
    public static function makeSomeDataFile($file_name, $size = 10)
65
    {
66
        @unlink($file_name);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

66
        /** @scrutinizer ignore-unhandled */ @unlink($file_name);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
67
68
        $handle = fopen($file_name, 'w');
69
        $z = 0;
70
        $rows = 0;
71
        $j = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $j is dead and can be removed.
Loading history...
72
73
        for ($ules = 0; $ules < $size; $ules++) {
74
            for ($dates = 0; $dates < 5; $dates++) {
75
                for ($site_id = 12; $site_id < 49; $site_id++) {
76
                    for ($hours = 0; $hours < 24; $hours++) {
77
                        $z++;
78
79
                        $dt = strtotime('-' . $dates . ' day');
80
                        $dt = strtotime('-' . $hours . ' hour', $dt);
81
82
                        $j = [];
83
                        $j['event_time'] = date('Y-m-d H:00:00', $dt);
84
                        $j['url_hash'] = 'XXXX' . $site_id . '_' . $ules;
85
                        $j['site_id'] = $site_id;
86
                        $j['views'] = 1;
87
88
                        foreach (['00', 55] as $key) {
89
                            $z++;
90
                            $j['v_' . $key] = ($z % 2 ? 1 : 0);
91
                        }
92
93
                        fputcsv($handle, $j);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
                        fputcsv(/** @scrutinizer ignore-type */ $handle, $j);
Loading history...
94
                        $rows++;
95
                    }
96
                }
97
            }
98
        }
99
100
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

100
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
101
102
        echo "Created file  [$file_name]: $rows rows...\n";
103
    }
104
105
106
    /**
107
     * @param $file_name
108
     * @param int $size
109
     * @return bool
110
     */
111
    public static function makeSomeDataFileBigOldDates($file_name, $size = 10)
112
    {
113
        if (is_file($file_name)) {
114
            echo "Exist file  [$file_name]: ± rows... size = " . self::humanFileSize(filesize($file_name)) . " \n";
115
            return false;
116
        }
117
118
        @unlink($file_name);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

118
        /** @scrutinizer ignore-unhandled */ @unlink($file_name);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
119
120
121
        $handle = fopen($file_name, 'w');
122
        $rows = 0;
123
124
        for ($day_ago = 0; $day_ago < 360; $day_ago++) {
125
            $date = strtotime('-' . $day_ago . ' day');
126
            for ($hash_id = 1; $hash_id < (1 + $size); $hash_id++)
127
                for ($site_id = 100; $site_id < 199; $site_id++) {
128
                    $j['event_time'] = date('Y-m-d H:00:00', $date);
129
                    $j['site_id'] = $site_id;
130
                    $j['hash_id'] = $hash_id;
131
                    $j['views'] = 1;
132
133
                    fputcsv($handle, $j);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
                    fputcsv(/** @scrutinizer ignore-type */ $handle, $j);
Loading history...
134
                    $rows++;
135
                }
136
        }
137
138
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
139
140
        echo "Created file  [$file_name]: $rows rows... size = " . self::humanFileSize(filesize($file_name)) . " \n";
141
    }
142
143
144
    /**
145
     * @param $file_name
146
     * @param int $size
147
     * @return bool
148
     */
149
    public static function makeSomeDataFileBig($file_name, $size = 10, $shift = 0)
150
    {
151
        if (is_file($file_name)) {
152
            echo "Exist file  [$file_name]: ± rows... size = " . self::humanFileSize(filesize($file_name)) . " \n";
153
            return false;
154
        }
155
156
        @unlink($file_name);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

156
        /** @scrutinizer ignore-unhandled */ @unlink($file_name);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
157
158
159
        $handle = fopen($file_name, 'w');
160
        $z = 0;
161
        $rows = 0;
162
        $j = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $j is dead and can be removed.
Loading history...
163
164
        for ($ules = 0; $ules < $size; $ules++) {
165
            for ($dates = 0; $dates < 5; $dates++) {
166
                for ($site_id = 12; $site_id < 49; $site_id++) {
167
                    for ($hours = 0; $hours < 24; $hours++) {
168
                        $z++;
169
170
                        $dt = strtotime('-' . ($dates + $shift) . ' day');
171
                        $dt = strtotime('-' . $hours . ' hour', $dt);
172
173
                        $j = [];
174
                        $j['event_time'] = date('Y-m-d H:00:00', $dt);
175
                        $j['url_hash'] = sha1('XXXX' . $site_id . '_' . $ules) . sha1(microtime() . $site_id . ' ' . mt_rand()) . sha1('XXXX' . $site_id . '_' . $ules);
176
                        $j['site_id'] = $site_id;
177
                        $j['views'] = 1;
178
179
                        foreach (['00', 55] as $key) {
180
                            $z++;
181
                            $j['v_' . $key] = ($z % 2 ? 1 : 0);
182
                        }
183
184
                        fputcsv($handle, $j);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
                        fputcsv(/** @scrutinizer ignore-type */ $handle, $j);
Loading history...
185
                        $rows++;
186
                    }
187
                }
188
            }
189
        }
190
191
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

191
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
192
193
        echo "Created file  [$file_name]: $rows rows... size = " . self::humanFileSize(filesize($file_name)) . " \n";
194
    }
195
196
}