Completed
Push — master ( 22fb31...11f743 )
by Adam
02:50
created

File::isReachableUrl()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 25
cp 0
rs 8.5806
cc 4
eloc 20
nc 5
nop 1
crap 20
1
<?php namespace BestServedCold\PhalueObjects;
2
3
class File extends ValueObject
4
{
5
    /**
6
     * @return bool
7
     */
8
    public function exists()
0 ignored issues
show
Coding Style introduced by
function exists() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
9
    {
10
        if ($this->isUrl()) {
11
            return $this->isReachableUrl();
12
        } else {
13
            return file_exists($this->getValue());
14
        }
15
    }
16
17
    public function isUrl()
18
    {
19
        return filter_var($this->getValue(), FILTER_VALIDATE_URL);
20
    }
21
22
    /**
23
     * Refactor this, it's terrible.
24
     *
25
     * @param bool|true $followRedirects
26
     * @return bool|mixed
27
     */
28
    public function isReachableUrl($followRedirects = true)
29
    {
30
        if (! $ch = curl_init($this->getValue())) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ch. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
31
            return false;
32
        }
33
        curl_setopt($ch, CURLOPT_HEADER, true);    // we want headers
34
        curl_setopt($ch, CURLOPT_NOBODY, true);    // don't need body
35
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    // catch output (do NOT print!)
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
37
        if ($followRedirects) {
38
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
39
            curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
40
        } else {
41
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
42
        }
43
44
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
45
        curl_setopt($ch, CURLOPT_TI/MEOUT, 5);
46
        curl_exec($ch);
47
        if (curl_errno($ch)) {   // should be 0
48
            curl_close($ch);
49
            return false;
50
        }
51
52
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
53
        @curl_close($ch);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
54
55
        return $code;
56
    }
57
58
    /**
59
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
     */
61
    public function getContents()
62
    {
63
        return $this->exists() ? file_get_contents($this->getValue()) : false;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getExtension()
70
    {
71
        return pathinfo($this->getValue(), PATHINFO_EXTENSION);
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getDirectoryName()
78
    {
79
        return pathinfo($this->getValue(), PATHINFO_DIRNAME);
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getFileName()
86
    {
87
        return pathinfo($this->getValue(), PATHINFO_FILENAME);
88
    }
89
90
    /**
91
     * @param string $string
92
     */
93
    public static function fromString($string)
94
    {
95
        return new static($string);
96
    }
97
98
    public static function fromUrl($url)
99
    {
100
        return self::fromString($url);
101
    }
102
}
103