Completed
Push — master ( dc6ae9...666f34 )
by Andrii
12:22
created

FileHelper::mkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2.5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 13
ccs 1
cts 2
cp 0.5
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 8
nc 2
nop 1
crap 2.5
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\helpers;
12
13
use Yii;
14
15
/**
16
 * Hidev FileHelper.
17
 */
18
class FileHelper
19
{
20
    /**
21
     * Writes given content to the file.
22
     * Doesn't touch file if it has exactly same content.
23
     * Creates intermediate directories when necessary.
24
     * @param string $path
25
     * @param string $content
26
     * @return bool true if file was changed
27
     */
28 5
    public static function write($path, $content)
29
    {
30 5
        $path = Yii::getAlias($path);
31 5
        if (is_file($path) && file_get_contents($path) === $content) {
32 5
            return false;
33 5
        }
34 5
35
        static::mkdir(dirname($path));
36 5
        if (!is_writable($path) && posix_isatty(0)) {
37
            $tmp = tempnam('/tmp', 'hidev.');
38
            file_put_contents($tmp, $content);
39
            chmod($tmp, 0644);
40
            passthru("sudo cp $tmp $path");
41
            unlink($tmp);
42
        } else {
43
            file_put_contents($path, $content);
44
        }
45
        Yii::warning('Written file: ' . $path, 'file');
46
47
        return true;
48 5
    }
49
50 5
    /**
51 5
     * Creates directory if not exists.
52 5
     * Creates intermediate directories when necessary.
53
     * @param string $path
54
     * @return bool true if directory did not exist and was created
55
     */
56
    public static function mkdir($path)
57
    {
58
        $path = Yii::getAlias($path);
59 5
        $path = rtrim(trim($path), '/');
60
        if (!file_exists($path)) {
61
            mkdir($path, 0777, true);
62
            Yii::warning('Created dir:  ' . $path . '/', 'file');
63
64
            return true;
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * Creates a symlink.
72
     * @param string $src existing source path
73
     * @param string $dst destionation path to be created
74
     * @return true on success or false on failure
75
     */
76
    public static function symlink($src, $dst)
77
    {
78
        if (!is_writable($dst) && posix_isatty(0)) {
79
            passthru("sudo ln -s $src $dst", $retval);
80
            return $retval === 0;
81
        } else {
82
            return symlink($this->path, $dest);
0 ignored issues
show
Bug introduced by
The property path does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $dest does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
83
        }
84
    }
85
}
86