Completed
Push — master ( c398b8...7a98a5 )
by Jitendra
10s
created

Path::join()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Util;
13
14
use Ahc\Json\Comment;
15
16
class Path
17
{
18
    /** @var string */
19
    protected $phintPath;
20
21
    /**
22
     * Platform agnostic absolute path detection.
23
     *
24
     * @param string $path
25
     *
26
     * @return bool
27
     */
28
    public function isAbsolute(string $path): bool
29
    {
30
        if (DIRECTORY_SEPARATOR === '\\') {
31
            return strpos($path, ':') === 1;
32
        }
33
34
        return isset($path[0]) && $path[0] === '/';
35
    }
36
37
    public function getRelativePath($fullPath, $basePath): string
38
    {
39
        if (strpos($fullPath, $basePath) === 0) {
40
            return substr($fullPath, strlen($basePath));
41
        }
42
43
        // Hmm!
44
        return $fullPath;
45
    }
46
47
    public function ensureDir(string $dir): bool
48
    {
49
        if (!\is_dir($dir)) {
50
            return \mkdir($dir, 0777, true);
51
        }
52
53
        return true;
54
    }
55
56
    public function getExtension(string $filePath): string
57
    {
58
        return \pathinfo($filePath, \PATHINFO_EXTENSION);
59
    }
60
61
    public function readAsJson(string $filePath, bool $asArray = true)
62
    {
63
        return (new Comment)->decode(\file_get_contents($filePath), $asArray);
64
    }
65
66
    public function writeFile(string $file, $content, int $mode = null): bool
67
    {
68
        $this->ensureDir(\dirname($file));
69
70
        if (!\is_string($content)) {
71
            $content = \json_encode($content, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES);
72
        }
73
74
        return \file_put_contents($file, $content, $mode) > 0;
75
    }
76
77
    public function getPhintPath(string $subpath = ''): string
78
    {
79
        $this->initPhintPath();
80
81
        if ($subpath && $this->phintPath) {
82
            return $this->phintPath . '/' . \ltrim($subpath, '/');
83
        }
84
85
        return $this->phintPath;
86
    }
87
88
    public function createBinaries(array $bins, string $basePath)
89
    {
90
        foreach ($bins as $bin) {
91
            $bin = $this->join($basePath, $bin);
92
93
            if ($this->writeFile($bin, "#!/usr/bin/env php\n<?php\n")) {
94
                @\chmod($bin, 0755);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). 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

94
                /** @scrutinizer ignore-unhandled */ @\chmod($bin, 0755);

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...
95
            }
96
        }
97
    }
98
99
    public function join(...$paths): string
100
    {
101
        if (\is_array($paths[0] ?? '')) {
102
            $paths = $paths[0];
103
        }
104
105
        $join = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $join is dead and can be removed.
Loading history...
106
        foreach ($paths as $i => &$path) {
107
            $path = $i === 0 ? \rtrim($path, '\\/') : \trim($path, '\\/');
108
        }
109
110
        return \implode('/', $paths);
111
    }
112
113
    protected function initPhintPath()
114
    {
115
        if (null !== $this->phintPath) {
116
            return;
117
        }
118
119
        $this->phintPath = '';
120
121
        if (false !== $home = ($_SERVER['HOME'] ?? \getenv('HOME'))) {
122
            $path = \rtrim($home, '/') . '/.phint';
123
124
            if ($this->ensureDir($path)) {
125
                $this->phintPath = $path;
126
            }
127
        }
128
    }
129
}
130