GeneralMethods::output_to_screen()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 8
c 1
b 1
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
4
class GeneralMethods extends Object
5
{
6
7
8
    /**
9
     * opens a location with curl to see if it exists.
10
     *
11
     * @param string $url
12
     *
13
     * @return boolean
14
     */
15
    public static function check_location($url)
16
    {
17
        $handle = curl_init($url);
18
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $ch of curl_setopt() 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

18
        curl_setopt(/** @scrutinizer ignore-type */ $handle, CURLOPT_RETURNTRANSFER, true);
Loading history...
19
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, true);
20
        $response = curl_exec($handle);
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
Bug introduced by
It seems like $handle can also be of type false; however, parameter $ch of curl_exec() 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

20
        $response = curl_exec(/** @scrutinizer ignore-type */ $handle);
Loading history...
21
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $ch of curl_getinfo() 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

21
        $httpCode = curl_getinfo(/** @scrutinizer ignore-type */ $handle, CURLINFO_HTTP_CODE);
Loading history...
22
        $outcome = $httpCode == 200;
23
        curl_close($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $ch of curl_close() 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

23
        curl_close(/** @scrutinizer ignore-type */ $handle);
Loading history...
24
        return $outcome;
25
    }
26
27
28
29
    /**
30
     *
31
     * @use
32
     * ```
33
     *    GeneralMethods::output_to_screen('asdf', 'created')
34
     * ```
35
     * @see DB::alteration_message for types...
36
     *
37
     * @param  string $message
38
     * @param  string $type
39
     */
40
    public static function output_to_screen($message, $type = "")
41
    {
42
        if (Director::is_cli()) {
43
            DB::alteration_message($message, $type);
44
        } else {
45
            echo "<br />";
46
            flush();
47
            ob_end_flush();
48
            DB::alteration_message($message, $type);
49
            ob_start();
50
        }
51
    }
52
53
    /*
54
     * Recursively removes a directory
55
     *
56
     * @param  string $path
57
     */
58
59
    public static function removeDirectory($path)
60
    {
61
        FileSystem::removeFolder($path);
62
    }
63
64
    /*
65
     * Replaces all instances of a string in a file, and rewrites the file
66
     *
67
     * @param string $fileName
68
     * @param string $search
69
     * @param string $replacement
70
     *
71
     **/
72
    public static function replaceInFile($fileName, $search, $replacement)
73
    {
74
        $file = fopen($fileName, 'r');
75
        if ($file) {
0 ignored issues
show
introduced by
$file is of type false|resource, thus it always evaluated to false.
Loading history...
76
            $content = fread($file, filesize($fileName) * 2);
77
            $newContent = str_replace($search, $replacement, $content);
78
            fclose($file);
79
80
81
            $file = fopen($fileName, 'w');
82
            if ($file) {
83
                fwrite($file, $newContent);
84
                fclose($file);
85
            }
86
        }
87
    }
88
}
89