Completed
Push — develop ( d73a05...7ce957 )
by Mike
07:24
created

Path::isAbsolutePath()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 0
loc 9
rs 9.2222
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor;
17
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * Value Object for paths.
22
 * This can be absolute or relative.
23
 */
24
final class Path
25
{
26
    /** @var string */
27
    private $path;
28
29
    /**
30
     * Initializes the path.
31
     */
32
    public function __construct(string $path)
33
    {
34
        Assert::stringNotEmpty($path);
35
36
        $this->path = $path;
37
    }
38
39
    /**
40
     * Verifies if another Path object has the same identity as this one.
41
     */
42
    public function equals(self $otherPath): bool
43
    {
44
        return $this->path === (string) $otherPath;
45
    }
46
47
    /**
48
     * returns a string representation of the path.
49
     */
50
    public function __toString(): string
51
    {
52
        return $this->path;
53
    }
54
55
    /**
56
     * Returns whether the file path is an absolute path.
57
     *
58
     * @param string $file A file path
59
     *
60
     * @return bool
61
     */
62
    public static function isAbsolutePath($file)
63
    {
64
        return strspn($file, '/\\', 0, 1)
65
            || (\strlen($file) > 3 && ctype_alpha($file[0])
66
                && ':' === $file[1]
67
                && strspn($file, '/\\', 2, 1)
68
            )
69
            || null !== parse_url($file, PHP_URL_SCHEME);
70
    }
71
}
72