Completed
Push — utils ( d02d5c )
by Arnaud
02:16
created

Util::isExternalUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil;
10
11
use Symfony\Component\Filesystem\Filesystem;
12
13
class Util
14
{
15
    /**
16
     * Symfony\Component\Filesystem.
17
     *
18
     * @var Filesystem
19
     */
20
    protected static $fs;
21
22
    /**
23
     * Return Symfony\Component\Filesystem instance.
24
     *
25
     * @return Filesystem
26
     */
27
    public static function getFS(): Filesystem
28
    {
29
        if (!self::$fs instanceof Filesystem) {
30
            self::$fs = new Filesystem();
31
        }
32
33
        return self::$fs;
34
    }
35
36
    /**
37
     * Checks if a date is valid.
38
     *
39
     * @param string|null $date
40
     * @param string      $format
41
     *
42
     * @return bool
43
     */
44
    public static function dateIsValid($date, string $format = 'Y-m-d'): bool
0 ignored issues
show
Coding Style introduced by
function dateIsValid() 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...
45
    {
46
        if ($date === null) {
47
            return false;
48
        }
49
        $d = \DateTime::createFromFormat($format, $date);
50
51
        return $d && $d->format($format) === $date;
52
    }
53
54
    /**
55
     * Date to DateTime.
56
     *
57
     * @param mixed $date
58
     *
59
     * @return \DateTime
60
     */
61
    public static function dateToDatetime($date): \DateTime
62
    {
63
        // DateTime
64
        if ($date instanceof \DateTime) {
65
            return $date;
66
        }
67
        // timestamp or AAAA-MM-DD
68
        if (is_numeric($date)) {
69
            return (new \DateTime())->setTimestamp($date);
70
        }
71
        // string (ie: '01/01/2019', 'today')
72
        if (is_string($date)) {
73
            return new \DateTime($date);
74
        }
75
    }
76
77
    /**
78
     * Format class name.
79
     *
80
     * @param \object $class
81
     * @param array   $options
82
     *
83
     * @return string
84
     */
85
    public static function formatClassName($class, array $options = []): string
86
    {
87
        $lowercase = false;
88
        extract($options);
89
90
        $className = substr(strrchr(get_class($class), '\\'), 1);
91
        if ($lowercase) {
92
            $className = strtolower($className);
93
        }
94
95
        return $className;
96
    }
97
98
    /**
99
     * Test if a string is an external URL or not.
100
     *
101
     * @param string|null $url
102
     *
103
     * @return bool
104
     */
105
    public static function isExternalUrl($url): bool
106
    {
107
        if ($url === null) {
108
            return false;
109
        }
110
111
        if (preg_match('~^(?:f|ht)tps?://~i', $url)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) preg_match...f|ht)tps?://~i', $url);.
Loading history...
112
            return true;
113
        }
114
115
        return false;
116
    }
117
}
118